1
0
mirror of https://gitlab.com/timvisee/send.git synced 2024-11-14 15:12:48 +01:00
send/common/assets.js

56 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-09-07 05:00:00 +02:00
const genmap = require('./generate_asset_map');
const isServer = typeof genmap === 'function';
let prefix = '';
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];
}
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);
}
const instance = {
setPrefix: setPrefix,
get: getAsset,
2018-06-19 21:58:42 +02:00
match: getMatches,
setMiddleware: function(middleware) {
2018-06-19 21:58:42 +02:00
function getManifest() {
return JSON.parse(
middleware.fileSystem.readFileSync(
middleware.getFilenameFromUrl('/manifest.json')
)
);
}
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]);
};
}
}
};
module.exports = instance;