Rename req.bundle => req.assets

This commit is contained in:
Michael Jackson 2018-07-18 00:10:37 -07:00
parent cb577b5d84
commit f8eea6ac2b
4 changed files with 23 additions and 28 deletions

View File

@ -4,8 +4,8 @@ const renderPage = require("../utils/renderPage");
function serveMainPage(req, res) {
res.send(
renderPage(MainPage, {
scripts: req.bundle.getScripts("main"),
styles: req.bundle.getStyles("main")
scripts: req.assets.getScripts("main"),
styles: req.assets.getStyles("main")
})
);
}

View File

@ -1,26 +1,26 @@
const invariant = require("invariant");
const createBundle = require("../utils/createBundle");
const createAssets = require("./utils/createAssets");
/**
* An express middleware that sets req.bundle from the
* An express middleware that sets req.assets from the
* latest result from a running webpack compiler (i.e. using
* webpack-dev-middleware). Should only be used in dev.
*/
function devAssets(webpackCompiler) {
let bundle;
let assets;
webpackCompiler.plugin("done", stats => {
bundle = createBundle(stats.toJson());
assets = createAssets(stats.toJson());
});
return (req, res, next) => {
invariant(
bundle != null,
assets != null,
"devAssets middleware needs a running compiler; " +
"use webpack-dev-middleware in front of devAssets"
);
req.bundle = bundle;
req.assets = assets;
next();
};

View File

@ -1,10 +1,10 @@
const fs = require("fs");
const invariant = require("invariant");
const createBundle = require("../utils/createBundle");
const createAssets = require("./utils/createAssets");
/**
* An express middleware that sets req.bundle from the build
* An express middleware that sets req.assets from the build
* info in the given stats file. Should be used in production.
*/
function staticAssets(webpackStatsFile) {
@ -20,10 +20,10 @@ function staticAssets(webpackStatsFile) {
);
}
const bundle = createBundle(stats);
const assets = createAssets(stats);
return (req, res, next) => {
req.bundle = bundle;
req.assets = assets;
next();
};
}

View File

@ -1,45 +1,40 @@
/**
* Creates a bundle object that is stored on req.bundle.
* Creates an assets object that is stored on req.assets.
*/
function createBundle(webpackStats) {
function createAssets(webpackStats) {
const { publicPath, assetsByChunkName } = webpackStats;
/**
* Returns a public URL to the given asset.
*/
function createURL(asset) {
return publicPath + asset;
}
const createURL = asset => publicPath + asset;
/**
* Returns an array of URLs to all assets in the given chunks.
*/
function getAssets(chunks = ["main"]) {
return (Array.isArray(chunks) ? chunks : [chunks])
const getAll = (chunks = ["main"]) =>
(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));
}
const getScripts = (...chunks) =>
getAll(...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));
}
const getStyles = (...chunks) =>
getAll(...chunks).filter(asset => /\.css$/.test(asset));
return {
createURL,
getAssets,
getAll,
getScripts,
getStyles
};
}
module.exports = createBundle;
module.exports = createAssets;