unpkg/modules/middleware/devAssets.js

30 lines
689 B
JavaScript
Raw Normal View History

2018-02-17 00:00:06 +00:00
const invariant = require("invariant");
2018-07-18 07:10:37 +00:00
const createAssets = require("./utils/createAssets");
2018-02-17 00:00:06 +00:00
/**
2018-07-18 07:10:37 +00:00
* An express middleware that sets req.assets from the
2018-02-17 00:00:06 +00:00
* latest result from a running webpack compiler (i.e. using
* webpack-dev-middleware). Should only be used in dev.
*/
function devAssets(webpackCompiler) {
2018-07-18 07:10:37 +00:00
let assets;
2018-02-17 00:00:06 +00:00
webpackCompiler.plugin("done", stats => {
2018-07-18 07:10:37 +00:00
assets = createAssets(stats.toJson());
2018-02-17 00:00:06 +00:00
});
return (req, res, next) => {
invariant(
2018-07-18 07:10:37 +00:00
assets != null,
2018-02-17 00:00:06 +00:00
"devAssets middleware needs a running compiler; " +
"use webpack-dev-middleware in front of devAssets"
);
2018-07-18 07:10:37 +00:00
req.assets = assets;
2018-02-17 00:00:06 +00:00
next();
};
}
module.exports = devAssets;