dts gen ReferenceError:未定义jQuery
我正在尝试为jquery colorbox生成定义文件。我从项目根目录中使用:dts gen ReferenceError:未定义jQuery,jquery,typescript,Jquery,Typescript,我正在尝试为jquery colorbox生成定义文件。我从项目根目录中使用: dts-gen -m jquery-colorbox 它失败于: Unexpected crash! Please log a bug with the commandline you specified. /usr/local/lib/node_modules/dts-gen/bin/lib/run.js:130 throw e; ^ ReferenceError: jQuery
dts-gen -m jquery-colorbox
它失败于:
Unexpected crash! Please log a bug with the commandline you specified.
/usr/local/lib/node_modules/dts-gen/bin/lib/run.js:130
throw e;
^
ReferenceError: jQuery is not defined
at Object.<anonymous> (/home/adr/Projects/albums/node_modules/jquery-colorbox/jquery.colorbox.js:1105:3)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/usr/local/lib/node_modules/dts-gen/bin/lib/run.js:57:67)
at Module._compile (module.js:653:30)
意外崩溃!请使用指定的命令行记录错误。
/usr/local/lib/node_modules/dts gen/bin/lib/run.js:130
投掷e;
^
ReferenceError:未定义jQuery
反对。(/home/adr/Projects/albums/node_modules/jquery colorbox/jquery.colorbox.js:1105:3)
编译(Module.js:653:30)
在Object.Module.\u extensions..js(Module.js:664:10)
在Module.load(Module.js:566:32)
在tryModuleLoad时(module.js:506:12)
在Function.Module.\u加载(Module.js:498:3)
at Module.require(Module.js:597:17)
根据需要(内部/module.js:11:18)
反对。(/usr/local/lib/node_modules/dts gen/bin/lib/run.js:57:67)
编译(Module.js:653:30)
我确实安装了jQuery和其他必需的库(全局和本地)。如何“告诉”dts gen“加载”/“使用”jQuery?第一个问题是
dts gen
需要实际加载您指定的模块以检查其结构,而jQuery colorbox
需要在全局对象上定义jQuery
、document
和window
,它们不在Node.js中。一种可能的方法(我没有追求)是使用dtsgen
的实验支持。另一种方法是加载与Node.js兼容的DOM实现,如jsdom
,并设置必要的全局变量,以便可以成功加载jquery colorbox
。具体来说,如果您在名为jquery colorbox wrapper.js的文件中编写以下内容(基于):
然后运行dtsgen--expression文件jquery colorbox wrapper.js
,它应该会成功运行
然后您将遇到第二个问题:生成的声明文件是空的,因为jquery colorbox
,作为jquery插件,向jquery对象添加内容,并且没有自己的导出。如果更改包装器脚本以返回jQuery对象,那么将得到jQuery colorbox
的声明与jQuery本身的声明混合。一种合理的方法是区分使用和不使用jQuery colorbox
的jQuery的声明文件。即追加global.jQuery
到上面给出的jquery colorbox wrapper.js
文件,运行dts gen
一次,然后注释掉require(“jquery colorbox”)代码>行并使用不同的输出文件再次运行dts gen
,最后区分这两个文件。这就给我留下了特定于jquery colorbox
的声明以及sizzle*
变量中的一些差异,这些变量的名称包含时间戳,应该很容易删除。当然,声明文件将需要大量清理。您可能想知道,名称下已有一个可用于jquery colorbox
的类型声明包。很高兴知道,谢谢
var jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM();
const { document } = (new JSDOM('')).window;
global.jQuery = require('jquery')(window);
global.document = document;
global.window = window;
require("jquery-colorbox");