Uncaught TypeError: Cannot read property 'locals' of undefined
Uncaught TypeError: Cannot read property 'locals' of undefined
我已将 bundle.js 和 bundle.css 用于我的网络应用程序,
我的 package.json 看起来像这样:
"dependencies": {
"bootstrap": "^4.5.3",
"jquery": "^3.5.1",
"popper.js": "^1.16.1"
},
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/plugin-proposal-object-rest-spread": "^7.12.1",
"@babel/preset-env": "^7.12.11",
"babel-loader": "^8.2.2",
"css-loader": "^5.0.1",
"file-loader": "^6.2.0",
"mini-css-extract-plugin": "^1.3.3",
"node-sass": "^5.0.0",
"sass-loader": "^10.1.0",
"style-loader": "^2.0.0",
"url-loader": "^4.1.1",
"webpack": "^5.11.0",
"webpack-cli": "^4.2.0"
},
我的webpack.config.js:
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = (env, argv) => {
return {
//Define entry point
entry: ['./src/index.js', './src/css/index.scss'],
//Define output point
output: {
path: path.resolve(__dirname, 'wwwroot/dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.s[c|a]ss$/,
include: path.resolve(__dirname, 'src'),
use: [
'style-loader',
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
},
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
loader: 'babel-loader',
options: {
presets: ["@babel/preset-env"],
plugins: ['@babel/plugin-proposal-object-rest-spread']
}
},
]
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
}),
new MiniCssExtractPlugin({
filename: 'bundle.css'
})
]
};
};
我的src结构是这样的:
src
index.js:
import 'jquery';
import 'bootstrap';
import './js/login';
import './js/helpers';
index.scss:
@import 'bootstrap';
@import 'util.scss';
@import 'login.scss';
@import 'site.css';
我在控制台中收到此错误:
Console
bundle.js
其他一切正常,我对 javascript 没有任何问题,但我不知道如何解决这个错误让我很烦恼。
PS:
一般来说,我是网络开发的新手,昨天开始使用 webpack,我的想法是在我的 html 中只使用 bundle.js 和 bundle.css,而不添加 jquery 和 bootstrap 分别为他们打标签
尝试将 MiniCssExtractPlugin 的选项 esModule 设置为 false
use: [
'style-loader',
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: false,
},
},
'css-loader',
'sass-loader'
]
不要将 'style-loader' 与 mini-css-extract-plugin 一起使用,请修改您的规则以匹配以下内容:
{
test: /\.s[c|a]ss$/,
include: path.resolve(__dirname, 'src'),
use: [
//'style-loader',
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
},
您可以在此处找到更多详细信息:https://github.com/webpack-contrib/mini-css-extract-plugin/issues/613
我已将 bundle.js 和 bundle.css 用于我的网络应用程序,
我的 package.json 看起来像这样:
"dependencies": {
"bootstrap": "^4.5.3",
"jquery": "^3.5.1",
"popper.js": "^1.16.1"
},
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/plugin-proposal-object-rest-spread": "^7.12.1",
"@babel/preset-env": "^7.12.11",
"babel-loader": "^8.2.2",
"css-loader": "^5.0.1",
"file-loader": "^6.2.0",
"mini-css-extract-plugin": "^1.3.3",
"node-sass": "^5.0.0",
"sass-loader": "^10.1.0",
"style-loader": "^2.0.0",
"url-loader": "^4.1.1",
"webpack": "^5.11.0",
"webpack-cli": "^4.2.0"
},
我的webpack.config.js:
const path = require('path');
const webpack = require('webpack');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
module.exports = (env, argv) => {
return {
//Define entry point
entry: ['./src/index.js', './src/css/index.scss'],
//Define output point
output: {
path: path.resolve(__dirname, 'wwwroot/dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.s[c|a]ss$/,
include: path.resolve(__dirname, 'src'),
use: [
'style-loader',
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
},
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
loader: 'babel-loader',
options: {
presets: ["@babel/preset-env"],
plugins: ['@babel/plugin-proposal-object-rest-spread']
}
},
]
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
}),
new MiniCssExtractPlugin({
filename: 'bundle.css'
})
]
};
};
我的src结构是这样的:
src
index.js:
import 'jquery';
import 'bootstrap';
import './js/login';
import './js/helpers';
index.scss:
@import 'bootstrap';
@import 'util.scss';
@import 'login.scss';
@import 'site.css';
我在控制台中收到此错误:
Console
bundle.js
其他一切正常,我对 javascript 没有任何问题,但我不知道如何解决这个错误让我很烦恼。
PS: 一般来说,我是网络开发的新手,昨天开始使用 webpack,我的想法是在我的 html 中只使用 bundle.js 和 bundle.css,而不添加 jquery 和 bootstrap 分别为他们打标签
尝试将 MiniCssExtractPlugin 的选项 esModule 设置为 false
use: [
'style-loader',
{
loader: MiniCssExtractPlugin.loader,
options: {
esModule: false,
},
},
'css-loader',
'sass-loader'
]
不要将 'style-loader' 与 mini-css-extract-plugin 一起使用,请修改您的规则以匹配以下内容:
{
test: /\.s[c|a]ss$/,
include: path.resolve(__dirname, 'src'),
use: [
//'style-loader',
MiniCssExtractPlugin.loader,
'css-loader',
'sass-loader'
]
},
您可以在此处找到更多详细信息:https://github.com/webpack-contrib/mini-css-extract-plugin/issues/613