Home  >  Article  >  Web Front-end  >  An in-depth introduction to webpack principles (with examples)

An in-depth introduction to webpack principles (with examples)

不言
不言forward
2019-01-15 10:29:133296browse
This article brings you an in-depth introduction to the principles of webpack (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

This article is copied from "Webpack Explained in a Simple and Simple Language". It is recommended that those who want to learn the principles type it once, operate it once, and explain it to others once, and then they will know it.

I hope you have it before reading. Practical experience related to webpack, otherwise you will not understand it after reading it

It takes a few minutes to read this article, and it takes a long time to understand it by yourself

0 Configuration file

First take a brief look at the webpack configuration file (webpack.config.js):

var path = require('path');
var node_modules = path.resolve(__dirname, 'node_modules');
var pathToReact = path.resolve(node_modules, 'react/dist/react.min.js');

module.exports = {
  // 入口文件,是模块构建的起点,同时每一个入口文件对应最后生成的一个 chunk。
  entry: {
    bundle: [
      'webpack/hot/dev-server',
      'webpack-dev-server/client?http://localhost:8080',
      path.resolve(__dirname, 'app/app.js')
    ]
  },
  // 文件路径指向(可加快打包过程)。
  resolve: {
    alias: {
      'react': pathToReact
    }
  },
  // 生成文件,是模块构建的终点,包括输出文件与输出路径。
  output: {
    path: path.resolve(__dirname, 'build'),
    filename: '[name].js'
  },
  // 这里配置了处理各模块的 loader ,包括 css 预处理 loader ,es6 编译 loader,图片处理 loader。
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel',
        query: {
          presets: ['es2015', 'react']
        }
      }
    ],
    noParse: [pathToReact]
  },
  // webpack 各插件对象,在 webpack 的事件流中执行对应的方法。
  plugins: [
    new webpack.HotModuleReplacementPlugin()
  ]
};

1. Overview of working principle

1.1 Basic concepts

Before understanding the principles of webpack, you need to master the following core concepts

  • Entry: Entry, the first step of webpack construction starts from entry

  • module: module, in webpack a module corresponds to a file. webpack will start from entry and recursively find all dependent modules

  • Chunk: code block, a chunk is composed of multiple modules, used for code merging and splitting

  • Loader: Module converter, used to convert the original content of the module into new content as required

  • Plugin: Expansion plug-in, in the webpack build process Corresponding events will be broadcast at specific times, and plug-ins can monitor the occurrence of these events and do corresponding things at specific times

##1.2 Process Overview

webpack from startup to end Perform the following operations in sequence:

graph TD
初始化参数 --> 开始编译 
开始编译 -->确定入口 
确定入口 --> 编译模块
编译模块 --> 完成编译模块
完成编译模块 --> 输出资源
输出资源 --> 输出完成
The operations performed at each stage are as follows:

  1. Initialization parameters: read from the configuration file (default webpack.config.js) and shell statements Get and merge the parameters to get the final parameters

  2. Start compilation (compile): Initialize the Comiler object with the parameters obtained in the previous step, load all configured plug-ins, and execute the run method of the object Start compiling

  3. Determine the entry: Find all entry files according to the entry in the configuration

  4. Compile module: Starting from the entry file, call All configured Loaders translate the module, then find out the modules that the module depends on, and then recurse this step until all the entry-dependent files have been processed

  5. Complete the compilation module: after the fourth After this step, the final translated content of each module and the dependencies between them are obtained.

  6. Output resources: According to the dependencies between the entry and the modules, they are assembled into individual modules. Contains chunks of multiple modules, and then converts each chunk into a separate file and adds it to the output list. This is the last chance to modify the output content

  7. Output is completed: After confirmation After outputting the content, determine the output path and file name according to the configuration (webpack.config.js && shell), and write the content of the file into the file system (fs)

In the above process , webpack will broadcast specific events at specific points in time, the plug-in listens to the events and executes corresponding logic, and the plug-in can call the api provided by webpack to change the running results of webpack

