@@ -162,26 +162,53 @@ function Server(compiler, options) {
162
162
} ) ;
163
163
}
164
164
165
+ var getProxyMiddleware = function ( proxyConfig ) {
166
+ var context = proxyConfig . context || proxyConfig . path ;
167
+
168
+ // It is possible to use the `bypass` method without a `target`.
169
+ // However, the proxy middleware has no use in this case, and will fail to instantiate.
170
+ if ( proxyConfig . target ) {
171
+ return httpProxyMiddleware ( context , proxyConfig ) ;
172
+ }
173
+ }
174
+
165
175
/**
166
176
* Assume a proxy configuration specified as:
167
177
* proxy: [
168
178
* {
169
179
* context: ...,
170
180
* ...options...
171
- * }
181
+ * },
182
+ * // or:
183
+ * function() {
184
+ * return {
185
+ * context: ...,
186
+ * ...options...
187
+ * };
188
+ * }
172
189
* ]
173
190
*/
174
- options . proxy . forEach ( function ( proxyConfig ) {
175
- var bypass = typeof proxyConfig . bypass === 'function' ;
176
- var context = proxyConfig . context || proxyConfig . path ;
191
+ options . proxy . forEach ( function ( proxyConfigOrCallback ) {
192
+ var proxyConfig ;
177
193
var proxyMiddleware ;
178
- // It is possible to use the `bypass` method without a `target`.
179
- // However, the proxy middleware has no use in this case, and will fail to instantiate.
180
- if ( proxyConfig . target ) {
181
- proxyMiddleware = httpProxyMiddleware ( context , proxyConfig ) ;
194
+
195
+ if ( typeof proxyConfigOrCallback === 'function' ) {
196
+ proxyConfig = proxyConfigOrCallback ( ) ;
197
+ } else {
198
+ proxyConfig = proxyConfigOrCallback ;
182
199
}
183
200
201
+ proxyMiddleware = getProxyMiddleware ( proxyConfig ) ;
202
+
184
203
app . use ( function ( req , res , next ) {
204
+ if ( typeof proxyConfigOrCallback === 'function' ) {
205
+ var newProxyConfig = proxyConfigOrCallback ( ) ;
206
+ if ( newProxyConfig !== proxyConfig ) {
207
+ proxyConfig = newProxyConfig ;
208
+ proxyMiddleware = getProxyMiddleware ( proxyConfig ) ;
209
+ }
210
+ }
211
+ var bypass = typeof proxyConfig . bypass === 'function' ;
185
212
var bypassUrl = bypass && proxyConfig . bypass ( req , res , proxyConfig ) || false ;
186
213
187
214
if ( bypassUrl ) {
0 commit comments