[英]html-webpack-plugin injection and webpack causing continuous refresh
我在使用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'
})
]
};
根据文档, 您可以让插件为您生成HTML文件,提供自己的模板
因此,您可以通过提供文件名来生成HTML文件,也可以传递诸如index.html之类的模板
在开发中,应删除文件名中的所有哈希,以防止出现这种情况!
Webpack可以通过热重载以新内容更新旧文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.