1.3 Process details

## The #webpack build process can be divided into the following three stages.

    Initialization: start the build, read and merge configuration parameters, load plugin, instantiate Compiler
  1. Compilation: starting from Entry, for each Each Module serially calls the corresponding Loader to translate the content in the file, then finds the Module that the Module depends on, and compiles it recursively.
  2. Output: Combine the compiled Modules into Chunks , Convert the Chunk into a file and output it to the file system
  3. If it is executed only once, the process is as above, but when the listening mode is turned on, the process is as follows
graph TD

  初始化-->编译;
  编译-->输出;
  输出-->文本发生变化
  文本发生变化-->编译

1.3 .1 Initialization phase

The events that will occur during the initialization phase are as follows

EventInitialization parametersInstantiate CompilerLoad the plug-inenvironmentEntry-optionAfter-pluginsAfter-resolvers

# 1.3.2 Compilation phase (event names are all lowercase)
Description
Read and merge parameters from the configuration file and shell statements to obtain the final parameters. This process will also execute the plug-in instantiation statement new in the configuration file. Plugin()
Instantiate Compiler and pass in the parameters obtained in the previous step. Compiler is responsible for file monitoring and starting compilation. The Compiler instance contains the complete webpack configuration, and there is only one Compiler instance globally.
Call the plug-in's apply method in sequence so that the plug-in can monitor all subsequent event nodes. At the same time, pass a reference to the compiler instance into the plug-in to facilitate the plug-in to call webpack's api through the compiler
Start applying the Node.js style file system to the compiler Object to facilitate subsequent file search and reading
Read the configured Entrys and instantiate a corresponding EntryPlugin for each Entry. Prepare for the recursive parsing of the Entry later
Calling the apply method of all built-in and configured plug-ins
Initialize the resolver according to the configuration. The resolver is responsible for finding the file with the specified path in the file system
##Event Explanation
#run Start compilation once
Watch-run Start compilation in monitor mode, the file occurs Changes will recompile
compile Tell the plug-in that a new compilation is about to start, and at the same time bring the compiler object to the plug-in
compilation When webpack is running in development mode, a new compilation is created every time a change to a file is detected. A Compilation object contains the current module resources, compiled resources, changed files, etc. The compilation object also provides a lot of event callbacks for plug-ins to expand
make After a new compilation object is created, the file will be read from entry. According to the file type Compile the file with the compiled loader. After compilation, find the files that the file depends on, compile and parse recursively
after-compile One compilation execution is completed
invalid When an error is encountered, a change event will be triggered. This event will not cause webpack to exit


##The most important event in the compilation phase is compilation, because Loader is called during the compilation phase and each completion ==Conversion== operation of a module. Many small events will occur during the compilation phase, as shown in the following table

EventExplanationbuild-moduleUse the corresponding Loader to convert a moduleNormal-module-loaderAfter using the loader to convert a module Finally, use acorn to parse the converted content and output the corresponding abstract syntax tree (AST) to facilitate webpack to analyze the codeprogramFrom the configured entry The module starts and its AST is analyzed. When it encounters require statements that import other modules, it is added to the list of dependent modules. At the same time, the newly found modules are recursively analyzed to finally figure out the dependencies of all modulessealAll modules and dependent modules are converted through Loader, and Chunks are generated based on dependencies


2.3 Output Phase

Events and explanations that will occur in the output phase:

EventExplanation##should-emitemitafter-mitdonefailed

在输出阶段已经得到了各个模块经过转化后的结果和其依赖关系,并且将相应的模块组合在一起形成一个个chunk.在输出阶段根据chunk的类型,使用对应的模板生成最终要输出的文件内容. |

//以下代码用来包含webpack运行过程中的每个阶段
//file:webpack.config.js

const path = require('path');
//插件监听事件并执行相应的逻辑
class TestPlugin {
  constructor() {
    console.log('@plugin constructor');
  }

