阅读理解webpack打包文件步骤执行

84 阅读2分钟

创建 index.html文件,代码如下

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Getting Started</title>
  </head>
  <body>
    <script src="main.js"></script>
  </body>
</html>

创建index.js文件,代码如下

import Say from "./a";

Say();

创建a.js文件,代码如下

export default function HelloSay() {
  console.log("saying console");
}

然后运行 npx webpack --mode development --devtool inline-source-map生成打包文件,代码如下

(() => {
  // webpackBootstrap
  "use strict";
  var __webpack_modules__ = {
    "./src/a.js": (__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
      __webpack_require__.r(__webpack_exports__);
      /* harmony export */ __webpack_require__.d(__webpack_exports__, {
        /* harmony export */ default: () => /* binding */ HelloSay,
        /* harmony export */
      });
      function HelloSay() {
        console.log("saying");
      }
    },
  };
  /************************************************************************/
  // 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 _a__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./a */ "./src/a.js");

    (0, _a__WEBPACK_IMPORTED_MODULE_0__["default"])();
  })();
})();

可以看到main.js的代码里四个立刻执行函数,从第一个开始看

第一个函数往__webpack_require__里追加了一个属性为d的函数,接收两个参数

第二个函数往__webpack_require__里追加了一个属性为o的函数,函数接收 objprop 两个参数,函数作用判断obj 是否有 prop 属性,并返回。

第三个函数往__webpack_require__里追加了一个属性为r的函数,接收一个 exports 函数,然后往 exports 函数里设置属性 __esModule,描述:{value: true, writable: false, enumerable: false, configurable: false}

第四个函数把 ./src/a.js 字符串作为参数传入__webpack_require__函数中,在__webpack_require__函数中再次调用__webpack_modules__["./src/a.js"]函数,然后传入module = {exports: {}}, module.exports = {}, __webpack_require__

__webpack_modules__["./src/a.js"] 中调用__webpack_require__.r(module.exports),此时输出 module = { exports: {__esModule: {value: true, writable: false, enumerable: false, configurable: false}},然后执行__webpack_require__.d函数,传入 module.exoirts{ /* harmony export */ default: () => /* binding */ HelloSay, /* harmony export */ }

再次看__webpack_require__.d函数的执行,遍历 { /* harmony export */ default: () => /* binding */ HelloSay, /* harmony export */ } 对象,如果definition包含keyexports不包含key,则把key写入到exports当中,且值为{ enumerable: true, get: definition[key] } =》 { enumerable: true, get: () => /* binding */ HelloSay }

此时 module.exports = {exports: {__esModule: {value: true, writable: false, enumerable: false, configurable: false}, default: { enumerable: true, get: () => /* binding */ HelloSay }}

最后执行(0, _a__WEBPACK_IMPORTED_MODULE_0__["default"])()=> 即执行a.js=>export default