@@ -25,11 +25,6 @@ Some of the benefits of using this middleware include:
2525 has completed.
2626- Supports hot module reload (HMR).
2727
28- ## Requirements
29-
30- This module requires a minimum of Node v6.9.0 and Webpack v4.0.0, and must be used with a
31- server that accepts express-style middleware.
32-
3328## Getting Started
3429
3530First thing's first, install the module:
@@ -62,10 +57,7 @@ app.listen(3000, () => console.log('Example app listening on port 3000!'));
6257
6358## Options
6459
65- The middleware accepts an ` options ` Object. The following is a property reference
66- for the Object.
67-
68- _ Note: The ` publicPath ` property is required, whereas all other options are optional_
60+ The middleware accepts an ` options ` Object. The following is a property reference for the Object.
6961
7062### methods
7163
@@ -79,109 +71,34 @@ This property allows a user to pass the list of HTTP request methods accepted by
7971Type: ` Object `
8072Default: ` undefined `
8173
82- This property allows a user to pass custom HTTP headers on each request. eg.
83- ` { "X-Custom-Header": "yes" } `
74+ This property allows a user to pass custom HTTP headers on each request.
75+ eg. ` { "X-Custom-Header": "yes" } `
8476
8577### index
8678
87- Type: ` String `
88- Default: ` undefined `
89-
90- "index.html",
91- // The index path for web server, defaults to "index.html".
92- // If falsy (but not undefined), the server will not respond to requests to the root URL.
93-
94- ### lazy
95-
96- Type: ` Boolean `
97- Default: ` undefined `
98-
99- This option instructs the module to operate in 'lazy' mode, meaning that it won't
100- recompile when files change, but rather on each request.
101-
102- ### logger
103-
104- Type: ` Object `
105- Default: [ ` webpack-log ` ] ( https://github.com/webpack-contrib/webpack-log/blob/master/index.js )
106-
107- In the rare event that a user would like to provide a custom logging interface,
108- this property allows the user to assign one. The module leverages
109- [ ` webpack-log ` ] ( https://github.com/webpack-contrib/webpack-log#readme )
110- for creating the [ ` loglevelnext ` ] ( https://github.com/shellscape/loglevelnext#readme )
111- logging management by default. Any custom logger must adhere to the same
112- exports for compatibility. Specifically, all custom loggers must have the
113- following exported methods at a minimum:
114-
115- - ` log.trace `
116- - ` log.debug `
117- - ` log.info `
118- - ` log.warn `
119- - ` log.error `
120-
121- Please see the documentation for ` loglevel ` for more information.
122-
123- ### logLevel
124-
125- Type: ` String `
126- Default: ` 'info' `
127-
128- This property defines the level of messages that the module will log. Valid levels
129- include:
130-
131- - ` trace `
132- - ` debug `
133- - ` info `
134- - ` warn `
135- - ` error `
136- - ` silent `
79+ Type: ` Boolean|String `
80+ Default: ` index.html `
13781
138- Setting a log level means that all other levels below it will be visible in the
139- console. Setting ` logLevel: 'silent' ` will hide all console output. The module
140- leverages [ ` webpack-log ` ] ( https://github.com/webpack-contrib/webpack-log#readme )
141- for logging management, and more information can be found on its page.
142-
143- ### logTime
144-
145- Type: ` Boolean `
146- Default: ` false `
147-
148- If ` true ` the log output of the module will be prefixed by a timestamp in the
149- ` HH:mm:ss ` format.
82+ If ` false ` (but not ` undefined ` ), the server will not respond to requests to the root URL.
15083
15184### mimeTypes
15285
15386Type: ` Object `
154- Default: ` null `
87+ Default: ` undefined `
15588
15689This property allows a user to register custom mime types or extension mappings.
157- eg. ` mimeTypes: { 'text/html': [ 'phtml' ] } ` .
90+ eg. ` mimeTypes: { phtml: 'text/html' } ` .
15891
159- By default node-mime will throw an error if you try to map a type to an extension
160- that is already assigned to another type. Passing ` force: true ` will suppress this behavior
161- (overriding any previous mapping).
162- eg. ` mimeTypes: { typeMap: { 'text/html': [ 'phtml' ] }, force: true } ` .
163-
164- Please see the documentation for
165- [ ` node-mime ` ] ( https://github.com/broofa/node-mime#mimedefinetypemap-force--false ) for more information.
92+ Please see the documentation for [ ` mime-types ` ] ( https://github.com/jshttp/mime-types ) for more information.
16693
16794### publicPath
16895
169- Type: ` String `
170- _ Required_
171-
172- The public path that the middleware is bound to. _ Best Practice: use the same
173- ` publicPath ` defined in your webpack config. For more information about
174- ` publicPath ` , please see
175- [ the webpack documentation] ( https://webpack.js.org/guides/public-path ) ._
96+ Type: ` String `
97+ Default: ` output.publicPath `
17698
177- ### reporter
178-
179- Type: ` Object `
180- Default: ` undefined `
181-
182- Allows users to provide a custom reporter to handle logging within the module.
183- Please see the [ default reporter] ( /lib/utils/reporter.js )
184- for an example.
99+ The public path that the middleware is bound to.
100+ _ Best Practice: use the same ` publicPath ` defined in your webpack config.
101+ For more information about ` publicPath ` , please see [ the webpack documentation] ( https://webpack.js.org/guides/public-path ) ._
185102
186103### serverSideRender
187104
@@ -191,24 +108,6 @@ Default: `undefined`
191108Instructs the module to enable or disable the server-side rendering mode. Please
192109see [ Server-Side Rendering] ( #server-side-rendering ) for more information.
193110
194- ### stats
195-
196- Type: ` Object `
197- Default: ` { context: process.cwd() } `
198-
199- Options for formatting statistics displayed during and after compile. For more
200- information and property details, please see the
201- [ webpack documentation] ( https://webpack.js.org/configuration/stats/#stats ) .
202-
203- ### watchOptions
204-
205- Type: ` Object `
206- Default: ` { aggregateTimeout: 200 } `
207-
208- The module accepts an ` Object ` containing options for file watching, which is
209- passed directly to the compiler provided. For more information on watch options
210- please see the [ webpack documentation] ( https://webpack.js.org/configuration/watch/#watchoptions )
211-
212111### writeToDisk
213112
214113Type: ` Boolean|Function `
@@ -228,24 +127,45 @@ in which a return value of `false` _will not_ write the file, and a return value
228127of ` true ` _ will_ write the file to disk. eg.
229128
230129``` js
231- {
130+ const webpack = require (' webpack' );
131+ const configuration = {
132+ /* Webpack configuration */
133+ };
134+ const compiler = webpack (configuration);
135+
136+ middleware (compiler, {
232137 writeToDisk : (filePath ) => {
233138 return / superman\. css$ / .test (filePath);
234- };
235- }
139+ },
140+ });
236141```
237142
238- ### fs
143+ ### outputFileSystem
239144
240145Type: ` Object `
241- Default: ` MemoryFileSystem `
146+ Default: [ memfs] ( https://github.com/streamich/memfs )
147+
148+ Set the default file system which will be used by webpack as primary destination of generated files.
149+ This option isn't affected by the [ writeToDisk] ( #writeToDisk ) option.
242150
243- Set the default file system which will be used by webpack as primary destination of generated files. Default is set to webpack's default file system: [ memory-fs ] ( https://github.com/webpack/memory-fs ) . This option isn't affected by the [ writeToDisk ] ( #writeToDisk ) option .
151+ You have to provide ` .join() ` and ` mkdirp ` method to the ` outputFileSystem ` instance manually for compatibility with ` webpack@4 ` .
244152
245- ** Note: ** As of 3.5.x version of the middleware you have to provide ` .join() ` method to the ` fs ` instance manually. This can be done simply by using ` path.join ` :
153+ This can be done simply by using ` path.join ` :
246154
247155``` js
248- fs .join = path .join ; // no need to bind
156+ const webpack = require (' webpack' );
157+ const path = require (' path' );
158+ const myOutputFileSystem = require (' my-fs' );
159+ const mkdirp = require (' mkdirp' );
160+
161+ myOutputFileSystem .join = path .join .bind (path); // no need to bind
162+ myOutputFileSystem .mkdirp = mkdirp .bind (mkdirp); // no need to bind
163+
164+ const compiler = webpack ({
165+ /* Webpack configuration */
166+ });
167+
168+ middleware (compiler, { outputFileSystem: myOutputFileSystem });
249169```
250170
251171## API
@@ -319,7 +239,7 @@ instance.waitUntilValid(() => {
319239
320240### Multiple Successive Builds
321241
322- Watching (by means of ` lazy: false ` ) will frequently cause multiple compilations
242+ Watching will frequently cause multiple compilations
323243as the bundle changes during compilation. This is due in part to cross-platform
324244differences in file watchers, so that webpack doesn't loose file changes when
325245watched files change rapidly. If you run into this situation, please make use of
@@ -333,9 +253,9 @@ In order to develop an app using server-side rendering, we need access to the
333253[ ` stats ` ] ( https://github.com/webpack/docs/wiki/node.js-api#stats ) , which is
334254generated with each build.
335255
336- With server-side rendering enabled, ` webpack-dev-middleware ` sets the ` stat ` to
337- ` res.locals.webpackStats ` and the memory filesystem to ` res.locals.fs ` before invoking the next middleware, allowing a
338- developer to render the page body and manage the response to clients.
256+ With server-side rendering enabled, ` webpack-dev-middleware ` sets the ` stats ` to ` res.locals.webpack.devMiddleware.stats `
257+ and the filesystem to ` res.locals.webpack.devMiddleware.outputFileSystem ` before invoking the next middleware,
258+ allowing a developer to render the page body and manage the response to clients.
339259
340260_ Note: Requests for bundle files will still be handled by
341261` webpack-dev-middleware ` and all requests will be pending until the build
@@ -364,11 +284,12 @@ app.use(middleware(compiler, { serverSideRender: true }));
364284
365285// The following middleware would not be invoked until the latest build is finished.
366286app .use ((req , res ) => {
367- const assetsByChunkName = res .locals .webpackStats .toJson ().assetsByChunkName ;
368- const fs = res .locals .fs ;
369- const outputPath = res .locals .webpackStats .toJson ().outputPath ;
287+ const { devMiddleware } = res .locals .webpack ;
288+ const outputFileSystem = devMiddleware .outputFileSystem ;
289+ const jsonWebpackStats = devMiddleware .stats .toJson ();
290+ const { assetsByChunkName , outputPath } = jsonWebpackStats;
370291
371- // then use `assetsByChunkName` for server-sider rendering
292+ // Then use `assetsByChunkName` for server-side rendering
372293 // For example, if you have only one main chunk:
373294 res .send (`
374295<html>
@@ -377,7 +298,7 @@ app.use((req, res) => {
377298 <style>
378299 ${ normalizeAssets (assetsByChunkName .main )
379300 .filter ((path ) => path .endsWith (' .css' ))
380- .map ((path ) => fs .readFileSync (outputPath + ' / ' + path))
301+ .map ((path ) => outputFileSystem .readFileSync (path . join ( outputPath, path) ))
381302 .join (' \n ' )}
382303 </style>
383304 </head>
@@ -447,5 +368,4 @@ Please take a moment to read our contributing guidelines if you haven't yet done
447368[ hash-url ] : https://twitter.com/search?q=webpack
448369[ middleware-url ] : https://github.com/webpack/webpack-dev-middleware
449370[ stack-url ] : https://stackoverflow.com/questions/tagged/webpack-dev-middleware
450- [ uglify-url ] : https://github.com/webpack-contrib/uglifyjs-webpack-plugin
451371[ wjo-url ] : https://github.com/webpack/webpack.js.org
0 commit comments