Better dev server integration

This commit is contained in:
MICHAEL JACKSON
2018-02-16 16:00:06 -08:00
parent a22e0fa801
commit d6f2bc089a
42 changed files with 1753 additions and 1154 deletions

View File

@ -0,0 +1,30 @@
const fs = require("fs");
const invariant = require("invariant");
const createBundle = require("./utils/createBundle");
/**
* An express middleware that sets req.bundle from the build
* info in the given stats file. Should be used in production.
*/
function staticAssets(webpackStatsFile) {
let stats;
try {
stats = JSON.parse(fs.readFileSync(webpackStatsFile, "utf8"));
} catch (error) {
invariant(
false,
"staticAssets middleware cannot read the build stats in %s; " +
"run `yarn build` before starting the server",
webpackStatsFile
);
}
const bundle = createBundle(stats);
return (req, res, next) => {
req.bundle = bundle;
next();
};
}
module.exports = staticAssets;