簡體   English   中英

試圖了解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__函數運行第一個模塊(即入口模塊)來__webpack_require__
  4. 所有以前的require語句都被__webpack_require__的調用替換為傳入的數組中正確的模塊索引

以上4點來自我寫的博客文章, 你可以在這里查看更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM