javascriptnode.jswebpackmini-css-extract-plugin

Webpack erroring on mini-css-extract-plugin loader


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


Solution

  • 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.