我在使用html-webpack-plugin和webpack时遇到问题,其中html-webpack-plugin将脚本注入index.html
的主体。这显然会导致webpack不断刷新,并且每次都会生成新文件。首先,我如何防止这种情况发生,其次,是否有办法配置html-webpack-plugin来删除与webpack生成的javascript文件相对应的脚本(在这种情况下,对应的是过时的文件)在下面的配置文件中输出static/bundle-[hash].js
?
这是webpack配置文件:
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: "./js/main.js",
output: {
filename: "static/bundle-[hash].js",
},
resolveLoader: {
moduleExtensions: ['-loader']
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
presets: ['react', 'es2015', 'stage-0']
}
},
{
test: /\.css$/,
loader: 'style-loader',
},
{
test: /\.css$/,
loader: 'css-loader',
query: {
modules: true,
localIdentName: '[name]__[local]___[hash:base64:5]'
}
}
]
},
plugins: [
new CleanWebpackPlugin(['static/bundle*.js']),
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'index.html',
inject: 'body'
})
]
};
答案 0 :(得分:0)
根据doc 您可以让插件为您生成HTML文件,提供您自己的模板
因此您可以通过提供文件名生成HTML文件,也可以传递 index.html
等模板答案 1 :(得分:0)
在开发中,您应该删除文件名中的所有哈希以防止此行为!
Webpack可以使用热重新加载新内容来更新旧文件。