问题描述:
使用webpack5配置了clean-webpack-plugin,发现dist文件下内容build时,上次打包dist内容并没有被删除,配置如下:
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: '[name].[chunkhash:8].js',
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: './index.html',
}),
]
};
解决方案:
一开始怀疑是不是webpack5再搞什么幺蛾子,后来经过一系列排查(主要是和可用项目配置对比),发现,应该是output配置没有配path,导致clean-webpack-plugin没有找到清除dist内容的地方😂,修改了配置如下,clean-webpack-plugin可以正常工作了
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
module.exports = {
entry: './src/index.js',
output: {
filename: '[name].[chunkhash:8].js',
path: path.resolve(__dirname, './dist')
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: './index.html',
}),
]
};