使用过nodejs的小伙伴应该都知道, commonjs规范其实就是用在nodejs服务器端的
commonjs 其实是同步加载机制
我们先来看一下这段代码,这个其实就是webpack 将 commonjs 规范的模块代码 打包编译之后的代码
const cache = {};
(function(modules){
const require = (path)=>{
if(cache[path]) return cache[path]
let module = cache[path] = {
exports:{},
name:path,
}
modules[path](module,module.exports,require)
return module.exports
}
return require('index.js')
})({
'a.js':function(module,exports,require){
console.log(`a load`)
exports.run = function(){
console.log(`a run`)
}
},
'index.js':function(module,exports,require){
console.log('index load')
let a = require('a.js')
console.log(a)
a.run()
exports.run = function(){
conso