使用webpack的mini-css-extract-plugin插件时出现错误

3
当我运行npm run build失败时,引用了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

我试图搜索错误原因,但只了解到它取决于加载器执行的顺序,因此我仅保留了MiniCssExtractPlugin.loader和css-loader,但错误仍然存在。

我查看了文档并获取了简化的设置,但同样的错误仍然出现。

我已经在index.js中加载了bootstrap,因此我将其移除以认为它是问题所在,但结果并不理想。

你能帮我吗?

这是我的webpack.config.js文件:

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
  }
};

这是我的package.json文件:

{
  "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"
  }
}

尝试作为插件...... new MiniCssExtractPlugin({ filename: "[name].css", chunkFilename: "[id].css" }), ...... - Robert Rowntree
2个回答

8
< p >mini-css-extract-plugin的loader仅将css-loader的输出作为其输入。您的CSS文件规则应如下所示:

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

这样一来,正确格式的CSS将被传递给MiniCssExtractPlugin.loader插件,以便从您的包中提取它。

3
目前这仅适用于您不尝试配置 'css-loader' 的情况。如果您正在尝试使用加载器的 options 属性,则此方法将无法解决您的问题。 - hellatan

2
这是针对错误信息的首个谷歌搜索结果之一,因此即使这个修复方法与原提问者的配置无关,它也可能有助于未来的用户。
对我而言,修复方法是从选项对象(或 options.modules 对象,如果您使用 css-loader v3.x)中移除 exportOnlyLocals/exportLocals(具体取决于您的 css-loader 版本)。如果您正在使用预渲染,则会产生副作用,这是我个人不熟悉的 SSR 问题。
还要确保在 css-loader 之前调用 MiniCssExtractPlugin,就像 Adam 已经提到的那样。

似乎在这里得到了确认:https://github.com/webpack-contrib/mini-css-extract-plugin/issues/205。看起来早期版本的css-loader没有这个问题。另一个相关链接:https://webpack.js.org/loaders/css-loader/#exportonlylocals。 - Ambroise Rabier

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接