plugins 插件

plugin 可以做很多事情 plugin 永远的神
在特定的时间做特定的事情

clean-webpack-plugin

作用:每次webpack编译之前删除dist里面的文件
image.png

html-webpack-plugin

  1. 动态的给dist中创建html,默认引入main.js
  2. 添加配置项 image.png
  3. image.pngimage.png

    DefinePlugin

    是webpack 内置的 插件
    获取方式 : const { DefinePlugin } = require(‘webpack’);

使用:
image.png
image.png

ReactRefreshWebpackPlugin

const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");

配合 react-refresh 实现 HMR 热更新
image.png

其他插件

terser-webpack-plugin (terser 简洁的)

  1. const TerserPlugin = require('terser-webpack-plugin');
  2. module.exports = {
  3. optimization: {
  4. minimizer: [
  5. new TerserPlugin({
  6. cache: true,
  7. parallel: true,
  8. sourceMap: true, // 如果在生产中使用源映射,必须设置为true
  9. terserOptions: {
  10. // https://github.com/webpack-contrib/terser-webpack-plugin#terseroptions
  11. }
  12. }),
  13. ],
  14. }
  15. };