1 | var urlparse = require('url').parse;
|
2 |
|
3 | module.exports = function(r, app, opts, NODE_ENV, middleware) {
|
4 | var defaultCdn = opts.cdnUrl;
|
5 | var root = opts.root;
|
6 | var DEVELOPMENT = NODE_ENV === 'development';
|
7 |
|
8 | app.useBefore('router', '/build', function build(req, res, next) {
|
9 | middleware.static(opts.root + '/build', {
|
10 | maxAge: req.env === 'production' ? 31557600 : 0
|
11 | })(req, res, next);
|
12 | });
|
13 |
|
14 | app.useBefore('router', function assetLocals(req, res, next) {
|
15 | var min = req.env === 'production';
|
16 | var cdn = req.get('x-cdn') || defaultCdn;
|
17 | var base = urlparse(req.base).pathname;
|
18 | if (base === '/') base = '';
|
19 | res.locals({
|
20 | cdn: cdn + base + '/build',
|
21 | scripts: format(cdn, min, base + '/build', 'scripts', [base + '/build/main.js']),
|
22 | styles: format(cdn, min, base + '/build', 'styles', []),
|
23 | chunks: format(cdn, min, base + '/build', 'chunks', []),
|
24 | base: base,
|
25 | pretty: !min,
|
26 | basePath: base
|
27 | });
|
28 | next();
|
29 | });
|
30 |
|
31 | function format(cdn, min, dir, type, fallback) {
|
32 | return DEVELOPMENT ? fallback : lookup(cdn, min, dir, type);
|
33 | }
|
34 |
|
35 | function lookup(cdn, min, dir, type) {
|
36 |
|
37 | return (r(root + '/manifest.json')[type] || []).map(function(entry) {
|
38 | return cdn + dir + '/' + entry;
|
39 | });
|
40 | }
|
41 | }; |
\ | No newline at end of file |