我只是检查browserify
如何创建捆绑代码。 prelude.js包含使用以下格式的参数执行的IIFE:
({
id_one: [function(require, module, exports) {
// Module Code
}, {
dependency_one: id_two,
dependency_two: id_three
}],
}, {}, [entryid])
但是,我无法理解为什么我们需要将这四个额外的参数outer, modules, cache, entry传递给包装模块代码的函数?它执行的函数的签名格式为require
,module
,exports
,如下例所示。我尝试了没有这个,它也可以。
一个例子是:
// modules are defined as an array
// [ module function, map of requireuires ]
//
// map of requireuires is short require name -> numeric require
//
// anything defined in a previous bundle is accessed via the
// orig method which is the requireuire for previous bundles
(function outer(modules, cache, entry) {
// Save the require from previous bundle to this closure if any
var previousRequire = typeof require === "function" && require;
function newRequire(name, jumped) {
if (!cache[name]) {
if (!modules[name]) {
// If we cannot find the module within our internal map or
// cache, jump to the current global require ie. the last bundle
// that was added to the page.
var currentRequire = typeof require == "function" && require;
if (!jumped && currentRequire) {
return currentRequire(name, true);
}
// If there are other bundles on this page the require from the
// previous one is saved to 'previousRequire'. Repeat this as
// many times as there are bundles until the module is found or
// we exhaust the require chain.
if (previousRequire) {
return previousRequire(name, true);
}
var err = new Error('Cannot find module \'' + name + '\'');
err.code = 'MODULE_NOT_FOUND';
throw err;
}
var m = cache[name] = {exports:{}};
modules[name][0].call(m.exports, function(x){
var id = modules[name][1][x];
return newRequire(id ? id : x);
},m,m.exports,outer,modules,cache,entry);
}
return cache[name].exports;
}
for(var i=0;i<entry.length;i++) newRequire(entry[i]);
// Override the current require with this new one
return newRequire;
})({
1: [function(require, module, exports) {
modulea = require("./modulea.js");
moduleb = require("./moduleb.js");
logger = function() {
console.log(modulea + modulea);
};
console.log("Main executed.")
}, {
"./modulea.js": 2,
"./moduleb.js": 3
}],
2: [function(require, module, exports) {
module.exports.name = "Module A";
}, {}],
3: [function(require, module, exports) {
module.exports.name = "Module B";
}, {}]
}, {}, [1]);