  apply(compiler) {
    console.log('@plugin apply');

    compiler.plugin('environment', (options) => {
      console.log('@environment');
    });

    compiler.plugin('after-environment', (options) => {
      console.log('@after-environment');
    });

    compiler.plugin('entry-option', (options) => {
      console.log('@entry-option');
    });

    compiler.plugin('after-plugins', (options) => {
      console.log('@after-plugins');
    });

    compiler.plugin('after-resolvers', (options) => {
      console.log('@after-resolvers');
    });

    compiler.plugin('before-run', (options, callback) => {
      console.log('@before-run');
      callback();
    });

    compiler.plugin('run', (options, callback) => {
      console.log('@run');
      callback();
    });

    compiler.plugin('watch-run', (options, callback) => {
      console.log('@watch-run');
      callback();
    });

    compiler.plugin('normal-module-factory', (options) => {
      console.log('@normal-module-factory');
    });

    compiler.plugin('context-module-factory', (options) => {
      console.log('@context-module-factory');
    });

    compiler.plugin('before-compile', (options, callback) => {
      console.log('@before-compile');
      callback();
    });

    compiler.plugin('compile', (options) => {
      console.log('@compile');
    });

    compiler.plugin('this-compilation', (options) => {
      console.log('@this-compilation');
    });

    compiler.plugin('compilation', (options) => {
      console.log('@compilation');
    });

    compiler.plugin('make', (options, callback) => {
      console.log('@make');
      callback();
    });

    compiler.plugin('compilation', (compilation) => {

      compilation.plugin('build-module', (options) => {
        console.log('@build-module');
      });

      compilation.plugin('normal-module-loader', (options) => {
        console.log('@normal-module-loader');
      });

      compilation.plugin('program', (options, callback) => {
        console.log('@program');
        callback();
      });

      compilation.plugin('seal', (options) => {
        console.log('@seal');
      });
    });

    compiler.plugin('after-compile', (options, callback) => {
      console.log('@after-compile');
      callback();
    });

    compiler.plugin('should-emit', (options) => {
      console.log('@should-emit');
    });

    compiler.plugin('emit', (options, callback) => {
      console.log('@emit');
      callback();
    });

    compiler.plugin('after-emit', (options, callback) => {
      console.log('@after-emit');
      callback();
    });

    compiler.plugin('done', (options) => {
      console.log('@done');
    });

    compiler.plugin('failed', (options, callback) => {
      console.log('@failed');
      callback();
    });

    compiler.plugin('invalid', (options) => {
      console.log('@invalid');
    });

  }
}
#在目录下执行
webpack
#输出以下内容
@plugin constructor
@plugin apply
@environment
@after-environment
@entry-option
@after-plugins
@after-resolvers
@before-run
@run
@normal-module-factory
@context-module-factory
@before-compile
@compile
@this-compilation
@compilation
@make
@build-module
@normal-module-loader
@build-module
@normal-module-loader
@seal
@after-compile
@should-emit
@emit
@after-emit
@done
Hash: 19ef3b418517e78b5286
Version: webpack 3.11.0
Time: 95ms
    Asset     Size  Chunks             Chunk Names
bundle.js  3.03 kB       0  [emitted]  main
   [0] ./main.js 44 bytes {0} [built]
   [1] ./show.js 114 bytes {0} [built]

2 输出文件分析

2.1 举个栗子

下面通过 Webpack 构建一个采用 CommonJS 模块化编写的项目,该项目有个网页会通过 JavaScript 在网页中显示 Hello,Webpack

运行构建前,先把要完成该功能的最基础的 JavaScript 文件和 HTML 建立好,需要如下文件:

页面入口文件 index.html


  <meta>


<p></p>
<!--导入 Webpack 输出的 JavaScript 文件-->
<script></script>

JS 工具函数文件 show.js

// 操作 DOM 元素,把 content 显示到网页上
function show(content) {
  window.document.getElementById('app').innerText = 'Hello,' + content;
}

// 通过 CommonJS 规范导出 show 函数
module.exports = show;

JS 执行入口文件 main.js

