webpack打包之有依赖js模块

时间:2023-03-10 07:14:41
webpack打包之有依赖js模块

一、入口文件main.js

var isd = require('./depend.js');
if(isd.isDepend){
    console.log('有依赖模块');
} else {
    console.log('没有依赖模块');
}

二、依赖模块depend.js

var isDepend = true;
module.exports = {
    isDepend: isDepend
}

三、出口文件bundle.js

1、浏览代码

2、本质

// 实际就是一个自执行函数
(function(modules) {
    //...
 })([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

var isd = __webpack_require__(1);
if(isd.isDepend){
    console.log('有依赖模块');
} else {
    console.log('没有依赖模块');
}

/***/ }),
/* 1 */
/***/ (function(module, exports) {

var isDepend = true;
module.exports = {
    isDepend: isDepend
}

/***/ })
/******/ ])

 四、总结

可以看到出口文件有调用__webpack_require__函数,webpack的作用得以发挥。