试图了解 bundle.js 如何使用 Webpack 执行

Trying to understand how bundle.js executes using Webpack

在查看由 webpack 生成的 bundle.js 时,我可以看到 "webpackBootstrap" 函数包含在括号内。由于它不是 IIFE,所以它里面的 "webpackBootstrap" 函数似乎很神奇 运行。

似乎是其他函数,同样包裹在括号内的是模块数组,这些模块是如何输入第一个函数的,但不清楚它是如何执行的。因此寻求帮助以了解 "bundle.js".

的执行流程
    /******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId]) {
/******/            return installedModules[moduleId].exports;
/******/        }
/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            i: moduleId,
/******/            l: false,
/******/            exports: {}
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.l = true;
/******/
/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }
/******/
/******/
/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;
/******/
/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;
/******/
/******/    // identity function for calling harmony imports with the correct context
/******/    __webpack_require__.i = function(value) { return value; };
/******/
/******/    // define getter function for harmony exports
/******/    __webpack_require__.d = function(exports, name, getter) {
/******/        if(!__webpack_require__.o(exports, name)) {
/******/            Object.defineProperty(exports, name, {
/******/                configurable: false,
/******/                enumerable: true,
/******/                get: getter
/******/            });
/******/        }
/******/    };
/******/
/******/    // getDefaultExport function for compatibility with non-harmony modules
/******/    __webpack_require__.n = function(module) {
/******/        var getter = module && module.__esModule ?
/******/            function getDefault() { return module['default']; } :
/******/            function getModuleExports() { return module; };
/******/        __webpack_require__.d(getter, 'a', getter);
/******/        return getter;
/******/    };
/******/
/******/    // Object.prototype.hasOwnProperty.call
/******/    __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, exports) {



function hello(){
    console.log('hello world');
}


hello();


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

Webpack 确实将所有内容包装在一个 IIFE 中,并且该函数按以下步骤运行:

  1. webpack 将所有内容包装在一个 IIFE 中,它被所有模块调用(作为一个数组)
  2. webpack 创建一个 __webpack_require__ 函数,需要时内部需要模块
  3. webpack 使用 __webpack_require__ 函数 运行 第一个模块(入口模块)启动
  4. 所有以前的 require 语句都被 __webpack_require__ 的调用替换,其中包含传入数组
  5. 中的正确模块索引

以上4点来自我写的一篇博客post,还有you're free to check it out here for more information.