// 通过 CommonJS 规范导入 show 函数
const show = require('./show.js');
// 执行 show 函数
show('Webpack');

Webpack 在执行构建时默认会从项目根目录下的 webpack.config.js 文件读取配置,所以你还需要新建它,其内容如下:

const path = require('path');

module.exports = {
  // JavaScript 执行入口文件
  entry: './main.js',
  output: {
    // 把所有依赖的模块合并输出到一个 bundle.js 文件
    filename: 'bundle.js',
    // 输出文件都放到 dist 目录下
    path: path.resolve(__dirname, './dist'),
  }
};

由于 Webpack 构建运行在 Node.js 环境下,所以该文件最后需要通过 CommonJS 规范导出一个描述如何构建的 Object 对象。

|-- index.html
|-- main.js
|-- show.js
|-- webpack.config.js

一切文件就绪,在项目根目录下执行 webpack 命令运行 Webpack 构建,你会发现目录下多出一个 dist目录,里面有个 bundle.js 文件, bundle.js 文件是一个可执行的 JavaScript 文件,它包含页面所依赖的两个模块 main.jsshow.js 及内置的 webpackBootstrap 启动函数。 这时你用浏览器打开 index.html 网页将会看到 Hello,Webpack

2.2 bundle.js文件做了什么

看之前记住:一个模块就是一个文件,

首先看下bundle.js长什么样子:

An in-depth introduction to webpack principles (with examples)

注意:序号1处是个自执行函数,序号2作为自执行函数的参数传入

具体代码如下:(建议把以下代码放入编辑器中查看,最好让index.html执行下,弄清楚执行的顺序)

(function(modules) { // webpackBootstrap
  // 1. 缓存模块
  var installedModules = {};
  // 2. 定义可以在浏览器使用的require函数
  function __webpack_require__(moduleId) {

    // 2.1检查模块是否在缓存里,在的话直接返回
    if(installedModules[moduleId]) {
      return installedModules[moduleId].exports;
    }
    // 2.2 模块不在缓存里,新建一个对象module=installModules[moduleId] {i:moduleId,l:模块是否加载,exports:模块返回值}
    var module = installedModules[moduleId] = {
      i: moduleId,//第一次执行为0
      l: false,
      exports: {}
    };//第一次执行module:{i:0,l:false,exports:{}}
    // 2.3 执行传入的参数中对应id的模块 第一次执行数组中传入的第一个参数
          //modules[0].call({},{i:0,l:false,exports:{}},{},__webpack_require__函数)
    modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
    // 2.4 将这个模块标记为已加载
    module.l = true;
    // 2.5 返回这个模块的导出值
    return module.exports;
  }
  // 3. webpack暴露属性 m c d n o p
  __webpack_require__.m = modules;
  __webpack_require__.c = installedModules;
  __webpack_require__.d = function(exports, name, getter) {
    if(!__webpack_require__.o(exports, name)) {
      Object.defineProperty(exports, name, {
        configurable: false,
        enumerable: true,
        get: getter
      });
    }
  };
  __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;
  };
  __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  __webpack_require__.p = "";
  // 4. 执行reruire函数引入第一个模块(main.js对应的模块)
  return __webpack_require__(__webpack_require__.s = 0);
})
([ // 0. 传入参数,参数是个数组

  /* 第0个参数 main.js对应的文件*/
  (function(module, exports, __webpack_require__) {

    // 通过 CommonJS 规范导入 show 函数
    const show = __webpack_require__(1);//__webpack_require__(1)返回show
    // 执行 show 函数
    show('Webpack');

  }),
  /* 第1个参数 show.js对应的文件 */
  (function(module, exports) {

    // 操作 DOM 元素,把 content 显示到网页上
    function show(content) {
      window.document.getElementById('app').innerText = 'Hello,' + content;
    }
    // 通过 CommonJS 规范导出 show 函数
    module.exports = show;

  })
]);

以上看上去复杂的代码其实是一个自执行函数(文件作为自执行函数的参数),可以简写如下:

