Webpack 5 不输出 LESS 样式?

Webpack 5 not outputting LESS styles?

我正在尝试将我所有的依赖项升级到最新版本,但除 webpack 5 之外的所有内容都没有按预期工作。问题是我的 less 样式根本没有加载。 运行 prod 构建时,dist 文件夹中没有输出 less 文件,localhost 开发页面中也没有样式。我浏览了他们的所有文档,并尽可能地模仿了示例。

节点 14.15.4

webpack.config.js

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
const webpack = require('webpack');
const packageJson = require('./package.json');

const ENV = process.env.NODE_ENV || 'production';
const isDev = process.env.NODE_ENV !== 'production';
console.log('isDev', isDev);

module.exports = {
    entry: {
        [`${packageJson.name}.${packageJson.version}`]: './src/index.js'
    },
    optimization: {
        minimizer: [
            new TerserJSPlugin({
                terserOptions: {
                    output: {
                        comments: false,
                    },
                },
                extractComments: false,
            }),
            new CssMinimizerPlugin({
                minimizerOptions: {
                    preset: [
                        'default',
                        {
                            discardComments: { removeAll: true },
                        },
                    ],
                },
            }),
        ],
        splitChunks: {
            cacheGroups: {
                styles: {
                    idHint: `${packageJson.name}.${packageJson.version}`,
                    type: 'css/mini-extract',
                    chunks: 'all',
                    enforce: true,
                },
            },
        },
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            },
            {
                test: /\.(le|c)ss$/,
                use: [
                    isDev ? 'style-loader' : MiniCssExtractPlugin.loader,
                    'css-loader',
                    'less-loader',
                ],
            }
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: '[name].min.css',
            chunkFilename: '[name].min.css',
        }),
        new HtmlWebpackPlugin({
            template: 'src/index.html',
            minify: {
                removeComments: true,
                collapseWhitespace: true,
                removeRedundantAttributes: true,
                useShortDoctype: true,
                removeEmptyAttributes: true,
                removeStyleLinkTypeAttributes: true,
                keepClosingSlash: true,
                minifyJS: true,
                minifyCSS: true,
                minifyURLs: true,
            },
        }),
        new webpack.DefinePlugin({
            // PRODUCTION: JSON.stringify(ENV) === 'production',
            'process.env.NODE_ENV': JSON.stringify(ENV)
        }),
        new webpack.IgnorePlugin({ resourceRegExp: /^\.\/locale$/ })
    ],
    output: {
        filename: '[name].min.js',
    },
    devServer: {
        port: 3000
    }
};

package.json

{
  "scripts": {
    "build": "NODE_ENV=production webpack --mode=production",
    "dev": "clear && webpack serve --open --mode=development",
  },
  "devDependencies": {
    "@babel/cli": "^7.12.13",
    "@babel/core": "^7.12.13",
    "@babel/plugin-proposal-class-properties": "^7.12.13",
    "@babel/plugin-proposal-object-rest-spread": "^7.12.13",
    "@babel/plugin-proposal-optional-chaining": "^7.12.13",
    "@babel/plugin-transform-runtime": "^7.12.15",
    "@babel/preset-env": "^7.12.13",
    "@babel/preset-flow": "^7.12.13",
    "@babel/preset-react": "^7.12.13",
    "babel-loader": "8.2.2",
    "babelify": "10.0.0",
    "css-loader": "5.0.2",
    "css-minimizer-webpack-plugin": "^1.2.0",
    "html-webpack-plugin": "5.0.0",
    "less": "4.1.1",
    "less-loader": "^8.0.0",
    "mini-css-extract-plugin": "^1.3.6",
    "style-loader": "2.0.0",
    "terser-webpack-plugin": "^5.1.1",
    "webpack": "^5.21.2",
    "webpack-cli": "4.5.0",
    "webpack-dev-server": "^3.11.2"
  },
  "dependencies": {
    "react": "^17.0.1",
    "react-dom": "^17.0.1"
  },
}

App.js

import React from 'react';
import './App.less';

const App = () => (
  <div className="App">
    ...
  </div>
);

export default App;

我需要从 package.json 中删除 "sideEffects": false。查看 github 话题 here