statics.js
1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const _ = require('lodash');
const config = global.yoho.config;
module.exports = (app) => {
let manifest;
if (!app.locals.devEnv) {
manifest = require('../../manifest.json');
}
function getStatic(path, def) {
return _.get(manifest, path, `${config.assetUrl}${def}`);
}
return (req, res, next) => {
res.on('beforeRender', (params) => {
if (params) {
const {data} = params;
const {module: moduleName, page, localCss, isFeature, vue} = Object.assign({}, res.locals, data);
if (moduleName && page) {
const statics = {
name: `${moduleName}.${page}`,
styles: [],
javascripts: [
getStatic('common.vendors.js', 'js/vendors.js'),
getStatic('common.main.js', 'js/main.js'),
]
};
if (isFeature || localCss || vue) {
if (!isFeature) {
statics.styles.push(getStatic('common.main.css', 'css/main.css'));
}
statics.styles.push(
getStatic(`pages.${moduleName}.${page}.css`, `css/page.${moduleName}.${page}.css`));
} else {
statics.styles.push(getStatic('common.index.css', 'css/index.css'));
}
res.locals.statics = statics;
}
}
});
return next();
};
};