Browserify shim似乎没有将Tether附加到窗口对象

时间:2016-04-22 12:06:43

标签: javascript browserify-shim tether

我的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未捕获的TypeError:无法设置属性' Tether'未定义的

但是,如果我没有尝试使用Tether并在需要它的模块中使用window.Tether,那么它可以正常工作。

const $ = require('jquery');
window.Tether = require('tether');

有谁知道为什么browserify-shim不会以这种方式为Tether工作?

1 个答案:

答案 0 :(得分:2)

您是对的 - 您需要手动指定捆绑包中的window对象。

我不是百分百肯定,但我的理解是this part of the documentation,当它说

  

x导出窗口。$

实际上意味着$可用于中的所有模块 $ - 这并不意味着您的webapp的window对象。

例如参见this issue

问题在于文档的那一部分,似乎人们认为该对象应该是window的一部分 - 改变其措辞可能是一个好主意。