unpkg/server/middleware/utils/createBundle.js

29 lines
709 B
JavaScript
Raw Normal View History

2018-02-17 00:00:06 +00:00
/**
* Creates a bundle object that is stored on req.bundle.
*/
function createBundle(webpackStats) {
const { publicPath, assetsByChunkName } = webpackStats;
const createURL = asset => publicPath + asset;
const getAssets = (chunks = ["main"]) =>
(Array.isArray(chunks) ? chunks : [chunks])
.reduce((memo, chunk) => memo.concat(assetsByChunkName[chunk] || []), [])
.map(createURL);
const getScripts = (...args) =>
getAssets(...args).filter(asset => /\.js$/.test(asset));
const getStyles = (...args) =>
getAssets(...args).filter(asset => /\.css$/.test(asset));
return {
createURL,
getAssets,
getScripts,
getStyles
};
}
module.exports = createBundle;