@@ -25,11 +25,6 @@ Some of the benefits of using this middleware include:
25
25
has completed.
26
26
- Supports hot module reload (HMR).
27
27
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
-
33
28
## Getting Started
34
29
35
30
First thing's first, install the module:
@@ -62,10 +57,7 @@ app.listen(3000, () => console.log('Example app listening on port 3000!'));
62
57
63
58
## Options
64
59
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.
69
61
70
62
### methods
71
63
@@ -79,109 +71,34 @@ This property allows a user to pass the list of HTTP request methods accepted by
79
71
Type: ` Object `
80
72
Default: ` undefined `
81
73
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" } `
84
76
85
77
### index
86
78
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 `
137
81
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.
150
83
151
84
### mimeTypes
152
85
153
86
Type: ` Object `
154
- Default: ` null `
87
+ Default: ` undefined `
155
88
156
89
This property allows a user to register custom mime types or extension mappings.
157
- eg. ` mimeTypes: { 'text/html': [ 'phtml' ] } ` .
90
+ eg. ` mimeTypes: { phtml: 'text/html' } ` .
158
91
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.
166
93
167
94
### publicPath
168
95
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 `
176
98
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 ) ._
185
102
186
103
### serverSideRender
187
104
@@ -191,24 +108,6 @@ Default: `undefined`
191
108
Instructs the module to enable or disable the server-side rendering mode. Please
192
109
see [ Server-Side Rendering] ( #server-side-rendering ) for more information.
193
110
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
-
212
111
### writeToDisk
213
112
214
113
Type: ` Boolean|Function `
@@ -228,24 +127,45 @@ in which a return value of `false` _will not_ write the file, and a return value
228
127
of ` true ` _ will_ write the file to disk. eg.
229
128
230
129
``` js
231
- {
130
+ const webpack = require (' webpack' );
131
+ const configuration = {
132
+ /* Webpack configuration */
133
+ };
134
+ const compiler = webpack (configuration);
135
+
136
+ middleware (compiler, {
232
137
writeToDisk : (filePath ) => {
233
138
return / superman\. css$ / .test (filePath);
234
- };
235
- }
139
+ },
140
+ });
236
141
```
237
142
238
- ### fs
143
+ ### outputFileSystem
239
144
240
145
Type: ` 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.
242
150
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 ` .
244
152
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 ` :
246
154
247
155
``` 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 });
249
169
```
250
170
251
171
## API
@@ -319,7 +239,7 @@ instance.waitUntilValid(() => {
319
239
320
240
### Multiple Successive Builds
321
241
322
- Watching (by means of ` lazy: false ` ) will frequently cause multiple compilations
242
+ Watching will frequently cause multiple compilations
323
243
as the bundle changes during compilation. This is due in part to cross-platform
324
244
differences in file watchers, so that webpack doesn't loose file changes when
325
245
watched 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
333
253
[ ` stats ` ] ( https://github.com/webpack/docs/wiki/node.js-api#stats ) , which is
334
254
generated with each build.
335
255
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.
339
259
340
260
_ Note: Requests for bundle files will still be handled by
341
261
` webpack-dev-middleware ` and all requests will be pending until the build
@@ -364,11 +284,12 @@ app.use(middleware(compiler, { serverSideRender: true }));
364
284
365
285
// The following middleware would not be invoked until the latest build is finished.
366
286
app .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;
370
291
371
- // then use `assetsByChunkName` for server-sider rendering
292
+ // Then use `assetsByChunkName` for server-side rendering
372
293
// For example, if you have only one main chunk:
373
294
res .send (`
374
295
<html>
@@ -377,7 +298,7 @@ app.use((req, res) => {
377
298
<style>
378
299
${ normalizeAssets (assetsByChunkName .main )
379
300
.filter ((path ) => path .endsWith (' .css' ))
380
- .map ((path ) => fs .readFileSync (outputPath + ' / ' + path))
301
+ .map ((path ) => outputFileSystem .readFileSync (path . join ( outputPath, path) ))
381
302
.join (' \n ' )}
382
303
</style>
383
304
</head>
@@ -447,5 +368,4 @@ Please take a moment to read our contributing guidelines if you haven't yet done
447
368
[ hash-url ] : https://twitter.com/search?q=webpack
448
369
[ middleware-url ] : https://github.com/webpack/webpack-dev-middleware
449
370
[ stack-url ] : https://stackoverflow.com/questions/tagged/webpack-dev-middleware
450
- [ uglify-url ] : https://github.com/webpack-contrib/uglifyjs-webpack-plugin
451
371
[ wjo-url ] : https://github.com/webpack/webpack.js.org
0 commit comments