摘要:一般會幫我們把所有的文件,,圖片等編譯成一個文件安裝,使用,一般這個文件名為。當(dāng)被加載到瀏覽器這個函數(shù)就會立即執(zhí)行。模塊緩存模塊緩存所有被加載過的模塊都會成為對象的屬性。這一步主要是靠函數(shù)做到的。
webpack一般會幫我們把所有的文件(js,css,圖片等)編譯成一個js文件(webpack安裝,使用),一般這個文件名為bundle.js。我們直接在html文件中用script標(biāo)簽引入就行了,就想下面這樣:
那bundle.js文件如何組織我們的資源文件的呢,我們下面通過一個例子看一下
創(chuàng)建entry.js文件
module.exports = "我是入口js!";
運行下面命令
webpack ./entry.js bundle.js
會生成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) 創(chuàng)建一個新模塊放到緩存中 /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ // Execute the module function 執(zhí)行模塊方法 /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded 標(biāo)記已被加載的模塊 /******/ module.loaded = 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; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports) { module.exports = "我是入口js!"; /***/ } /******/ ]);立即調(diào)用的函數(shù)表達式(Immediately-Invoked Function Expression)
其實bundle.js文件里就是一個立即調(diào)用的函數(shù)表達式(Immediately-Invoked Function Expression)像下面這樣:
(function(/*parameter*/){ /* code */ })(/*argument*/)
這個函數(shù)里定義了一個變量installedModules和一個函數(shù)function __webpack_require__(moduleId)。當(dāng)bundle.js被加載到瀏覽器這個函數(shù)就會立即執(zhí)行。
模塊緩存 installedModules// The module cache 模塊緩存 var installedModules = {};
所有被加載過的模塊都會成為installedModules對象的屬性。這一步主要是靠函數(shù)__webpack_require__做到的。
加載方法 webpack_require// 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) 創(chuàng)建一個新模塊放到緩存中 var module = installedModules[moduleId] = { exports: {}, id: moduleId, loaded: false }; // Execute the module function 執(zhí)行模塊方法 modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); // Flag the module as loaded 標(biāo)記已被加載的模塊 module.loaded = true; // Return the exports of the module 返回模塊的輸出 return module.exports; }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/78294.html
摘要:一般會幫我們把所有的文件,,圖片等編譯成一個文件安裝,使用,一般這個文件名為。當(dāng)被加載到瀏覽器這個函數(shù)就會立即執(zhí)行。模塊緩存模塊緩存所有被加載過的模塊都會成為對象的屬性。這一步主要是靠函數(shù)做到的。 webpack一般會幫我們把所有的文件(js,css,圖片等)編譯成一個js文件(webpack安裝,使用),一般這個文件名為bundle.js。我們直接在html文件中用script標(biāo)簽引...
摘要:一般會幫我們把所有的文件,,圖片等編譯成一個文件安裝,使用,一般這個文件名為。當(dāng)被加載到瀏覽器這個函數(shù)就會立即執(zhí)行。模塊緩存模塊緩存所有被加載過的模塊都會成為對象的屬性。這一步主要是靠函數(shù)做到的。 webpack一般會幫我們把所有的文件(js,css,圖片等)編譯成一個js文件(webpack安裝,使用),一般這個文件名為bundle.js。我們直接在html文件中用script標(biāo)簽引...
閱讀 2185·2023-04-26 00:23
閱讀 861·2021-09-08 09:45
閱讀 2472·2019-08-28 18:20
閱讀 2597·2019-08-26 13:51
閱讀 1629·2019-08-26 10:32
閱讀 1426·2019-08-26 10:24
閱讀 2058·2019-08-26 10:23
閱讀 2230·2019-08-23 18:10