去搞清楚webpack作了什麼以前,我以爲首先要思考一下咱們爲何須要webpack,它究竟解決了什麼痛點。想一想咱們平常搬磚的場景:
1.開發的時候須要一個開發環境,要是咱們修改一下代碼保存以後瀏覽器就自動展示最新的代碼那就行了(熱更新服務)
2.本地寫代碼的時候,要是調後端的接口不跨域就行了(代理服務)
3.爲了跟上時代,要是能用上什麼ES678N等等新東西就行了(翻譯服務)
4.項目要上線了,要是能一鍵壓縮代碼啊圖片什麼的就行了(壓縮打包服務)
5.咱們平時的靜態資源都是放到CDN上的,要是能自動幫我把這些搞好的靜態資源懟到CDN去就行了(自動上傳服務)javascript
- 若是與輸入相關的需求,找entry(好比多頁面就有多個入口)
- 若是與輸出相關的需求,找output(好比你須要定義輸出文件的路徑、名字等等)
- 若是與模塊尋址相關的需求,找resolve(好比定義別名alias)
- 若是與轉譯相關的需求,找loader(好比處理sass處理es678N)
- 若是與構建流程相關的需求,找plugin(好比我須要在打包完成後,將打包好的文件複製到某個目錄,而後提交到git上)
webpack打包出來的什麼
webpack搞了不少東西,但最終產出的無非就是通過重重服務處理過的代碼,那麼這些代碼是怎樣的呢?
首先咱們先來看看入口文件index.js:java
console.log('index') const one = require('./module/one.js') const two = require('./module/two.js') one() two()
嗯,很簡單,沒什麼特別,引入了兩個模塊,最後執行了它們一下。其中one.js和two.js的代碼也很簡單,就是導出了個函數:node
// one.js module.exports = function () { console.log('one') }
// two.js module.exports = function () { console.log('two') }
好了,就是這麼簡單的代碼,放到webpack打包出來的是什麼呢?webpack
/******/ (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; /******/ /******/ // 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); /******/ }) /************************************************************************/ /