0%

commonjs

example.js

1
2
3
const inc = require('./increment').increment;
const a = 1;
console.log(inc(a)); // 2

increment.js

1
2
3
4
5
const add = require('./math').add;
exports.increment = function(val) {
return add(val, 1);
};

math.js

1
2
3
4
5
6
7
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};

output.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(() => {
//定义了一个 __webpack_modules__数组
var __webpack_modules__ = ([
//index 0
,
//index 1 increment
((__unused_webpack_module, exports, __webpack_require__) => {
// add 方法来自于 index 2中
const add = __webpack_require__(2).add;
exports.increment = function (val) {
return add(val, 1);
};


}),
//index 2 add
((__unused_webpack_module, exports) => {

exports.add = function () {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};

})
]);
// 缓存
var __webpack_module_cache__ = {};

function __webpack_require__(moduleId) {
var cachedModule = __webpack_module_cache__[moduleId];
// 如果有缓存先从缓存获取
if (cachedModule !== undefined) {
return cachedModule.exports;
}
// 没有缓存定义一个缓存 __webpack_module_cache__ 并复制给module
var module = __webpack_module_cache__[moduleId] = {
exports: {}
};
// 获取exports
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
return module.exports;
}

var __webpack_exports__ = {};
//
(() => {
//
const inc = __webpack_require__(1).increment;
const a = 1;
console.log(inc(a)); // 2

})();
})()
;