我已经创建了bundle.js和bundle.css用于我的Web应用程序,
我的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:我对Web开发还是新手,昨天开始使用webpack,我的想法是在我的html中只使用bundle.js和bundle.css,而不单独为它们添加jquery和bootstrap标记
3条答案
按热度按时间qyswt5oh1#
尝试将MiniCssExtractPlugin的esModule选项设置为false
a64a0gku2#
不要将'style-loader'与mini-css-extract-plugin沿着使用,修改您的规则以匹配以下内容:
您可以在此处找到更多详细信息:https://github.com/webpack-contrib/mini-css-extract-plugin/issues/613
3htmauhk3#
类似于@Dina的答案,但没有
MiniCssExtractPlugin
: