Rename req.bundle => req.assets
This commit is contained in:
parent
cb577b5d84
commit
f8eea6ac2b
|
@ -4,8 +4,8 @@ const renderPage = require("../utils/renderPage");
|
||||||
function serveMainPage(req, res) {
|
function serveMainPage(req, res) {
|
||||||
res.send(
|
res.send(
|
||||||
renderPage(MainPage, {
|
renderPage(MainPage, {
|
||||||
scripts: req.bundle.getScripts("main"),
|
scripts: req.assets.getScripts("main"),
|
||||||
styles: req.bundle.getStyles("main")
|
styles: req.assets.getStyles("main")
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,26 @@
|
||||||
const invariant = require("invariant");
|
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
|
* latest result from a running webpack compiler (i.e. using
|
||||||
* webpack-dev-middleware). Should only be used in dev.
|
* webpack-dev-middleware). Should only be used in dev.
|
||||||
*/
|
*/
|
||||||
function devAssets(webpackCompiler) {
|
function devAssets(webpackCompiler) {
|
||||||
let bundle;
|
let assets;
|
||||||
webpackCompiler.plugin("done", stats => {
|
webpackCompiler.plugin("done", stats => {
|
||||||
bundle = createBundle(stats.toJson());
|
assets = createAssets(stats.toJson());
|
||||||
});
|
});
|
||||||
|
|
||||||
return (req, res, next) => {
|
return (req, res, next) => {
|
||||||
invariant(
|
invariant(
|
||||||
bundle != null,
|
assets != null,
|
||||||
"devAssets middleware needs a running compiler; " +
|
"devAssets middleware needs a running compiler; " +
|
||||||
"use webpack-dev-middleware in front of devAssets"
|
"use webpack-dev-middleware in front of devAssets"
|
||||||
);
|
);
|
||||||
|
|
||||||
req.bundle = bundle;
|
req.assets = assets;
|
||||||
|
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const invariant = require("invariant");
|
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.
|
* info in the given stats file. Should be used in production.
|
||||||
*/
|
*/
|
||||||
function staticAssets(webpackStatsFile) {
|
function staticAssets(webpackStatsFile) {
|
||||||
|
@ -20,10 +20,10 @@ function staticAssets(webpackStatsFile) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const bundle = createBundle(stats);
|
const assets = createAssets(stats);
|
||||||
|
|
||||||
return (req, res, next) => {
|
return (req, res, next) => {
|
||||||
req.bundle = bundle;
|
req.assets = assets;
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
const { publicPath, assetsByChunkName } = webpackStats;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a public URL to the given asset.
|
* Returns a public URL to the given asset.
|
||||||
*/
|
*/
|
||||||
function createURL(asset) {
|
const createURL = asset => publicPath + asset;
|
||||||
return publicPath + asset;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array of URLs to all assets in the given chunks.
|
* Returns an array of URLs to all assets in the given chunks.
|
||||||
*/
|
*/
|
||||||
function getAssets(chunks = ["main"]) {
|
const getAll = (chunks = ["main"]) =>
|
||||||
return (Array.isArray(chunks) ? chunks : [chunks])
|
(Array.isArray(chunks) ? chunks : [chunks])
|
||||||
.reduce((memo, chunk) => memo.concat(assetsByChunkName[chunk] || []), [])
|
.reduce((memo, chunk) => memo.concat(assetsByChunkName[chunk] || []), [])
|
||||||
.map(createURL);
|
.map(createURL);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array of URLs to all JavaScript files in the given chunks.
|
* Returns an array of URLs to all JavaScript files in the given chunks.
|
||||||
*/
|
*/
|
||||||
function getScripts(...chunks) {
|
const getScripts = (...chunks) =>
|
||||||
return getAssets(...chunks).filter(asset => /\.js$/.test(asset));
|
getAll(...chunks).filter(asset => /\.js$/.test(asset));
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array of URLs to all CSS files in the given chunks.
|
* Returns an array of URLs to all CSS files in the given chunks.
|
||||||
*/
|
*/
|
||||||
function getStyles(...chunks) {
|
const getStyles = (...chunks) =>
|
||||||
return getAssets(...chunks).filter(asset => /\.css$/.test(asset));
|
getAll(...chunks).filter(asset => /\.css$/.test(asset));
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
createURL,
|
createURL,
|
||||||
getAssets,
|
getAll,
|
||||||
getScripts,
|
getScripts,
|
||||||
getStyles
|
getStyles
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = createBundle;
|
module.exports = createAssets;
|
Loading…
Reference in New Issue