我的 package.json 文件中有以下内容:
"browserify": {
"transform": [
"browserify-shim"
]
},
"browser": {
"jquery": "./node_modules/jquery/dist/jquery.js",
"tether": "./node_modules/tether/dist/tether.js"
},
"browserify-shim": {
"jquery": "$",
"tether": "Tether"
}
然后这是我的 JS 模块之一:
const $ = require('jquery');
const Tether = require('tether');
然后我在浏览器中收到以下错误:
tether.min.js:1 Uncaught TypeError: Cannot set property 'Tether' of undefined
但是,如果我不尝试对 Tether 进行填充,而只是在需要它的模块中使用 window.Tether
,它就可以正常工作。
const $ = require('jquery');
window.Tether = require('tether');
有谁知道为什么 browserify-shim 不能以这种方式为 Tether 工作?
最佳答案
您是对的 - 您需要从包中手动指定 window
对象。
我不是 100% 确定,但我的理解是 this part of the documentation ,当它说
x exports window.$
实际上意味着$
可作为$
用于 bundle 内的所有模块 - 这并不意味着窗口
您的 web 应用程序的对象。
例如参见 this issue .
问题出在文档的那一部分,人们似乎认为该对象应该是窗口
的一部分 - 更改其措辞可能是个好主意。
关于javascript - Browserify shim 似乎没有将 Tether 附加到窗口对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36793438/