Hope0922 +
hopexie0922@.163.com

webpack 插件的基本原理

webpack 插件的基本原理

基本用法

const HtmlWebpackPlugin = require("html-webpack-plugin");
module.exports = {
  plugins: [
    new HtmlWebpackPlugin({
      template: "./public/index.html", // 模版文件
    }),
  ],
};

如上是一个 webpack 插件的基本用法,那么他的原理是什么呢

基本原理

1. 调用 webpack 函数,接收 config 配置信息,实例化 compiler,初始化内置插件(调用所有内置插件的.apply)

2. 调用 compiler.run 进入编译阶段

webpack 插件规范

基本插件架构及使用方法 ↓

class HelloWorldPlugin {
  apply(compiler) {
    compiler.hooks.done.tap(
      "Hello World Plugin",
      (stats /* 绑定 done 钩子后,stats 会作为参数传入。 */) => {
        console.log("Hello World!");
      }
    );
  }
}

module.exports = HelloWorldPlugin;
// webpack.config.js
var HelloWorldPlugin = require("hello-world");

module.exports = {
  // ... 这里是其他配置 ...
  plugins: [new HelloWorldPlugin({ options: true })],
};
/*
 * @Descripttion: 插件测试
 * @Author: xietianfeng
 * @Date: 2022-01-04 14:35:05
 * @LastEditors: xietianfeng
 * @LastEditTime: 2022-01-04 16:08:40
 */

// 插件输出类
class RemoveCommentPlugin {
  constructor(opts) {
    this.opts = opts;
    this.externalModules = {};
  }
  apply(compiler) {
    // 去除注释正则
    const reg =
      /("([^\\\"]*(\\.)?)*")|('([^\\\']*(\\.)?)*')|(\/{2,}.*?(\r|\n))|(\/\*(\n|.)*?\*\/)|(\/\*\*\*\*\*\*\/)/g;

    // 注册自定义插件钩子到生成资源到 output 目录之前,拿到compilation对象(编译好的stream)
    compiler.hooks.emit.tap("RemoveComment", (compilation) => {
      // 遍历构建产物
      Object.keys(compilation.assets).forEach((item) => {
        // .source()是获取构建产物的文本
        // .assets中包含构建产物的文件名
        let content = compilation.assets[item].source();
        content = content.replace(reg, function (word) {
          // 去除注释后的文本
          return /^\/{2,}/.test(word) ||
            /^\/\*!/.test(word) ||
            /^\/\*{3,}\//.test(word)
            ? ""
            : word;
        });
        // console.info(content);
        // 更新构建产物对象
        compilation.assets[item] = {
          source: () => content,
          size: () => content.length,
        };
      });
    });
  }
}

module.exports = RemoveCommentPlugin;

Blog

About Me