记录使用webpack5压缩css,js遇到的问题

700 阅读1分钟

一般来说在webpack的mode设置为production的时候,会自动压缩js代码,但是,在webpack5中配置压缩css代码之后,即使mode为production,也不会压缩js。webpack.config.js文件内容如下:

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const ESLintPlugin = require("eslint-webpack-plugin");

process.env.NODE_ENV = "production";
module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "main.js",
    path: path.resolve(__dirname, "dist"),
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: [
              [
                "@babel/preset-env",
                {
                  targets: "ie 11",
                  useBuiltIns: "usage",
                  corejs: { version: 3 },
                },
              ],
            ],
          },
        },
      },
      {
        test: /\.css$/i,
        use: [
          // 提取css插件的loader,不能使用style-loader
          // compiles Less to CSS
          MiniCssExtractPlugin.loader,
          "css-loader",
          {
            loader: "postcss-loader",
            options: {
              postcssOptions: {
                plugins: [["postcss-preset-env"]],
              },
            },
          },
        ],
      },
      {
        test: /\.less$/i,
        use: [
          // compiles Less to CSS
          MiniCssExtractPlugin.loader,
          "css-loader",
          "less-loader",
        ],
      },
      {
        test: /\.(png|jpg|gif)$/i,
        use: [
          {
            loader: "url-loader",
            options: {
              limit: 8191,
              name: "[hash:10].[ext]",
              outputPath: "images",
              esModule: false,
            },
          },
        ],
        type: "javascript/auto",
      },
      {
        test: /\.html$/i,
        use: "html-loader",
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        use: [
          {
            loader: "file-loader",
            options: {
              outputPath: "fonts",
              esModule: false,
            },
          },
        ],
        type: "javascript/auto",
      },
    ],
  },
  optimization: {
    minimizer: [
      // css压缩,
      new CssMinimizerPlugin(),
    ],
    minimize: true,
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html",
      minify: {
        collapseWhitespace: true,
        removeComments: true,
      },
    }),
    //提取css为单独的文件
    new MiniCssExtractPlugin({
      filename: "css/main.css",
    }),
    new ESLintPlugin({
      fix: true,
    }),
  ],
  mode: "production",
  devServer: {
    static: path.resolve(__dirname, "dist"),
    compress: true,
    port: 3000,
    open: true,
    hot: true,
  },
};

webpack5中压缩文件的配置在optimization中,后面查阅文档发现这样配置是代表你使用第三方plugin来压缩代码 所以要额外加上压缩js的plugin。修改后如下:

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
const ESLintPlugin = require("eslint-webpack-plugin");
const TerserPlugin = require("terser-webpack-plugin");

process.env.NODE_ENV = "production";
module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "main.js",
    path: path.resolve(__dirname, "dist"),
  },
  module: {
    rules: [
      {
        test: /\.m?js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: [
              [
                "@babel/preset-env",
                {
                  targets: "ie 11",
                  useBuiltIns: "usage",
                  corejs: { version: 3 },
                },
              ],
            ],
          },
        },
      },
      {
        test: /\.css$/i,
        use: [
          // 提取css插件的loader,不能使用style-loader
          // compiles Less to CSS
          MiniCssExtractPlugin.loader,
          "css-loader",
          {
            loader: "postcss-loader",
            options: {
              postcssOptions: {
                plugins: [["postcss-preset-env"]],
              },
            },
          },
        ],
      },
      {
        test: /\.less$/i,
        use: [
          // compiles Less to CSS
          MiniCssExtractPlugin.loader,
          "css-loader",
          "less-loader",
        ],
      },
      {
        test: /\.(png|jpg|gif)$/i,
        use: [
          {
            loader: "url-loader",
            options: {
              limit: 8191,
              name: "[hash:10].[ext]",
              outputPath: "images",
              esModule: false,
            },
          },
        ],
        type: "javascript/auto",
      },
      {
        test: /\.html$/i,
        use: "html-loader",
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        use: [
          {
            loader: "file-loader",
            options: {
              outputPath: "fonts",
              esModule: false,
            },
          },
        ],
        type: "javascript/auto",
      },
    ],
  },
  optimization: {
    minimizer: [
      // css压缩,
      new CssMinimizerPlugin(),
      new TerserPlugin(),
    ],
    minimize: true,
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html",
      minify: {
        collapseWhitespace: true,
        removeComments: true,
      },
    }),
    //提取css为单独的文件
    new MiniCssExtractPlugin({
      filename: "css/main.css",
    }),
    new ESLintPlugin({
      fix: true,
    }),
  ],
  mode: "production",
  devServer: {
    static: path.resolve(__dirname, "dist"),
    compress: true,
    port: 3000,
    open: true,
    hot: true,
  },
};