创建webpack-demo
使用webpack@5.89.0
创建webpack-demo项目,文件结构如下:

utils.js
js
export const printHello = () => {
console.log("hello world");
};
const key = "yes";
export default key;
入口文件index.js
js
import key, { printHello } from "./utils";
function component() {
const element = document.createElement("div");
element.innerText = "webpack";
printHello();
console.log("key: ", key);
return element;
}
document.body.appendChild(component());
使用默认webpack
配置,在package.json
添加"build": "webpack --mode=development --devtool=false"
,使用开发模式并关闭devtool
保证产物可读性
运行npm run build
在/dist
目录下生成main.js
文件
js
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "./src/utils.js":
/*!**********************!*\
!*** ./src/utils.js ***!
\**********************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__),
/* harmony export */ printHello: () => (/* binding */ printHello)
/* harmony export */ });
const printHello = () => {
console.log("hello world");
};
const key = "yes";
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (key);
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
/*!**********************!*\
!*** ./src/index.js ***!
\**********************/
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils */ "./src/utils.js");
function component() {
const element = document.createElement("div");
element.innerText = "webpack";
(0,_utils__WEBPACK_IMPORTED_MODULE_0__.printHello)();
console.log("key: ", _utils__WEBPACK_IMPORTED_MODULE_0__["default"]);
return element;
}
document.body.appendChild(component());
})();
/******/ })()
;
对代码进行分析
- 代码最外层是一个立即执行函数
- 定义
__webpack_modules__
对象,key是模块路径,值是模块内容 - 定义
__webpack_require__
函数,并且添加.d
,.r
,.o
三个属性,都是函数 - 执行入口代码
1. __webpack_modules__
__webpack_modules__
是一个对象,key是模块文件相对根目录的路径./src/utils.js
,值是一个函数,内部包含utils.js
模块内容
js
var __webpack_modules__ = {
"./src/utils.js": (
__unused_webpack_module,
__webpack_exports__,
__webpack_require__
) => {
// __unused_webpack_module 没有用到的参数
// __webpack_exports__ 是一个空对象,把 utils.js 模块导出的内容添加到__webpack_exports__里
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
default: () => __WEBPACK_DEFAULT_EXPORT__,
printHello: () => /* binding */ printHello,
});
// utils.js模块代码
const printHello = () => {
console.log("hello world");
};
const key = "yes";
const __WEBPACK_DEFAULT_EXPORT__ = key;
},
};
2. __webpack_require__
__webpack_require__
是一个函数,作用是返回module.exports
对象
js
function __webpack_require__(moduleId) {
// moduleId是模块的路径
// 1. 从缓存对象__webpack_module_cache__取出模块信息,__webpack_module_cache__是一个空对象
var cachedModule = __webpack_module_cache__[moduleId];
if (cachedModule !== undefined) {
return cachedModule.exports;
}
// 2. 创建一个模块,并给__webpack_module_cache__赋值
var module = (__webpack_module_cache__[moduleId] = {
// no module.id needed
// no module.loaded needed
exports: {},
});
// 3. 从__webpack_modules__取出moduleId对应的函数并执行
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
// 4. 返回module.exports
return module.exports;
}
__webpack_require__.o
定义了一个工具方法,作用是检查对象的属性是自身属性还是原型属性
js
__webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
__webpack_require__.r
作用是给exports对象加一个Module
标志,代码内不起作用,可以忽略
js
__webpack_require__.r = (exports) => {
if (typeof Symbol !== "undefined" && Symbol.toStringTag) {
Object.defineProperty(exports, Symbol.toStringTag, {
value: "Module",
});
}
Object.defineProperty(exports, "__esModule", { value: true });
};
__webpack_require__.d
作用是把第二个参数的自身属性转移到exports
对象上
js
__webpack_require__.d = (exports, definition) => {
for (var key in definition) {
if ( __webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
Object.defineProperty(exports, key, {
enumerable: true,
get: definition[key],
});
}
}
};
所以__webpack_modules__
对象内容基本都是下面的形式
js
{
"path to file": (, exports, __webpack_require__) => {
1. 执行 __webpack_require__.d 函数,把模块导出属性添加到 exports 对象上
2. 模块内容
}
}
3. 入口文件的执行
js
var __webpack_exports__ = {};
// 使用立即执行函数,防止内部变量污染
(() => {
// 给__webpack_exports__添加esModule标志
__webpack_require__.r(__webpack_exports__);
// 取出源码中import模块导出的内容
var _utils__WEBPACK_IMPORTED_MODULE_0__ =
__webpack_require__("./src/utils.js");
// 入口文件源码
function component() {
const element = document.createElement("div");
element.innerText = "webpack";
// 使用用模块导出的属性
(0, _utils__WEBPACK_IMPORTED_MODULE_0__.printHello)();
console.log("key: ", _utils__WEBPACK_IMPORTED_MODULE_0__["default"]);
return element;
}
document.body.appendChild(component());
})();
主要是两步工作:
- 取出模块导出的属性
- 执行入口文件源码,使用模块导出的属性
总结
webpack
的打包结果里,实现了一套基于webpack_require
和webpack_exports
的一套模块化规范,通过把源码中的一个个文件作为key + function
的形式存储进webpack_modules
里,同时会把在源码中所有模块化规范 都改成webpak_require
和他自己的exports
,加载的时候就是执行对应的webpack_modules
里的对应key
的函数,这个函数执行完成以后就会把对应的exports
对象里写满值,默认导出就会有一个default
属性,在expoprts
对象上具名导出就是具体的key
在exports
对象上- 由于导入语句使用了
webpack_require
,这个webpack_require
会返回已经填充好值的exports
对象 - 然后在使用的时候 直接是把我们原来使用模式从模块化改成对象
.key
的使用模式