2018-02-17 00:00:06 +00:00
|
|
|
const invariant = require("invariant");
|
2018-05-21 20:26:00 +00:00
|
|
|
|
|
|
|
const createBundle = require("../utils/createBundle");
|
2018-02-17 00:00:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An express middleware that sets req.bundle 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;
|
|
|
|
webpackCompiler.plugin("done", stats => {
|
|
|
|
bundle = createBundle(stats.toJson());
|
|
|
|
});
|
|
|
|
|
|
|
|
return (req, res, next) => {
|
|
|
|
invariant(
|
|
|
|
bundle != null,
|
|
|
|
"devAssets middleware needs a running compiler; " +
|
|
|
|
"use webpack-dev-middleware in front of devAssets"
|
|
|
|
);
|
|
|
|
|
|
|
|
req.bundle = bundle;
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = devAssets;
|