【发布时间】:2023-04-08 23:41:01
【问题描述】:
我正在尝试通过在每个 javascript 文件的末尾添加一个哈希来使用 webpack 进行缓存破坏。我的 webpack 配置文件如下:
const AssetsPlugin = require('assets-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
//const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
entry: "./js/main.js",
output: {
path: __dirname + '/static/',
publicPath: '',
filename: "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'], {watch: true}),
new AssetsPlugin({
filename: 'static/webpack.assets.json',
prettyPrint: true
}),
]
};
为 webpack 创建的 javascript 文件提供服务的 index.html 文件如下:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" >
$.getJSON('webpack.assets.json', function(data){
<!--
var bundleScript = "<script src=" + data['main']['js'] + "></script>";
var div = document.getElementById('app');
div.innerHTML = bundleScript;
$(bundleScript).appendTo('#app');
//!-->
});
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>
当我对代码进行更改时,我必须用力刷新浏览器才能看到更改,而不是单纯地刷新,如果缓存破坏能够正常工作,我会期望这样。任何帮助表示赞赏;谢谢!
【问题讨论】:
-
我认为在 webpack 服务器运行脚本中添加
--watch会对你有所帮助
标签: javascript webpack ecmascript-6 browser-cache