解讀webpack的bundle.js

來源:互聯網
上載者:User

標籤:edm   rip   技術   script   運算式   tor   列印   標識   讀取   

      可能就是好奇心略重了,讀了一下webpack打包後的bundle.js的代碼,複雜的模組可能讀不懂,但簡單的hello world模組我還是能看懂的。沒什麼目的,就是想通過幾個簡單的模組,一條簡單的webpack命令,一個神奇的bundle.js代碼來瞭解webpack是怎麼把遵循commonJs規範的模組應用到瀏覽器端的。

幾個簡單的模組:

一條簡單的webpack命令:

一個神奇的bundle.js:

 1 /******/ (function(modules) { // webpackBootstrap 2 /******/     // The module cache 3 /******/     var installedModules = {}; 4  5 /******/     // The require function 6 /******/     function __webpack_require__(moduleId) { 7  8 /******/         // Check if module is in cache 9 /******/         if(installedModules[moduleId])10 /******/             return installedModules[moduleId].exports;11 12 /******/         // Create a new module (and put it into the cache)13 /******/         var module = installedModules[moduleId] = {14 /******/             exports: {},15 /******/             id: moduleId,16 /******/             loaded: false17 /******/         };18 19 /******/         // Execute the module function20 /******/         modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);21 22 /******/         // Flag the module as loaded23 /******/         module.loaded = true;24 25 /******/         // Return the exports of the module26 /******/         return module.exports;27 /******/     }28 29 30 /******/     // expose the modules object (__webpack_modules__)31 /******/     __webpack_require__.m = modules;32 33 /******/     // expose the module cache34 /******/     __webpack_require__.c = installedModules;35 36 /******/     // __webpack_public_path__37 /******/     __webpack_require__.p = "";38 39 /******/     // Load entry module and return exports40 /******/     return __webpack_require__(0);41 /******/ })42 /************************************************************************/43 /******/ ([44 /* 0 */45 /***/ function(module, exports, __webpack_require__) {46 47     /*48       列印文本的index模組49      */50     var text = __webpack_require__(1);51     console.log(text);52 53 /***/ },54 /* 1 */55 /***/ function(module, exports) {56 57     /*58       產生文本的Hello world模組59      */60     module.exports = ‘Hello world!‘;61 62 /***/ }63 /******/ ]);
View Code

注釋太多有點懵逼,可是細看也不就是一個立即執行的函數運算式嘛(IIFE),這是JavaScript中常見的獨立範圍的方法。這裡我將注釋簡化一下:

 1 (function(modules){ 2     //module緩衝對象 3     var installedModules = {}; 4     //require函數 5     function __webpack_require__(moduleId){ 6         //檢查module是否在cache中 7         if(installedModules[moduleId]){ 8             return installedModules[moduleId].exports; 9         }10         //若不在cache中則建立module並放入cache中11         var module = installedModules[moduleId] = {12             exports: {},13             id: moduleId,14             loaded: false15         };16         //執行module函數17         modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);18         //標記module已經載入19         module.loaded = true;20         //返回module的匯出模組21         return module.exports;22     }23 24     //暴露modules對象(__webpack_modules__)25     __webpack_require__.m = modules;26     //暴露modules緩衝27     __webpack_require__.c = installedModules;28     //設定webpack公用路徑__webpack_public_path__29     __webpack_require__.p = "";30     //讀取入口模組並且返回exports匯出31     return __webpack_require__(0);32     33 })([function(module, exports, __webpack_require__){ /*模組Id為0*/34     var text = __webpack_require__(1);35     console.log(text);36 },function(module, exports){ /*模組Id為1*/37     module.exports = ‘Hello world‘;38 }]);

這個IIFE接收一個數組作為參數modules,數組的每一項都是一個匿名函數代表一個模組(index.js和hello.js模組)。可是為什麼構建命令中只出現了“index.js"呢,這是因為webpack通過靜態分析index.js檔案得到文法樹,遞迴檢測index.js所依賴的模組,以及依賴的依賴,入口模組和所有的相依模組作為數組中的項,併合併到最終的代碼中。

正是由於模組代碼被封裝成函數後,每個模組的運行時機變的可控,可以決定何時調用(通過 modules[moduleId].call ),而且也有了獨立的範圍,定義變數,聲明函數都不會汙染全域範圍。模組函數的參數列表中除了commonJS規範所要求的 module 與 exports 外,還有 __webpack_require__ 函數,用來替換 require ,作用是聲明對其他模組的依賴並獲得該模組的 exports ( return installedModules[moduleId].exports 和 return module.exports; )。而且不需要提供模組的相對路徑或其他形式的ID,直接傳入該模組在modules列表中索引即可,這樣可以省掉模組標識符的解析過程(準確說,webpack是把require模組的解析過程提前到了構建期),從而可以獲得更好的運行效能。

程式解讀:
bundle.js通過 __webpack_require__(0); 啟動整個程式,先檢查模組ID = 0是否在緩衝對象中,若該模組的緩衝存在返回 module.exports 即模組所暴露出來的資料,若該模組的緩衝不在則新建立module對象(該module對象作用是用來指向真實模組)並加入到緩衝對象中,此時由於module對象和該模組的緩衝對象 installedModules[moduleId] 的exports屬性為沒有資料,所以需要通過執行該模組函數來返回具體require其他模組的資料,傳入的內容物件是 module.exports 和 installedModules[moduleId].exports 所共同指向的一個對象。當程式執行到 var text = __webpack_require__(1); 時,又會執行 modules[1].call ,然後 module.exports = ‘Hello world‘; 將執行 __webpack_require__(1) 時建立的module1的exports賦值為Hello world,並返回,此時 __webpack_require__(1) 執行完畢,text為Hello world並列印, __webpack_require__(0) 執行完畢。這是一個遞迴的過程,如果還有更多相依模組的話會更明顯。

總結一下,webpack主要做了兩部分工作:
1.分析得到所有必須模組併合並。
2.提供讓這些模組有序,正常的執行環境。

參考:
《React全棧 Redux+Flux+webpack+Babel整合開發》

解讀webpack的bundle.js

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.