webpack在mini-css-extract-plugin加载器上出错

当我尝试为 mini-css-extract-plugin 使用加载器时,webpack 返回以下错误:

/node_modules/mini-css-extract-plugin/dist/loader.js:122
    for (const asset of compilation.getAssets()) {
                                    ^

    TypeError: compilation.getAssets(...) is not a function or its return value is not iterable

我需要插件并在我的 prod 配置文件中调用加载程序:

const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const path = require("path");
const common = require("./webpack.common");
const merge = require("webpack-merge");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = merge(common, {
  mode: "production",
  output: {
    filename: "[name].[contentHash].bundle.js",
    path: path.resolve(__dirname, "dist")
  },
  plugins: [
    new MiniCssExtractPlugin({filename: "[name].[contentHash].css"}), 
    new CleanWebpackPlugin()
  ],
  module: {
    rules: [
      {
        test: /.scss$/,
        use: [
          MiniCssExtractPlugin.loader, //3. Extract css into files
          "css-loader", //2. Turns css into commonjs
          "sass-loader" //1. Turns sass into css
        ],
      },
    ],
  },
});

我将它包含在我的 devDependencies 中:

"mini-css-extract-plugin": "^1.3.6",

但我仍然收到错误。我没有在 [文档][1] 中找到任何内容来表明可能发生的情况。这段代码有什么我忽略的地方吗?

为什么 loader.js 中的方法会被标记为“不是函数”?[1]:https : //www.npmjs.com/package/mini-css-extract-plugin

回答

我知道 getAssets 仅在 webpack 4.40.0 https://webpack.js.org/api/compilation-object/#getassets 之后可用

你可以试试:

  1. webpack至少更新版本4.40.0
  2. 降级min-css-extract-plugin1.3.0

我尝试了第二个,并为我工作,但升级 webpack 也应该工作。


以上是webpack在mini-css-extract-plugin加载器上出错的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>