Error in using the webpack mini-css-extract-plugin plugin

user8891963 picture user8891963 · Sep 29, 2018 · Viewed 12.2k times · Source

When I run the npm run build fails, referencing the mini-css-extract-plugin:

C:\dev\udemy-restfull\webpack\node_modules\mini-css-extract-plugin\dist\index.js:76
    const resource = this._identifier.split('!').pop();
                                         ^
TypeError: Cannot read property 'split' of undefined

I tried to search for the error, but only understood that it depends on the order of the loaders execution, so I left only the MiniCssExtractPlugin.loader and the css-loader, but the error continued.

I went through the documentation and got the simplified settings, also the same error occurred.

I have loaded the bootstrap in index.js so I removed it thinking that it could be the cause, also not good.

can you help me?

Here's my webpack.config.js file:

const path = require('path');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  entry: ["@babel/polyfill", "./src/index.js"],
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'style.css'
    })
  ],
  module: {
    rules: [

      {
        test: /\.css$/,
        use: [
          { loader: MiniCssExtractPlugin.loader }, // style-loader
          { loader: "file-loader" }
        ]
      },
      {
        test: /\.(scss)$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader, // inject CSS to page / style-loader
          },
          {
            loader: 'css-loader', // translates CSS into CommonJS modules
          },
          {
            loader: 'postcss-loader', // Run post css actions
            options: {
              plugins: function () { // post css plugins, can be exported to postcss.config.js
                return [
                  require('precss'),
                  require('autoprefixer')
                ];
              }
            }
          },
          {
            loader: 'sass-loader' // compiles Sass to CSS
          }]
      },
    ]
  },
  devServer: {
    contentBase: './dist',
    port: 9000
  }
};

Here's my package.json file:

{
  "name": "app-webpack",
  "version": "1.0.0",
  "description": "teste com webpack",
  "main": "index.js",
  "scripts": {
    "dev": "webpack --mode development --watch",
    "build": "webpack --mode production",
    "start": "webpack-dev-server --mode development"
  },
  "author": "Johnatan Lopes",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.1.0",
    "@babel/core": "^7.1.0",
    "@babel/preset-env": "^7.1.0",
    "autoprefixer": "^9.1.5",
    "babel-loader": "^8.0.4",
    "css-loader": "^1.0.0",
    "file-loader": "^2.0.0",
    "mini-css-extract-plugin": "^0.4.3",
    "node-sass": "^4.9.3",
    "postcss-loader": "^3.0.0",
    "precss": "^3.1.2",
    "sass-loader": "^7.1.0",
    "style-loader": "^0.23.0",
    "webpack": "^4.20.2",
    "webpack-cli": "^3.1.1",
    "webpack-dev-server": "^3.1.9"
  },
  "dependencies": {
    "@babel/polyfill": "^7.0.0",
    "bootstrap": "^4.1.3",
    "jquery": "^3.3.1",
    "popper.js": "^1.14.4"
  }
}

Answer

Adam picture Adam · Sep 30, 2018

mini-css-extract-plugin's loader only takes the output of css-loader as its input. Your rule for CSS files should look like this:

{
  test: /\.css$/,
  use: [
    MiniCssExtractPlugin.loader,
    "css-loader"
  ]
}

This way, the properly formatted CSS will be passed to MiniCssExtractPlugin.loader for the plugin to extract it from your bundle.