@@ -49,8 +49,8 @@ function Server(compiler, options) {
49
49
// Init express server
50
50
var app = this . app = new express ( ) ;
51
51
52
- // serve webpack bundle
53
- app . use ( this . middleware = webpackDevMiddleware ( compiler , options ) ) ;
52
+ // middleware for serving webpack bundle
53
+ this . middleware = webpackDevMiddleware ( compiler , options ) ;
54
54
55
55
app . get ( "/__webpack_dev_server__/live.bundle.js" , function ( req , res ) {
56
56
res . setHeader ( "Content-Type" , "application/javascript" ) ;
@@ -109,82 +109,114 @@ function Server(compiler, options) {
109
109
res . end ( '</body></html>' ) ;
110
110
} . bind ( this ) ) ;
111
111
112
- if ( options . proxy ) {
113
- if ( ! Array . isArray ( options . proxy ) ) {
114
- options . proxy = Object . keys ( options . proxy ) . map ( function ( path ) {
115
- var proxyOptions ;
116
- if ( typeof options . proxy [ path ] === 'string' ) {
117
- proxyOptions = { path : path , target : options . proxy [ path ] } ;
118
- } else {
119
- proxyOptions = options . proxy [ path ] ;
120
- proxyOptions . path = path ;
121
- }
122
- return proxyOptions ;
123
- } ) ;
124
- }
125
- options . proxy . forEach ( function ( proxyOptions ) {
126
- proxyOptions . ws = proxyOptions . hasOwnProperty ( 'ws' ) ? proxyOptions . ws : true ;
127
- app . all ( proxyOptions . path , function ( req , res ) {
128
- if ( typeof proxyOptions . rewrite === 'function' ) proxyOptions . rewrite ( req , proxyOptions ) ;
129
- if ( proxyOptions . host ) {
130
- req . headers . host = proxyOptions . host ;
112
+ var features = {
113
+ proxy : function ( ) {
114
+ if ( options . proxy ) {
115
+ if ( ! Array . isArray ( options . proxy ) ) {
116
+ options . proxy = Object . keys ( options . proxy ) . map ( function ( path ) {
117
+ var proxyOptions ;
118
+ if ( typeof options . proxy [ path ] === 'string' ) {
119
+ proxyOptions = { path : path , target : options . proxy [ path ] } ;
120
+ } else {
121
+ proxyOptions = options . proxy [ path ] ;
122
+ proxyOptions . path = path ;
123
+ }
124
+ return proxyOptions ;
125
+ } ) ;
131
126
}
132
- proxy . web ( req , res , proxyOptions , function ( err ) {
133
- var msg = "cannot proxy to " + proxyOptions . target + " (" + err . message + ")" ;
134
- this . io . sockets . emit ( "proxy-error" , [ msg ] ) ;
135
- res . statusCode = 502 ;
136
- res . end ( ) ;
127
+ options . proxy . forEach ( function ( proxyOptions ) {
128
+ proxyOptions . ws = proxyOptions . hasOwnProperty ( 'ws' ) ? proxyOptions . ws : true ;
129
+ app . all ( proxyOptions . path , function ( req , res ) {
130
+ if ( typeof proxyOptions . rewrite === 'function' ) proxyOptions . rewrite ( req , proxyOptions ) ;
131
+ if ( proxyOptions . host ) {
132
+ req . headers . host = proxyOptions . host ;
133
+ }
134
+ proxy . web ( req , res , proxyOptions , function ( err ) {
135
+ var msg = "cannot proxy to " + proxyOptions . target + " (" + err . message + ")" ;
136
+ this . io . sockets . emit ( "proxy-error" , [ msg ] ) ;
137
+ res . statusCode = 502 ;
138
+ res . end ( ) ;
139
+ } . bind ( this ) ) ;
140
+ if ( proxyOptions . configure ) {
141
+ proxyOptions . configure ( proxy ) ;
142
+ }
143
+ } . bind ( this ) ) ;
137
144
} . bind ( this ) ) ;
138
- if ( proxyOptions . configure ) {
139
- proxyOptions . configure ( proxy ) ;
145
+ }
146
+ } . bind ( this ) ,
147
+
148
+ historyApiFallback : function ( ) {
149
+ if ( options . historyApiFallback ) {
150
+ // Fall back to /index.html if nothing else matches.
151
+ app . use ( historyApiFallback ( typeof options . historyApiFallback === 'object' ? options . historyApiFallback : null ) ) ;
152
+ }
153
+ } . bind ( this ) ,
154
+
155
+ contentBase : function ( ) {
156
+ if ( options . contentBase !== false ) {
157
+ var contentBase = options . contentBase || process . cwd ( ) ;
158
+
159
+ if ( typeof contentBase === "object" ) {
160
+ console . log ( 'Using contentBase as a proxy is deprecated and will be removed in the next major version. Please use the proxy option instead.\n\nTo update remove the contentBase option from webpack.config.js and add this:' ) ;
161
+ console . log ( 'proxy: {\n\t"*": <your current contentBase configuration>\n}' ) ;
162
+ // Proxy every request to contentBase.target
163
+ app . all ( "*" , function ( req , res ) {
164
+ proxy . web ( req , res , contentBase , function ( err ) {
165
+ var msg = "cannot proxy to " + contentBase . target + " (" + err . message + ")" ;
166
+ this . io . sockets . emit ( "proxy-error" , [ msg ] ) ;
167
+ res . statusCode = 502 ;
168
+ res . end ( ) ;
169
+ } . bind ( this ) ) ;
170
+ } . bind ( this ) ) ;
171
+ } else if ( / ^ ( h t t p s ? : ) ? \/ \/ / . test ( contentBase ) ) {
172
+ // Redirect every request to contentBase
173
+ app . get ( "*" , function ( req , res ) {
174
+ res . writeHead ( 302 , {
175
+ 'Location' : contentBase + req . path + ( req . _parsedUrl . search || "" )
176
+ } ) ;
177
+ res . end ( ) ;
178
+ } . bind ( this ) ) ;
179
+ } else if ( typeof contentBase === "number" ) {
180
+ // Redirect every request to the port contentBase
181
+ app . get ( "*" , function ( req , res ) {
182
+ res . writeHead ( 302 , {
183
+ 'Location' : "//localhost:" + contentBase + req . path + ( req . _parsedUrl . search || "" )
184
+ } ) ;
185
+ res . end ( ) ;
186
+ } . bind ( this ) ) ;
187
+ } else {
188
+ // route content request
189
+ app . get ( "*" , express . static ( contentBase ) , serveIndex ( contentBase ) ) ;
140
190
}
141
- } . bind ( this ) ) ;
142
- } . bind ( this ) ) ;
143
- }
191
+ }
192
+ } . bind ( this ) ,
144
193
145
- if ( options . historyApiFallback ) {
146
- // Fall back to /index.html if nothing else matches.
147
- app . use ( historyApiFallback ( typeof options . historyApiFallback === 'object' ? options . historyApiFallback : null ) ) ;
148
- // include our middleware to ensure it is able to handle '/index.html' requrst after redirect
149
- app . use ( this . middleware ) ;
150
- }
194
+ middleware : function ( ) {
195
+ // include our middleware to ensure it is able to handle '/index.html' requrst after redirect
196
+ app . use ( this . middleware ) ;
197
+ } . bind ( this ) ,
151
198
152
- if ( options . contentBase !== false ) {
153
- var contentBase = options . contentBase || process . cwd ( ) ;
199
+ headers : function ( ) {
200
+ app . get ( "*" , this . setContentHeaders . bind ( this ) ) ;
201
+ } . bind ( this ) ,
154
202
155
- if ( typeof contentBase === "object" ) {
156
- console . log ( 'Using contentBase as a proxy is deprecated and will be removed in the next major version. Please use the proxy option instead.\n\nTo update remove the contentBase option from webpack.config.js and add this:' ) ;
157
- console . log ( 'proxy: {\n\t"*": <your current contentBase configuration>\n}' ) ;
158
- // Proxy every request to contentBase.target
159
- app . all ( "*" , function ( req , res ) {
160
- proxy . web ( req , res , contentBase , function ( err ) {
161
- var msg = "cannot proxy to " + contentBase . target + " (" + err . message + ")" ;
162
- this . io . sockets . emit ( "proxy-error" , [ msg ] ) ;
163
- res . statusCode = 502 ;
164
- res . end ( ) ;
165
- } . bind ( this ) ) ;
166
- } . bind ( this ) ) ;
167
- } else if ( / ^ ( h t t p s ? : ) ? \/ \/ / . test ( contentBase ) ) {
168
- // Redirect every request to contentBase
169
- app . get ( "*" , function ( req , res ) {
170
- res . writeHead ( 302 , {
171
- 'Location' : contentBase + req . path + ( req . _parsedUrl . search || "" )
172
- } ) ;
173
- res . end ( ) ;
174
- } . bind ( this ) ) ;
175
- } else if ( typeof contentBase === "number" ) {
176
- // Redirect every request to the port contentBase
177
- app . get ( "*" , function ( req , res ) {
178
- res . writeHead ( 302 , {
179
- 'Location' : "//localhost:" + contentBase + req . path + ( req . _parsedUrl . search || "" )
180
- } ) ;
181
- res . end ( ) ;
182
- } . bind ( this ) ) ;
183
- } else {
184
- // route content request
185
- app . get ( "*" , this . setContentHeaders . bind ( this ) , this . serveMagicHtml . bind ( this ) , express . static ( contentBase ) , serveIndex ( contentBase ) ) ;
186
- }
187
- }
203
+ magicHtml : function ( ) {
204
+ app . get ( "*" , this . serveMagicHtml . bind ( this ) ) ;
205
+ } . bind ( this )
206
+ } ;
207
+
208
+ var defaultFeatures = [ "headers" , "middleware" ] ;
209
+ if ( options . proxy )
210
+ defaultFeatures . push ( "proxy" ) ;
211
+ if ( options . historyApiFallback )
212
+ defaultFeatures . push ( "historyApiFallback" , "middleware" ) ;
213
+ defaultFeatures . push ( "magicHtml" ) ;
214
+ if ( options . contentBase !== false )
215
+ defaultFeatures . push ( "contentBase" ) ;
216
+
217
+ ( options . features || defaultFeatures ) . forEach ( function ( feature ) {
218
+ features [ feature ] ( ) ;
219
+ } , this ) ;
188
220
189
221
this . listeningApp = options . https
190
222
? https . createServer ( {
0 commit comments