1
- var fs = require ( "fs" ) ;
2
- var chokidar = require ( "chokidar" ) ;
3
- var path = require ( "path" ) ;
4
- var webpackDevMiddleware = require ( "webpack-dev-middleware" ) ;
5
- var express = require ( "express" ) ;
6
- var compress = require ( "compression" ) ;
7
- var sockjs = require ( "sockjs" ) ;
8
- var http = require ( "http" ) ;
9
- var spdy = require ( "spdy" ) ;
10
- var httpProxyMiddleware = require ( "http-proxy-middleware" ) ;
11
- var serveIndex = require ( "serve-index" ) ;
12
- var historyApiFallback = require ( "connect-history-api-fallback" ) ;
13
- var webpack = require ( "webpack" ) ;
14
- var OptionsValidationError = require ( "./OptionsValidationError" ) ;
15
- var optionsSchema = require ( "./optionsSchema.json" ) ;
16
-
17
- var clientStats = { errorDetails : false } ;
1
+ "use strict" ;
2
+
3
+ const fs = require ( "fs" ) ;
4
+ const chokidar = require ( "chokidar" ) ;
5
+ const path = require ( "path" ) ;
6
+ const webpackDevMiddleware = require ( "webpack-dev-middleware" ) ;
7
+ const express = require ( "express" ) ;
8
+ const compress = require ( "compression" ) ;
9
+ const sockjs = require ( "sockjs" ) ;
10
+ const http = require ( "http" ) ;
11
+ const spdy = require ( "spdy" ) ;
12
+ const httpProxyMiddleware = require ( "http-proxy-middleware" ) ;
13
+ const serveIndex = require ( "serve-index" ) ;
14
+ const historyApiFallback = require ( "connect-history-api-fallback" ) ;
15
+ const webpack = require ( "webpack" ) ;
16
+ const OptionsValidationError = require ( "./OptionsValidationError" ) ;
17
+ const optionsSchema = require ( "./optionsSchema.json" ) ;
18
+
19
+ const clientStats = { errorDetails : false } ;
18
20
19
21
function Server ( compiler , options ) {
20
22
// Default options
21
23
if ( ! options ) options = { } ;
22
24
23
- var validationErrors = webpack . validateSchema ( optionsSchema , options ) ;
25
+ const validationErrors = webpack . validateSchema ( optionsSchema , options ) ;
24
26
if ( validationErrors . length ) {
25
27
throw new OptionsValidationError ( validationErrors ) ;
26
28
}
@@ -36,7 +38,7 @@ function Server(compiler, options) {
36
38
this . contentBaseWatchers = [ ] ;
37
39
38
40
// Listening for events
39
- var invalidPlugin = function ( ) {
41
+ const invalidPlugin = function ( ) {
40
42
this . sockWrite ( this . sockets , "invalid" ) ;
41
43
} . bind ( this ) ;
42
44
compiler . plugin ( "compile" , invalidPlugin ) ;
@@ -47,7 +49,7 @@ function Server(compiler, options) {
47
49
} . bind ( this ) ) ;
48
50
49
51
// Init express server
50
- var app = this . app = new express ( ) ;
52
+ const app = this . app = new express ( ) ;
51
53
52
54
// middleware for serving webpack bundle
53
55
this . middleware = webpackDevMiddleware ( compiler , options ) ;
@@ -76,22 +78,22 @@ function Server(compiler, options) {
76
78
res . setHeader ( "Content-Type" , "text/html" ) ;
77
79
/* eslint-disable quotes */
78
80
res . write ( '<!DOCTYPE html><html><head><meta charset="utf-8"/></head><body>' ) ;
79
- var path = this . middleware . getFilenameFromUrl ( options . publicPath || "/" ) ;
80
- var fs = this . middleware . fileSystem ;
81
+ const path = this . middleware . getFilenameFromUrl ( options . publicPath || "/" ) ;
82
+ const fs = this . middleware . fileSystem ;
81
83
82
84
function writeDirectory ( baseUrl , basePath ) {
83
- var content = fs . readdirSync ( basePath ) ;
85
+ const content = fs . readdirSync ( basePath ) ;
84
86
res . write ( "<ul>" ) ;
85
87
content . forEach ( function ( item ) {
86
- var p = basePath + "/" + item ;
88
+ const p = ` ${ basePath } / ${ item } ` ;
87
89
if ( fs . statSync ( p ) . isFile ( ) ) {
88
90
res . write ( '<li><a href="' ) ;
89
91
res . write ( baseUrl + item ) ;
90
92
res . write ( '">' ) ;
91
93
res . write ( item ) ;
92
94
res . write ( '</a></li>' ) ;
93
95
if ( / \. j s $ / . test ( item ) ) {
94
- var htmlItem = item . substr ( 0 , item . length - 3 ) ;
96
+ const htmlItem = item . substr ( 0 , item . length - 3 ) ;
95
97
res . write ( '<li><a href="' ) ;
96
98
res . write ( baseUrl + htmlItem ) ;
97
99
res . write ( '">' ) ;
@@ -106,7 +108,7 @@ function Server(compiler, options) {
106
108
res . write ( '<li>' ) ;
107
109
res . write ( item ) ;
108
110
res . write ( '<br>' ) ;
109
- writeDirectory ( baseUrl + item + "/" , p ) ;
111
+ writeDirectory ( ` ${ baseUrl + item } /` , p ) ;
110
112
res . write ( '</li>' ) ;
111
113
}
112
114
} ) ;
@@ -117,14 +119,14 @@ function Server(compiler, options) {
117
119
res . end ( "</body></html>" ) ;
118
120
} . bind ( this ) ) ;
119
121
120
- var contentBase ;
122
+ let contentBase ;
121
123
if ( options . contentBase !== undefined ) {
122
124
contentBase = options . contentBase ;
123
125
} else {
124
126
contentBase = process . cwd ( ) ;
125
127
}
126
128
127
- var features = {
129
+ const features = {
128
130
compress : function ( ) {
129
131
if ( options . compress ) {
130
132
// Enable gzip compression.
@@ -146,9 +148,9 @@ function Server(compiler, options) {
146
148
*/
147
149
if ( ! Array . isArray ( options . proxy ) ) {
148
150
options . proxy = Object . keys ( options . proxy ) . map ( function ( context ) {
149
- var proxyOptions ;
151
+ let proxyOptions ;
150
152
// For backwards compatibility reasons.
151
- var correctedContext = context . replace ( / ^ \* $ / , "**" ) . replace ( / \/ \* $ / , "" ) ;
153
+ const correctedContext = context . replace ( / ^ \* $ / , "**" ) . replace ( / \/ \* $ / , "" ) ;
152
154
153
155
if ( typeof options . proxy [ context ] === "string" ) {
154
156
proxyOptions = {
@@ -165,8 +167,8 @@ function Server(compiler, options) {
165
167
} ) ;
166
168
}
167
169
168
- var getProxyMiddleware = function ( proxyConfig ) {
169
- var context = proxyConfig . context || proxyConfig . path ;
170
+ const getProxyMiddleware = function ( proxyConfig ) {
171
+ const context = proxyConfig . context || proxyConfig . path ;
170
172
171
173
// It is possible to use the `bypass` method without a `target`.
172
174
// However, the proxy middleware has no use in this case, and will fail to instantiate.
@@ -192,8 +194,8 @@ function Server(compiler, options) {
192
194
* ]
193
195
*/
194
196
options . proxy . forEach ( function ( proxyConfigOrCallback ) {
195
- var proxyConfig ;
196
- var proxyMiddleware ;
197
+ let proxyConfig ;
198
+ let proxyMiddleware ;
197
199
198
200
if ( typeof proxyConfigOrCallback === "function" ) {
199
201
proxyConfig = proxyConfigOrCallback ( ) ;
@@ -205,14 +207,14 @@ function Server(compiler, options) {
205
207
206
208
app . use ( function ( req , res , next ) {
207
209
if ( typeof proxyConfigOrCallback === "function" ) {
208
- var newProxyConfig = proxyConfigOrCallback ( ) ;
210
+ const newProxyConfig = proxyConfigOrCallback ( ) ;
209
211
if ( newProxyConfig !== proxyConfig ) {
210
212
proxyConfig = newProxyConfig ;
211
213
proxyMiddleware = getProxyMiddleware ( proxyConfig ) ;
212
214
}
213
215
}
214
- var bypass = typeof proxyConfig . bypass === "function" ;
215
- var bypassUrl = bypass && proxyConfig . bypass ( req , res , proxyConfig ) || false ;
216
+ const bypass = typeof proxyConfig . bypass === "function" ;
217
+ const bypassUrl = bypass && proxyConfig . bypass ( req , res , proxyConfig ) || false ;
216
218
217
219
if ( bypassUrl ) {
218
220
req . url = bypassUrl ;
@@ -257,7 +259,7 @@ function Server(compiler, options) {
257
259
// Redirect every request to the port contentBase
258
260
app . get ( "*" , function ( req , res ) {
259
261
res . writeHead ( 302 , {
260
- "Location" : " //localhost:" + contentBase + req . path + ( req . _parsedUrl . search || "" )
262
+ "Location" : ` //localhost:${ contentBase } ${ req . path } ${ req . _parsedUrl . search || "" } `
261
263
} ) ;
262
264
res . end ( ) ;
263
265
} ) ;
@@ -308,7 +310,7 @@ function Server(compiler, options) {
308
310
} . bind ( this )
309
311
} ;
310
312
311
- var defaultFeatures = [ "setup" , "headers" , "middleware" ] ;
313
+ const defaultFeatures = [ "setup" , "headers" , "middleware" ] ;
312
314
if ( options . proxy )
313
315
defaultFeatures . push ( "proxy" , "middleware" ) ;
314
316
if ( contentBase !== false )
@@ -341,7 +343,7 @@ function Server(compiler, options) {
341
343
}
342
344
343
345
// Use built-in self-signed certificate if no certificate was configured
344
- var fakeCert = fs . readFileSync ( path . join ( __dirname , "../ssl/server.pem" ) ) ;
346
+ const fakeCert = fs . readFileSync ( path . join ( __dirname , "../ssl/server.pem" ) ) ;
345
347
options . https . key = options . https . key || fakeCert ;
346
348
options . https . cert = options . https . cert || fakeCert ;
347
349
@@ -363,7 +365,7 @@ Server.prototype.use = function() {
363
365
364
366
Server . prototype . setContentHeaders = function ( req , res , next ) {
365
367
if ( this . headers ) {
366
- for ( var name in this . headers ) {
368
+ for ( const name in this . headers ) {
367
369
res . setHeader ( name , this . headers [ name ] ) ;
368
370
}
369
371
}
@@ -373,8 +375,8 @@ Server.prototype.setContentHeaders = function(req, res, next) {
373
375
374
376
// delegate listen call and init sockjs
375
377
Server . prototype . listen = function ( ) {
376
- var returnValue = this . listeningApp . listen . apply ( this . listeningApp , arguments ) ;
377
- var sockServer = sockjs . createServer ( {
378
+ const returnValue = this . listeningApp . listen . apply ( this . listeningApp , arguments ) ;
379
+ const sockServer = sockjs . createServer ( {
378
380
// Use provided up-to-date sockjs-client
379
381
sockjs_url : "/__webpack_dev_server__/sockjs.bundle.js" ,
380
382
// Limit useless logs
@@ -389,7 +391,7 @@ Server.prototype.listen = function() {
389
391
this . sockets . push ( conn ) ;
390
392
391
393
conn . on ( "close" , function ( ) {
392
- var connIndex = this . sockets . indexOf ( conn ) ;
394
+ const connIndex = this . sockets . indexOf ( conn ) ;
393
395
if ( connIndex >= 0 ) {
394
396
this . sockets . splice ( connIndex , 1 ) ;
395
397
}
@@ -435,9 +437,9 @@ Server.prototype.sockWrite = function(sockets, type, data) {
435
437
}
436
438
437
439
Server . prototype . serveMagicHtml = function ( req , res , next ) {
438
- var _path = req . path ;
440
+ const _path = req . path ;
439
441
try {
440
- if ( ! this . middleware . fileSystem . statSync ( this . middleware . getFilenameFromUrl ( _path + " .js" ) ) . isFile ( ) )
442
+ if ( ! this . middleware . fileSystem . statSync ( this . middleware . getFilenameFromUrl ( ` ${ _path } .js` ) ) . isFile ( ) )
441
443
return next ( ) ;
442
444
// Serve a page that executes the javascript
443
445
/* eslint-disable quotes */
@@ -473,7 +475,7 @@ Server.prototype._sendStats = function(sockets, stats, force) {
473
475
}
474
476
475
477
Server . prototype . _watch = function ( path ) {
476
- var watcher = chokidar . watch ( path ) . on ( "change" , function ( ) {
478
+ const watcher = chokidar . watch ( path ) . on ( "change" , function ( ) {
477
479
this . sockWrite ( this . sockets , "content-changed" ) ;
478
480
} . bind ( this ) )
479
481
0 commit comments