【问题标题】:Webpack erroring on mini-css-extract-plugin loadermini-css-extract-plugin 加载器上的 Webpack 错误
【发布时间】:2021-05-17 00:11:04
【问题描述】:

当我尝试使用 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

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

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",

但我仍然收到错误消息。我在 [documentation][1] 中没有找到任何说明可能发生的情况。这段代码有什么我忽略的地方吗?

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

【问题讨论】:

    标签: javascript node.js webpack mini-css-extract-plugin


    【解决方案1】:

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

    你可以试试:

    1. webpack 版本更新到至少4.40.0
    2. min-css-extract-plugin 降级为1.3.0

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

    【讨论】:

    • 很好,这对我有用-谢谢!我在 webpack 4.26.0 上。我最近刚刚建立了这个 webpack 项目,所以我想我认为最新的稳定版会自动为我安装。我正在使用 mini-css-extract-plugin@1.3.6。我将我的 webpack 版本更新到 4.40.0,它现在可以工作了!
    猜你喜欢
    • 2020-07-07
    • 2023-01-05
    • 2019-05-23
    • 2018-08-30
    • 2019-01-29
    • 2019-03-05
    • 2019-04-23
    • 2018-08-26
    • 2023-03-07
    相关资源
    最近更新 更多