Skip to content

Commit f9a138e

Browse files
feat(middleware): expose the memory filesystem (response.locals.fs) (#337)
1 parent bf586c9 commit f9a138e

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ In order to develop an app using server-side rendering, we need access to the
309309
generated with each build.
310310

311311
With server-side rendering enabled, `webpack-dev-middleware` sets the `stat` to
312-
`res.locals.webpackStats` before invoking the next middleware, allowing a
312+
`res.locals.webpackStats` and the memory filesystem to `res.locals.fs` before invoking the next middleware, allowing a
313313
developer to render the page body and manage the response to clients.
314314

315315
_Note: Requests for bundle files will still be handled by
@@ -337,17 +337,21 @@ app.use(middleware(compiler, { serverSideRender: true }))
337337
// The following middleware would not be invoked until the latest build is finished.
338338
app.use((req, res) => {
339339
const assetsByChunkName = res.locals.webpackStats.toJson().assetsByChunkName
340+
const fs = res.locals.fs
341+
const outputPath = res.locals.webpackStats.toJson().outputPath
340342

341343
// then use `assetsByChunkName` for server-sider rendering
342344
// For example, if you have only one main chunk:
343345
res.send(`
344346
<html>
345347
<head>
346348
<title>My App</title>
349+
<style>
347350
${normalizeAssets(assetsByChunkName.main)
348351
.filter(path => path.endsWith('.css'))
349-
.map(path => `<link rel="stylesheet" href="${path}" />`)
352+
.map(path => fs.readFileSync(outputPath + '/' + path))
350353
.join('\n')}
354+
</style>
351355
</head>
352356
<body>
353357
<div id="root"></div>

lib/middleware.js

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = function wrapper(context) {
1919
return new Promise(((resolve) => {
2020
ready(context, () => {
2121
res.locals.webpackStats = context.webpackStats;
22+
res.locals.fs = context.fs;
2223
resolve(next());
2324
}, req);
2425
}));

test/tests/server.js

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ describe('Server', () => {
339339
request(app).get('/foo/bar')
340340
.expect(200, () => {
341341
assert(locals.webpackStats);
342+
assert(locals.fs);
342343
done();
343344
});
344345
});

0 commit comments

Comments
 (0)