2017-08-24 23:54:02 +02:00
|
|
|
const genmap = require('../build/generate_asset_map');
|
|
|
|
const isServer = typeof genmap === 'function';
|
2018-11-08 22:35:19 +01:00
|
|
|
let prefix = '';
|
2017-08-24 23:54:02 +02:00
|
|
|
let manifest = {};
|
|
|
|
try {
|
|
|
|
//eslint-disable-next-line node/no-missing-require
|
|
|
|
manifest = require('../dist/manifest.json');
|
|
|
|
} catch (e) {
|
|
|
|
// use middleware
|
|
|
|
}
|
|
|
|
|
|
|
|
const assets = isServer ? manifest : genmap;
|
|
|
|
|
|
|
|
function getAsset(name) {
|
|
|
|
return prefix + assets[name];
|
|
|
|
}
|
|
|
|
|
2018-11-08 22:35:19 +01:00
|
|
|
function setPrefix(name) {
|
|
|
|
prefix = name;
|
|
|
|
}
|
|
|
|
|
2018-06-19 21:58:42 +02:00
|
|
|
function getMatches(match) {
|
|
|
|
return Object.keys(assets)
|
|
|
|
.filter(k => match.test(k))
|
|
|
|
.map(getAsset);
|
|
|
|
}
|
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
const instance = {
|
2018-11-08 22:35:19 +01:00
|
|
|
setPrefix: setPrefix,
|
2017-08-24 23:54:02 +02:00
|
|
|
get: getAsset,
|
2018-06-19 21:58:42 +02:00
|
|
|
match: getMatches,
|
2017-08-24 23:54:02 +02:00
|
|
|
setMiddleware: function(middleware) {
|
2018-06-19 21:58:42 +02:00
|
|
|
function getManifest() {
|
|
|
|
return JSON.parse(
|
|
|
|
middleware.fileSystem.readFileSync(
|
|
|
|
middleware.getFilenameFromUrl('/manifest.json')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2017-08-24 23:54:02 +02:00
|
|
|
if (middleware) {
|
|
|
|
instance.get = function getAssetWithMiddleware(name) {
|
2018-06-19 21:58:42 +02:00
|
|
|
const m = getManifest();
|
|
|
|
return prefix + m[name];
|
|
|
|
};
|
|
|
|
instance.match = function matchAssetWithMiddleware(match) {
|
|
|
|
const m = getManifest();
|
|
|
|
return Object.keys(m)
|
|
|
|
.filter(k => match.test(k))
|
|
|
|
.map(k => prefix + m[k]);
|
2017-08-24 23:54:02 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = instance;
|