Webpack erroring on mini-css-extract-plugin loader
Asked Answered
T

1

7

When I try to use the loader for mini-css-extract-plugin webpack returns the following error:

/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

I am requiring the plugin and invoking the loader in my prod config file:

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
        ],
      },
    ],
  },
});

I have it included in my devDependencies:

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

But still I receive the error. I haven't found anything in the [documentation][1] to indicate what could be happening. Is there something I'm overlooking with this code?

Why would methods from loader.js be getting flagged as 'not a function?' [1]: https://www.npmjs.com/package/mini-css-extract-plugin

Tomb answered 13/2, 2021 at 13:8 Comment(0)
K
13

I understand getAssets is only available since webpack 4.40.0 https://webpack.js.org/api/compilation-object/#getassets

You could try:

  1. Update webpack version to at least 4.40.0
  2. Downgrade min-css-extract-plugin to 1.3.0

I tried the second one, and worked for me, but upgrading webpack should work too.

Kofu answered 13/2, 2021 at 19:37 Comment(1)
nice, this worked for me- thanks! I was on webpack 4.26.0. I just recently set up this webpack project, so I suppose I assumed that the latest stable would have automatically installed for me. I am using [email protected]. I updated my webpack version to 4.40.0 and it's working now!Tomb

© 2022 - 2024 — McMap. All rights reserved.