Move middleware utils into server/utils

This commit is contained in:
Michael Jackson
2018-05-21 13:26:00 -07:00
parent 269b756aeb
commit c792515d01
14 changed files with 59 additions and 40 deletions

View File

@ -0,0 +1,45 @@
/**
* Creates a bundle object that is stored on req.bundle.
*/
function createBundle(webpackStats) {
const { publicPath, assetsByChunkName } = webpackStats;
/**
* Returns a public URL to the given asset.
*/
function createURL(asset) {
return publicPath + asset;
}
/**
* Returns an array of URLs to all assets in the given chunks.
*/
function getAssets(chunks = ["main"]) {
return (Array.isArray(chunks) ? chunks : [chunks])
.reduce((memo, chunk) => memo.concat(assetsByChunkName[chunk] || []), [])
.map(createURL);
}
/**
* Returns an array of URLs to all JavaScript files in the given chunks.
*/
function getScripts(...chunks) {
return getAssets(...chunks).filter(asset => /\.js$/.test(asset));
}
/**
* Returns an array of URLs to all CSS files in the given chunks.
*/
function getStyles(...chunks) {
return getAssets(...chunks).filter(asset => /\.css$/.test(asset));
}
return {
createURL,
getAssets,
getScripts,
getStyles
};
}
module.exports = createBundle;