New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
webpack 打包文件名加 hash #55
Comments
我的临时方案是: 核心代码:
|
我的粗糙解决方案如下: webpack配置webpack-manifest-plugin,然后将manifest.json传到layout/index.jsx中:
|
上面两个方法思路差不多, 把构建后的hash落盘, 在应用中读取, 最后对
// config/config.prod.js
const path = require('path');
const fs = require('fs');
function getMainfest() {
try {
const mainfestFile = path.join(__dirname, '../build/manifest.json');
if (fs.existsSync(mainfestFile)) {
const raw = fs.readFileSync(mainfestFile);
return JSON.parse(raw);
}
} catch (e) {
return {};
}
}
module.exports = {
manifest: getMainfest(),
}); 这样做只会在应用启动时读取一次
// app/extend/context.js
module.exports = {
assetWithHash(asset) {
// 读取配置中的manifest
const manifest = this.app.config.manifest || {};
// 映射文件名
let filename = asset;
if (manifest[asset]) {
filename = manifest[asset];
}
return this.asset(filename);
},
} manifest.json 中的文件映射关系, 可以更具不同需要, 在 assetWithHash中定制 |
webpack-manifest-plugin并不能生成hash,生成hash还是需要修改webpack.config.js中的output去生成,webpack-manifest-plugin只是对这些文件做映射。不知道我这么说对不对 |
close. support after #161 |
Current behavior
目前 webpack 打包出来的文件名都是不带 hash 的,类似 bundle.css, bundle.js
Expected behavior
期待打包出来的文件名默认带 hash (例如 bundle.8da6c.js),并且 helper.asset 能自动引用带 hash 的文件,解决打包文件更新了但浏览器缓存没更新问题
The text was updated successfully, but these errors were encountered: