Better dev server integration
This commit is contained in:
30
server/middleware/staticAssets.js
Normal file
30
server/middleware/staticAssets.js
Normal 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;
|
Reference in New Issue
Block a user