2015-08-27 06:20:56 +00:00
|
|
|
import manifest from '../rev-manifest.json';
|
|
|
|
|
2015-08-28 22:54:40 +00:00
|
|
|
const __DEV__ = process.env.NODE_ENV === 'development';
|
2015-09-08 22:03:27 +00:00
|
|
|
const manifestPath = '../rev-manifest.json';
|
|
|
|
|
2015-08-27 06:20:56 +00:00
|
|
|
export default function({ globalPrepend = '' } = {}) {
|
|
|
|
|
2015-08-28 22:54:40 +00:00
|
|
|
function rev(manifest, scopedPrepend, asset) {
|
2015-08-27 06:20:56 +00:00
|
|
|
return `${globalPrepend}${scopedPrepend}/${ manifest[asset] || asset }`;
|
|
|
|
}
|
2015-08-28 22:54:40 +00:00
|
|
|
const boundRev = rev.bind(null, manifest);
|
2015-08-27 06:20:56 +00:00
|
|
|
|
|
|
|
return function(req, res, next) {
|
2015-08-28 22:54:40 +00:00
|
|
|
// in dev environment, we reread the manifest on every call
|
|
|
|
// this means we do not need to restart server on every change to
|
|
|
|
// client code
|
|
|
|
if (__DEV__) {
|
2015-09-08 22:03:27 +00:00
|
|
|
// we first need to remove the manifest from require cache
|
|
|
|
delete require.cache[require.resolve(manifestPath)];
|
|
|
|
// and re-require
|
|
|
|
const manifest = require(manifestPath);
|
2015-08-28 22:54:40 +00:00
|
|
|
res.locals.rev = rev.bind(null, manifest);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
// in production we take use the initially loaded manifest
|
|
|
|
// since this should not change in production
|
|
|
|
res.locals.rev = boundRev;
|
2015-08-27 06:20:56 +00:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|