[英]Correct way to require extensions with browserify
What is the best way is to include an extension for a common library like jQuery or Knockout with Browserify ? 最好的方法是在Browserify中包含jQuery或Knockout等通用库的扩展吗?
For example, with a project like knockout-switch-case
, the global ko
(knockout) variables is not passed to the module-definition call. 例如,对于像
knockout-switch-case
这样的项目,全局ko
(基因敲除)变量不会传递给模块定义调用。
The AMD code for knockout-switch-case is: 敲除开关盒的AMD代码为:
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['knockout'], factory);
} else {
// Browser globals
factory(root.ko);
}
}(this, function(ko) {
where it expects ko
(knockout) to be a global on root
, which would ordinarily be window
but when using Browserify it is Object {}
. 它期望
ko
(knockout)是root
上的全局变量,该root
通常是window
但是在使用Browserify时,它是Object {}
。
I have tried with the example using browserify-shim
with something like this, but it did not work as expected (though it did work for knockout-mapping , which has a better module-dance): 我已经尝试过使用带有类似以下内容的
browserify-shim
的示例,但是它没有按预期方式工作(尽管它确实适用于敲除映射 ,后者具有更好的模块舞步):
knockout:
path: VENDOR_PATH + '/knockout.js'
exports: 'ko'
depends:
jquery: '$'
I feel as though I must be overlooking something that must be quite obvious, as I expect this would be a fairly common module definition pattern for including any jQuery, Knockout or any other extension for a library that relies on a global. 我觉得我似乎必须忽略一些必须显而易见的东西,因为我希望这将是一个相当常见的模块定义模式,用于包括任何jQuery,Knockout或依赖于全局库的任何其他扩展。 Or perhaps this is an issue fairly specific to something knockout-switch-case is doing.
或者,这可能是特定于敲除开关盒正在做的事情的问题。
In any case, thoughts and comments sincerely appreciated. 无论如何,衷心感谢您的想法和评论。
This browserify-shim config works for me: 这个browserify-shim配置适用于我:
shim(browserify(), {
jquery: {
path: './js/vendor/jquery.js',
exports: '$'
},
'knockout': {
path: './js/vendor/knockout.js',
exports: 'ko',
depends: {
jquery: '$'
}
},
'knockout-switch-case': {
path: './js/vendor/knockout-switch-case.js',
exports: null,
depends: {
knockout: 'ko'
}
}
})
With that, you can require as usual: var ko = require('knockout');
这样,您可以像往常一样要求:
var ko = require('knockout');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.