我已经配置了 webpack@5.10.0(带有 webpack-cli@4.2.0)以使用 html-webpack-plugin(加上 file-loader@6.2.0 和 sass-loader@10.1.0):
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const postcssPresetEnv = require('postcss-preset-env');
module.exports = {
entry: {
index: './src/index.js',
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: './src/index.html',
}),
new MiniCssExtractPlugin({
filename: '[name].css',
}),
],
output: {
publicPath: './',
filename: '[name].js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.s?css$/i,
use: [
{ loader: MiniCssExtractPlugin.loader },
'css-loader',
{
loader: 'postcss-loader',
options: {
ident: 'postcss',
plugins: () => [
postcssPresetEnv(/* pluginOptions */)
]
}
},
'sass-loader',
],
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
loader: 'file-loader',
},
],
},
};
到那时,一切正常(style.scss
被导入index.js
,样式表本身加载 2 jpgs background-image: url(...)
)。
现在,我的 html 模板 ( index.html
) 引用了一个文件 ( <img src="./file.svg">
),该文件 ( ) 被复制(通过file-loader
,使用另一个名称)。但是决赛的参考index.html
必须反映这种变化。
所以我安装了html-loader@1.3.2
:
[...]
module: {
rules: [
{
test: /\.html$/i,
loader: 'html-loader',
},
[...]
现在npx webpack build
在没有进一步说明的情况下崩溃:
[webpack-cli] Compilation finished
assets by status 84 KiB [cached] 2 assets
./src/index.js 26 bytes [built] [code generated]
1 ERROR in child compilations
webpack 5.10.0 compiled with 1 error in 867 ms
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
怎么办?如何获取调试信息?出了什么问题?
$ npm list
a@1.0.0 /home/rslemos/workspace/a/webpack
├── clean-webpack-plugin@3.0.0
├── css-loader@5.0.1
├── file-loader@6.2.0
├── html-loader@1.3.2
├── html-webpack-plugin@4.5.0
├── lodash@4.17.20
├── mini-css-extract-plugin@1.3.2
├── node-sass@5.0.0
├── postcss-loader@3.0.0
├── postcss-preset-env@6.7.0
├── sass-loader@10.1.0
├── sass@1.30.0
├── style-loader@2.0.0
├── webpack-cli@4.2.0
└── webpack@5.10.0