当我运行npm run构建失败时,请引用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中,因此我以为可能是原因,也将其删除了。
你能帮我吗?
这是我的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"
}
}
答案 0 :(得分:1)
mini-css-extract-plugin
的加载器仅将css-loader
的输出作为输入。您的CSS文件规则应如下所示:
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader"
]
}
通过这种方式,格式正确的CSS将传递到MiniCssExtractPlugin.loader
中,以供插件从您的捆绑包中提取。
答案 1 :(得分:0)
这是此错误消息的首批Google搜索结果之一-因此,即使此修复程序与原始发问者的配置无关,也可能对以后的人们有所帮助。
对我来说,此修复程序是从options对象(或如果使用css-loader v3.x,则是options.modules对象)中删除exportOnlyLocals / exportLocals(取决于您的css-loader版本)。如果您使用的是预渲染,这会产生副作用-我认为这是我个人不熟悉的SSR。
还要确保在css-loader之前调用MiniCssExtractPlugin-正如亚当已经提到的。