Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(middleware): expose the memory filesystem (response.locals.fs) #337

Merged
merged 3 commits into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ In order to develop an app using server-side rendering, we need access to the
generated with each build.

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

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

// then use `assetsByChunkName` for server-sider rendering
// For example, if you have only one main chunk:
res.send(`
<html>
<head>
<title>My App</title>
<style>
${normalizeAssets(assetsByChunkName.main)
.filter(path => path.endsWith('.css'))
.map(path => `<link rel="stylesheet" href="${path}" />`)
.map(path => fs.readFileSync(outputPath + '/' + path))
.join('\n')}
</style>
</head>
<body>
<div id="root"></div>
Expand Down
1 change: 1 addition & 0 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = function wrapper(context) {
return new Promise(((resolve) => {
ready(context, () => {
res.locals.webpackStats = context.webpackStats;
res.locals.fs = context.fs;
resolve(next());
}, req);
}));
Expand Down
1 change: 1 addition & 0 deletions test/tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ describe('Server', () => {
request(app).get('/foo/bar')
.expect(200, () => {
assert(locals.webpackStats);
assert(locals.fs);
done();
});
});
Expand Down