• Webpack入门:常用loader和plugin配置


    文档

    本文以 "webpack": "^5.74.0" 为例演示

    文档是个好东西,掌握了基本的用法之后,建议多看看文档。

    环境准备

    node -v
    v16.14.0
    
    # 初始化项目
    npm init -y
    
    # 安装依赖 npm cnpm yarn pnpm 均可
    npm i -D webpack webpack-cli
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    使用默认配置打包

    输入 src/index.js

    console.log('Hello World!')
    
    • 1

    使用默认配置打包

    npx webpack
    
    • 1

    输出 dist/main.js

    console.log("Hello World!");
    
    • 1

    使用配置文件 webpack.config.js

    webpack.config.js

    const path = require("path");
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "main.js",
        path: path.resolve(__dirname, "dist"),
      },
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    css样式文件处理

    依赖

    npm i -D style-loader css-loader
    
    • 1

    webpack.config.js

    const path = require("path");
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "main.js",
        path: path.resolve(__dirname, "dist"),
      },
    
      // 增加以下配置
      module: {
        rules: [
          {
            test: /\.css$/i,
            use: ["style-loader", "css-loader"],
          },
        ],
      },
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    css预处理-less

    依赖

    npm install -D less less-loader
    
    • 1

    webpack.config.js

    const path = require("path");
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "main.js",
        path: path.resolve(__dirname, "dist"),
      },
      module: {
        rules: [
          // 处理css
          {
            test: /\.css$/i,
            use: ["style-loader", "css-loader"],
          },
          // 处理less
          {
            test: /\.less$/i,
            loader: ["style-loader", "css-loader", "less-loader"],
          },
        ],
      },
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    css兼容性处理

    依赖

    npm install -D postcss-loader postcss postcss-preset-env
    
    • 1

    webpack.config.js

    const path = require("path");
    
    // css处理公共loader
    const commonCssLoaders = [
      "style-loader",
      "css-loader",
      {
        loader: "postcss-loader",
        options: {
          postcssOptions: {
            plugins: [["postcss-preset-env"]],
          },
        },
      },
    ];
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "main.js",
        path: path.resolve(__dirname, "dist"),
      },
      module: {
        rules: [
          // 处理css
          {
            test: /\.css$/i,
            use: commonCssLoaders,
          },
          // 处理less
          {
            test: /\.less$/i,
            use: [...commonCssLoaders, "less-loader"],
          },
        ],
      },
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    css代码提取

    依赖

    npm install --save-dev mini-css-extract-plugin
    
    • 1

    webpack.config.js

    const path = require("path");
    const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    
    // css处理公共loader
    const commonCssLoaders = [
      // "style-loader",
      MiniCssExtractPlugin.loader,
      "css-loader",
      {
        loader: "postcss-loader",
        options: {
          postcssOptions: {
            plugins: [["postcss-preset-env"]],
          },
        },
      },
    ];
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "main.js",
        path: path.resolve(__dirname, "dist"),
      },
    
      plugins: [
        // 将 CSS 提取到单独的文件中
        new MiniCssExtractPlugin(),
      ],
    
      module: {
        rules: [
          // 处理css
          {
            test: /\.css$/i,
            use: commonCssLoaders,
          },
          // 处理less
          {
            test: /\.less$/i,
            use: [...commonCssLoaders, "less-loader"],
          },
        ],
      },
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    css代码压缩

    依赖

    npm install css-minimizer-webpack-plugin --save-dev
    
    • 1

    webpack.config.js

    const path = require("path");
    const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
    
    // css处理公共loader
    const commonCssLoaders = [
      // "style-loader",
      MiniCssExtractPlugin.loader,
      "css-loader",
      // css兼容性处理
      {
        loader: "postcss-loader",
        options: {
          postcssOptions: {
            plugins: [["postcss-preset-env"]],
          },
        },
      },
    ];
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "main.js",
        path: path.resolve(__dirname, "dist"),
      },
    
      plugins: [
        // 将 CSS 提取到单独的文件中
        new MiniCssExtractPlugin(),
      ],
    
      module: {
        rules: [
          // 处理css
          {
            test: /\.css$/i,
            use: commonCssLoaders,
          },
          // 处理less
          {
            test: /\.less$/i,
            use: [...commonCssLoaders, "less-loader"],
          },
        ],
      },
    
      optimization: {
        minimizer: [
          // 使用 cssnano 优化和压缩 CSS
          new CssMinimizerPlugin(),
        ],
      },
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    js代码转译 babel

    依赖

    npm install -D babel-loader @babel/core @babel/preset-env
    
    • 1

    webpack.config.js

    const path = require("path");
    
    module.exports = {
      entry: "./src/index.js",
    
      output: {
        filename: "main.js",
        path: path.resolve(__dirname, "dist"),
      },
    
      module: {
        rules: [
          {
            test: /\.m?js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
              loader: "babel-loader",
              options: {
                presets: ["@babel/preset-env"],
              },
            },
          },
        ],
      },
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    js代码压缩

    npm install --save-dev terser-webpack-plugin 
    
    • 1

    webpack.config.js

    const path = require("path");
    const TerserPlugin = require("terser-webpack-plugin");
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "main.js",
        path: path.resolve(__dirname, "dist"),
      },
    
      module: {
        rules: [
          {
            test: /\.m?js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
              loader: "babel-loader",
              options: {
                presets: ["@babel/preset-env"],
              },
            },
          },
        ],
      },
    
      optimization: {
        minimize: true,
        minimizer: [
            // 使用 terser 来压缩 JavaScript
            new TerserPlugin()
        ],
      },
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    html模板

    依赖

    npm install --save-dev html-webpack-plugin
    
    • 1

    webpack.config.js

    const path = require("path");
    const HtmlWebpackPlugin = require("html-webpack-plugin");
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "main.js",
        path: path.resolve(__dirname, "./dist"),
      },
    
      plugins: [
        // 生成一个 HTML5 文件, 引入所有 webpack 生成的 bundle
        new HtmlWebpackPlugin(),
      ],
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    开发服务器

    依赖

    pnpm i -D webpack-dev-server
    
    • 1

    webpack.config.js

    const path = require("path");
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "main.js",
        path: path.resolve(__dirname, "./dist"),
      },
    
      devServer: {
        // 打开浏览器
        open: true,
    
        // 提供静态文件
        static: "./public",
      },
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    运行开发服务器

    npx webpack serve
    
    • 1

    浏览器缓存

    webpack.config.js

    const path = require("path");
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "[name].[contenthash:6].js",
        path: path.resolve(__dirname, "./dist"),
        clean: true, // 在生成文件之前清空 output 目录
      },
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    路径别名

    webpack.config.js

    const path = require("path");
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "[name].[contenthash:6].js",
        path: path.resolve(__dirname, "./dist"),
        clean: true, // 在生成文件之前清空 output 目录
      },
    
      // 设置路径别名
      resolve: {
        alias: {
          "@": path.resolve(__dirname, "src"),
        },
      },
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    打包分析

    依赖

    npm install --save-dev webpack-bundle-analyzer
    
    • 1

    webpack.config.js

    const path = require("path");
    const BundleAnalyzerPlugin =
      require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "[name].[contenthash:6].js",
        path: path.resolve(__dirname, "./dist"),
        clean: true, // 在生成文件之前清空 output 目录
      },
    
      plugins: [new BundleAnalyzerPlugin()],
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    使用CDN

    webpack.config.js

    const path = require("path");
    
    module.exports = {
      entry: "./src/index.js",
      output: {
        filename: "[name].[contenthash:6].js",
        path: path.resolve(__dirname, "./dist"),
        clean: true, // 在生成文件之前清空 output 目录
      },
    
      // 外部加载
      externals: {
        vue: "Vue",
      },
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    输出 manifest

    依赖

    npm install webpack-nano webpack-manifest-plugin --save-dev
    
    • 1

    webpack.config.js

    const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
    
    module.exports = {
      ...
      plugins: [
        new WebpackManifestPlugin()
      ]
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    完整配置

    package.json

    {
      "scripts": {
        "build": "cross-env NODE_ENV=production  webpack --config webpack.config.js",
        "dev": "cross-env NODE_ENV=development webpack serve --config webpack.config.js"
      },
      "devDependencies": {
        "@babel/core": "^7.18.10",
        "@babel/preset-env": "^7.18.10",
        "babel-loader": "^8.2.5",
        "cross-env": "^7.0.3",
        "css-loader": "^6.7.1",
        "css-minimizer-webpack-plugin": "^4.0.0",
        "html-webpack-plugin": "^5.5.0",
        "less": "^4.1.3",
        "less-loader": "^11.0.0",
        "mini-css-extract-plugin": "^2.6.1",
        "postcss": "^8.4.16",
        "postcss-loader": "^7.0.1",
        "postcss-preset-env": "^7.8.0",
        "style-loader": "^3.3.1",
        "terser-webpack-plugin": "^5.3.5",
        "webpack": "^5.74.0",
        "webpack-bundle-analyzer": "^4.6.0",
        "webpack-cli": "^4.10.0",
        "webpack-manifest-plugin": "^5.0.0",
        "webpack-nano": "^1.1.1"
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    webpack.config.js

    const path = require("path");
    const MiniCssExtractPlugin = require("mini-css-extract-plugin");
    const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
    const TerserPlugin = require("terser-webpack-plugin");
    const HtmlWebpackPlugin = require("html-webpack-plugin");
    const BundleAnalyzerPlugin =
      require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
    const { WebpackManifestPlugin } = require("webpack-manifest-plugin");
    
    // css处理公共loader
    const commonCssLoaders = [
      // "style-loader",
      MiniCssExtractPlugin.loader,
      "css-loader",
      // css兼容性处理
      {
        loader: "postcss-loader",
        options: {
          postcssOptions: {
            plugins: [["postcss-preset-env"]],
          },
        },
      },
    ];
    
    module.exports = {
      mode: process.env.NODE_ENV,
    
      entry: "./src/index.js",
    
      output: {
        filename: "[name].[contenthash:6].js",
        path: path.resolve(__dirname, "dist"),
        clean: true, // 在生成文件之前清空 output 目录
      },
    
      devServer: {
        // 打开浏览器
        open: true,
    
        // 提供静态文件
        static: "./public",
      },
    
      // 设置路径别名
      resolve: {
        alias: {
          "@": path.resolve(__dirname, "src"),
        },
      },
    
      // 外部加载
      externals: {
        vue: "Vue",
      },
    
      module: {
        rules: [
          // 处理css
          {
            test: /\.css$/i,
            use: commonCssLoaders,
          },
    
          // 处理less
          {
            test: /\.less$/i,
            use: [...commonCssLoaders, "less-loader"],
          },
    
          // 处理图片资源
          {
            test: /\.(png|svg|jpg|jpeg|gif)$/i,
            type: "asset/resource",
          },
    
          // 处理js
          {
            test: /\.m?js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
              loader: "babel-loader",
              options: {
                presets: ["@babel/preset-env"],
              },
            },
          },
        ],
      },
    
      optimization: {
        minimize: true,
    
        minimizer: [
          // 使用 cssnano 优化和压缩 CSS
          new CssMinimizerPlugin(),
    
          // 使用 terser 来压缩 JavaScript
          new TerserPlugin(),
        ],
      },
    
      plugins: [
        // 生成一个 HTML5 文件, 引入所有 webpack 生成的 bundle
        new HtmlWebpackPlugin(),
    
        // 将 CSS 提取到单独的文件中
        new MiniCssExtractPlugin(),
    
        // 依赖分析
        // new BundleAnalyzerPlugin(),
    
        // manifest
        new WebpackManifestPlugin(),
      ],
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117

    参考
    30 分钟掌握 Webpack

  • 相关阅读:
    【前端必会】不知道webpack插件? webpack插件源码分析BannerPlugin
    Flutter创建工程
    给矩阵添加地理信息
    2023年9月25日,历史上的今天大事件早读
    JSP知识
    python--第六章 python函数 装饰器 && 类 && 对象
    【深入浅出设计模式--命令模式】
    【企业架构】现代企业架构方法——第 1 章
    什么牌子的蓝牙耳机耐用?平价蓝牙耳机里面性价比高的推荐
    汇率之谜:揭秘黄金折算与真实人民币汇率的神秘差距
  • 原文地址:https://blog.csdn.net/mouday/article/details/126453560