diff --git a/server/actions/serveMainPage.js b/server/actions/serveMainPage.js index 5fc1b21..9bb3f09 100644 --- a/server/actions/serveMainPage.js +++ b/server/actions/serveMainPage.js @@ -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") }) ); } diff --git a/server/middleware/devAssets.js b/server/middleware/devAssets.js index fd281a7..c071048 100644 --- a/server/middleware/devAssets.js +++ b/server/middleware/devAssets.js @@ -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(); }; diff --git a/server/middleware/staticAssets.js b/server/middleware/staticAssets.js index 3158762..ad5d8d4 100644 --- a/server/middleware/staticAssets.js +++ b/server/middleware/staticAssets.js @@ -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(); }; } diff --git a/server/utils/createBundle.js b/server/middleware/utils/createAssets.js similarity index 50% rename from server/utils/createBundle.js rename to server/middleware/utils/createAssets.js index 46fc7b8..08e364a 100644 --- a/server/utils/createBundle.js +++ b/server/middleware/utils/createAssets.js @@ -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;