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

3

当查看由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();


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

为什么你说这不是IIFE?试着在浏览器控制台中输入(function(a) { console.log(a); })(['1', '2', '3']);,你会发现它可以正常工作 - 这并不是魔法。 - chazsolo
实际上这是一个IIFE,该函数使用函数数组进行调用。 - Gerben
1个回答

5
Webpack确实会将所有内容都包装在IIFE中,并按以下步骤运行该函数:
  1. webpack将所有模块(作为数组)封装在IIFE中并调用它。
  2. webpack创建一个__webpack_require__函数,在需要时内部调用模块。
  3. webpack通过使用__webpack_require__函数来运行第一个模块(即入口模块)启动应用程序。
  4. 所有先前的require语句都被替换为使用传入数组中正确的模块索引的__webpack_require__调用。
以上4点来自我撰写的博客文章,您可以点击此处查看更多信息。

根据我的理解,一旦Webpack输出了"bundle.js"文件,Webpack的工作就完成了。然后我们在HTML页面中链接bundle.js。因此,仅仅从bundle.js中的代码来看,似乎没有立即调用函数表达式(IIFE)。所以我想知道为什么(function(modules) { // webpackBootstrap...会首先被执行。 - rosnk
1
编译后的 bundle.js 文件中的代码是一个立即调用函数表达式(IIFE):(function(modules) {})([function() {}, function() {}]) - Nick Salloum
我在那段代码中错过了IIFE,当你告诉我它是IIFE时,我才意识到数组被作为参数传递。谢谢,感激不尽。 - rosnk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接