我想将一些常用代码捆绑为CommonJS模块,然后使用来自不同捆绑包和/或直接来自全局的常用模块。
entry1-common.js
-- a.js
-- b.js
entry2-app.js
-- x.js
inside i would like to to access entry1-common's a.js here
var moduleA = require('./a.js');
<script>
// i would also like to access modules from outside
var moduleA = require('./a.js');
var moduleX = require('./x.js');
</script>
我正在使用gulp。一些浏览器选项似乎是我需要的但不能让我在那里:
browserify(bundleConfigs: [{
entries: './entry1-common.js',
dest: dest,
outputName: 'common.js',
hasExports: true, // this gives me require() function on the outside
require: ['jquery']
}])
我是否需要捆绑&#39;通过&#39;和&#39;双工器&#39; ?我在浏览器文档中看到了相关示例。
我可以在我的gulp任务中创建两个单独的包,但我不知道如何从一个到另一个访问模块。
答案 0 :(得分:1)
阅读Webpack,我看到他们解决了很多问题,包括上面的问题。您可以系统地外化所有内容,如webpack文档中所示。下面的代码段:
externals: [
{
a: false, // a is not external
b: true, // b is external (require("b"))
"./c": "c", // "./c" is external (require("c"))
"./d": "var d" // "./d" is external (d)
},
// Every non-relative module is external
// abc -> require("abc")
/^[a-z\-0-9]+$/,
function(context, request, callback) {
// Every module prefixed with "global-" becomes external
// "global-abc" -> abc
if(/^global-/.test(request))
return callback(null, "var " + request.substr(7));
callback();
},
"./e" // "./e" is external (require("./e"))
]