Webpack4 - CSS 从 less 文件导入的文件没有被缩小

Webpack4 - CSS files imported from less files aren't being minified

我正在用 webpack 4 构建一个项目。几乎所有项目都可以正常工作,我只是在缩小 CSS.

时遇到了问题

如果我在入口点包含 a.css 或 .less 文件,那么一切正常。如果我使用 @include './myfile.less',它也能正常工作。但是,如果我使用 @include './myfile.css',该部分将在未缩小的情况下导入。

如何使导入的 CSS 文件起作用?

webpack.config

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin'); //installed via npm
const webpack = require('webpack'); //to access built-in plugins
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {

    entry: {
        'app': './src/app/app.js',
    },

    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].bundle.js'
    },

    module: {
        rules: [
            {
                test: /\.html/,
                use: 'html-loader'
            },
            {
                test: /\.less$/,
                use: [
                    {
                        loader: MiniCssExtractPlugin.loader,
                    },
                    "css-loader",
                    "less-loader"
                ]
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                use: ['file-loader?name=assets/images/[path][name].[ext]?[hash]']
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                use: [
                    'file-loader?name=assets/fonts/[name].[ext]?[hash]'
                ]
            }
        ],
    },

    plugins: [
        new HtmlWebpackPlugin({ template: './src/public/index.html' }),
        new MiniCssExtractPlugin({
            // Options similar to the same options in webpackOptions.output
            // both options are optional
            filename: "[name].bundle.css",
            chunkFilename: "[id].css"
        }),
    ]

};

我找到了笔记 on MiniCssExtractPlugin production usage

以下解决了问题:

具体添加:

const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
...
module.exports = {
    ... 
    optimization: {
        minimizer: [
            new UglifyJsPlugin({
                cache: true,
                parallel: true,
                sourceMap: true // set to true if you want JS source maps
            }),
            new OptimizeCSSAssetsPlugin({})
        ]
    },
}

虽然 webpack 5 可能会内置 CSS 最小化器,但对于 webpack 4,您需要自带。要缩小输出,请使用像 optimize-css-assets-webpack-plugin 这样的插件。设置 optimization.minimizer 会覆盖 webpack 提供的默认值,因此请确保还指定了一个 JS 最小化器

webpack.config.js

const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
module.exports = {
  optimization: {
    minimizer: [
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        sourceMap: true // set to true if you want JS source maps
      }),
      new OptimizeCSSAssetsPlugin({})
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: "[name].css",
      chunkFilename: "[id].css"
    })
  ],
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          "css-loader"
        ]
      }
    ]
  }
}

Reference

use: [
    MiniCssExtractPlugin.loader,
    {
        loader: "css-loader",
        options: {
            minimize: true
        }
    }
]