Comments (5)
上面两个方法思路差不多, 把构建后的hash落盘, 在应用中读取, 最后对 helper.asset()
行为做一些定制.
- hash生成用
webpack-manifest-plugin
好一些, 给出每个文件对应的hash (contentHash或者 chunkHash), 读取manifest.json
可以放在 config 里进行:
// 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(),
});
这样做只会在应用启动时读取一次
- 定制 asset 方法, 可以在helper里或者context里进行, 这里我给出在 context 里扩展 asset的示例代码:
// 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中定制
from beidou.
我的临时方案是:
在生产环境启动时,先读取 build 后在项目根目录生成的.stat 文件第一行的 hash,
然后把这个 hash 存放在 helper 扩展里。
在 html 模板里,在静态资源后面加入这个 deploy hash作为参数。这样每次构建之后,都会有不同的 hash。
核心代码:
// app/extend/helper.js
if (app.config.env === 'prod') {
try {
const liner = new lineByLine(statsFilePath);
let line;
while ((line = liner.next())) {
let hash = line.toString('ascii').substr(-5);
if (hash) deployhash = `?${hash}`;
break; // 只读第一行
}
} catch (err) { }
}
// layout.jsx
helper.asset(`manifest.js${helper.DEPLOY_HASH}`)
from beidou.
我的粗糙解决方案如下:
webpack配置webpack-manifest-plugin,然后将manifest.json传到layout/index.jsx中:
// webpack.config.js
new ManifestPlugin({
fileName: 'manifest.json',
manifestVariable: "webpackManifest",
writeToFileEmit: true,
}),
// app/db/index.js
/**
* Disk Based JSON Database
* Only for demo usage.
* egg-sequelize is recommended in real word scenarios
* see. https://github.com/eggjs/egg-sequelize
*/
const path = require('path');
const db = require('diskdb');
module.exports = db.connect(path.join(__dirname, '../../build'), [
'manifest'
]);
// client/layout/index.jsx
const source = this.props.ctx.db.manifest.find();
// css 引用
<link rel="stylesheet" href={helper.asset(source[`${asset}.css`])}/>
// js 引用
<script src={helper.asset(source[`${asset}.js`])}/>
from beidou.
webpack-manifest-plugin并不能生成hash,生成hash还是需要修改webpack.config.js中的output去生成,webpack-manifest-plugin只是对这些文件做映射。不知道我这么说对不对
from beidou.
close. support after #161
from beidou.
Related Issues (20)
- 路由错误最小复现
- 招贤纳士
- v2.2.1版本修改factory.getPlugin返回值无效 HOT 1
- with-dva 项目运行警告
- with-dva 在官方demo 修改成路由的模式
- 如何使用app.injectCsrf和app.injectNonce HOT 1
- Build framework on the top of Beidou HOT 2
- npm包版本问题
- 关于设置alias,出现Debug下无法加载资源
- ReactDOM.hydrate会覆盖服务端渲染结果,如果注释,相关事件就无效。如何处理? HOT 2
- facebook分享报206 HOT 4
- client获取controller数据 HOT 2
- 请问现在beidou还更新吗,有更好的解决方案了吗? HOT 2
- NODE_LOG_DIR not configured HOT 1
- typescript模板打包报错 HOT 3
- with-react-loadable 無法載入 HOT 1
- webpack 服务是否可以支持https
- 继续升级,作者继续啊
- 从零开始搭建应用 无法启动 HOT 3
- 主进程会偶尔收到SIGTERM信号导致程序exit HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from beidou.