Code Monkey home page Code Monkey logo

webpack-custom-plugin's Introduction

手动编写一个 plugin

Init:

创建 webpack-custom-plugin 目录

初始化 git yarn init -y

配置 package.json

"scripts": {
  "build": "webpack --mode development"
},

添加依赖 yarn add webpack webpack-cli clean-webpack-plugin html-webpack-plugin -D

根目录创建 webpack.config.js

const path = require("path");
const resolve = (file) => path.resolve(__dirname, file);
const HtmlWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "[name].bundle.js",
    path: path.resolve(__dirname, "dist")
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: "custom-plugin"
    }),
  ]
};


运行 yarn build 即可打包项目,初始化完成。

编写 plugin

根目录创建 plugins文件夹,添加 file-list-plugin.js

class FileListPlugin {
  constructor(options) {
    this.options = options;
    this.filename = this.options.filename || "fileList.md";
  }
  apply(compiler) {
    compiler.hooks.emit.tapAsync("FileListPlugin", (compilation, cb) => {
      // 2.
      const fileListName = this.filename;
      // 3.
      let len = Object.keys(compilation.assets).length;
      // 4.
      let content = `# 一共有${len}个文件\n\n`;
      // 5.
      for (let filename in compilation.assets) {
        content += `- ${filename}\n`;
      }
      // 6.
      compilation.assets[fileListName] = {
        // 7.
        source: function () {
          return content;
        },
        // 8.
        size: function () {
          return content.length;
        }
      };
      // 9.
      cb();
    });
  }
}

module.exports = FileListPlugin;



配置 webpack.config.js plugins

module: {
    plugins: [
    new HtmlWebpackPlugin({
      title: "custom-plugin"
    }),
    new CleanWebpackPlugin(),
    new FileListPlugin({
      filename: "fileList.md"
    })
  ]
},

运行 yarn build 查看处理后的结果

webpack-custom-plugin's People

Contributors

13168335674 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.