(function(modules){
    //模拟require语句
    function __webpack_require__(){}
    //执行存放所有模块数组中的第0个模块(main.js)
    __webpack_require_[0]
})([/*存放所有模块的数组*/])

bundles.js能直接在浏览器中运行的原因是,在输出的文件中通过__webpack_require__函数,定义了一个可以在浏览器中执行的加载函数(加载文件使用ajax实现),来模拟Node.js中的require语句。

原来一个个独立的模块文件被合并到了一个单独的 bundle.js 的原因在于浏览器不能像 Node.js 那样快速地去本地加载一个个模块文件,而必须通过网络请求去加载还未得到的文件。 如果模块数量很多,加载时间会很长,因此把所有模块都存放在了数组中,执行一次网络加载。

修改main.js,改成import引入模块

import show from './show';
show('Webpack');

在目录下执行webpack,会发现:

  1. 生成的代码会有所不同,但是主要的区别是自执行函数的参数不同,也就是2.2代码的第二部分不同

([//自执行函数和上面相同,参数不同
/* 0 */
(function(module, __webpack_exports__, __webpack_require__) {

"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__show__ = __webpack_require__(1);

Object(__WEBPACK_IMPORTED_MODULE_0__show__["a" /* default */])('Webpack');


}),
/* 1 */
(function(module, __webpack_exports__, __webpack_require__) {

"use strict";
/* harmony export (immutable) */ __webpack_exports__["a"] = show;
function show(content) {
  window.document.getElementById('app').innerText = 'Hello,' + content;
}


})
]);

参数不同的原因是es6的import和export模块被webpack编译处理过了,其实作用是一样的,接下来看一下在main.js中异步加载模块时,bundle.js是怎样的

2.3异步加载时,bundle.js代码分析

main.js修改如下

import('./show').then(show=>{
    show('Webpack')
})

构建成功后会生成两个文件

  1. bundle.js  执行入口文件

  2. 0.bundle.js 异步加载文件

其中0.bundle.js文件的内容如下:

webpackJsonp(/*在其他文件中存放的模块的ID*/[0],[//本文件所包含的模块
/* 0 */,
/* 1 show.js对应的模块 */
(function(module, __webpack_exports__, __webpack_require__) {

  "use strict";
  Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
  /* harmony export (immutable) */ 
  __webpack_exports__["default"] = show;

  function show(content) {
    window.document.getElementById('app').innerText = 'Hello,' + content;
  }

})
]);

bundle.js文件的内容如下:

注意:bundle.js比上面的bundle.js的区别在于:

  1. 多了一个__webpack_require__.e,用于加载被分割出去的需要异步加载的chunk对应的文件

  2. 多了一个webpackJsonp函数,用于从异步加载的文件中安装模块

(function(modules) { // webpackBootstrap
    // install a JSONP callback for chunk loading
  var parentJsonpFunction = window["webpackJsonp"];
  // webpackJsonp用于从异步加载的文件中安装模块
  // 将webpackJsonp挂载到全局是为了方便在其他文件中调用
  /**
   * @param chunkIds 异步加载的模块中需要安装的模块对应的id
   * @param moreModules 异步加载的模块中需要安装模块列表
   * @param executeModules 异步加载的模块安装成功后需要执行的模块对应的index
   */
    window["webpackJsonp"] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {
        // add "moreModules" to the modules object,
        // then flag all "chunkIds" as loaded and fire callback
        var moduleId, chunkId, i = 0, resolves = [], result;
        for(;i {
    show('Webpack')
})


/***/ })
]);

All files that need to be output have been generated, ask the plug-in Which files need to be output and which do not need to be output
After determining which files to output, execute file output, == can be obtained and modified here Output content==
File output completed
Completed successfully A complete compilation and output process
If an error occurs during compilation and output, causing webpack to exit, it will jump directly to this step. The plug-in can Get the specific error reason in this event

The above is the detailed content of An in-depth introduction to webpack principles (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete