@@ -155,7 +155,10 @@ function getServerConfig(
155
155
* Private method to enhance a webpack config with Proxy configuration.
156
156
* @private
157
157
*/
158
- async function addProxyConfig ( root : string , proxyConfig : string | undefined ) {
158
+ async function addProxyConfig (
159
+ root : string ,
160
+ proxyConfig : string | undefined ,
161
+ ) : Promise < object [ ] | undefined > {
159
162
if ( ! proxyConfig ) {
160
163
return undefined ;
161
164
}
@@ -166,13 +169,15 @@ async function addProxyConfig(root: string, proxyConfig: string | undefined) {
166
169
throw new Error ( `Proxy configuration file ${ proxyPath } does not exist.` ) ;
167
170
}
168
171
172
+ let proxyConfiguration : Record < string , object > | object [ ] ;
173
+
169
174
switch ( extname ( proxyPath ) ) {
170
175
case '.json' : {
171
176
const content = await fsPromises . readFile ( proxyPath , 'utf-8' ) ;
172
177
173
178
const { parse, printParseErrorCode } = await import ( 'jsonc-parser' ) ;
174
179
const parseErrors : import ( 'jsonc-parser' ) . ParseError [ ] = [ ] ;
175
- const proxyConfiguration = parse ( content , parseErrors , { allowTrailingComma : true } ) ;
180
+ proxyConfiguration = parse ( content , parseErrors , { allowTrailingComma : true } ) ;
176
181
177
182
if ( parseErrors . length > 0 ) {
178
183
let errorMessage = `Proxy configuration file ${ proxyPath } contains parse errors:` ;
@@ -183,32 +188,44 @@ async function addProxyConfig(root: string, proxyConfig: string | undefined) {
183
188
throw new Error ( errorMessage ) ;
184
189
}
185
190
186
- return proxyConfiguration ;
191
+ break ;
187
192
}
188
193
case '.mjs' :
189
194
// Load the ESM configuration file using the TypeScript dynamic import workaround.
190
195
// Once TypeScript provides support for keeping the dynamic import this workaround can be
191
196
// changed to a direct dynamic import.
192
- return ( await loadEsmModule < { default : unknown } > ( pathToFileURL ( proxyPath ) ) ) . default ;
197
+ proxyConfiguration = (
198
+ await loadEsmModule < { default : Record < string , object > | object [ ] } > (
199
+ pathToFileURL ( proxyPath ) ,
200
+ )
201
+ ) . default ;
202
+ break ;
193
203
case '.cjs' :
194
- return require ( proxyPath ) ;
204
+ proxyConfiguration = require ( proxyPath ) ;
205
+ break ;
195
206
default :
196
207
// The file could be either CommonJS or ESM.
197
208
// CommonJS is tried first then ESM if loading fails.
198
209
try {
199
- return require ( proxyPath ) ;
210
+ proxyConfiguration = require ( proxyPath ) ;
200
211
} catch ( e ) {
201
212
assertIsError ( e ) ;
202
- if ( e . code === 'ERR_REQUIRE_ESM' ) {
203
- // Load the ESM configuration file using the TypeScript dynamic import workaround.
204
- // Once TypeScript provides support for keeping the dynamic import this workaround can be
205
- // changed to a direct dynamic import.
206
- return ( await loadEsmModule < { default : unknown } > ( pathToFileURL ( proxyPath ) ) ) . default ;
213
+ if ( e . code !== 'ERR_REQUIRE_ESM' ) {
214
+ throw e ;
207
215
}
208
216
209
- throw e ;
217
+ // Load the ESM configuration file using the TypeScript dynamic import workaround.
218
+ // Once TypeScript provides support for keeping the dynamic import this workaround can be
219
+ // changed to a direct dynamic import.
220
+ proxyConfiguration = (
221
+ await loadEsmModule < { default : Record < string , object > | object [ ] } > (
222
+ pathToFileURL ( proxyPath ) ,
223
+ )
224
+ ) . default ;
210
225
}
211
226
}
227
+
228
+ return normalizeProxyConfiguration ( proxyConfiguration ) ;
212
229
}
213
230
214
231
/**
@@ -333,3 +350,9 @@ function getPublicHostOptions(options: WebpackDevServerOptions, webSocketPath: s
333
350
334
351
return `auto://${ publicHost || '0.0.0.0:0' } ${ webSocketPath } ` ;
335
352
}
353
+
354
+ function normalizeProxyConfiguration ( proxy : Record < string , object > | object [ ] ) : object [ ] {
355
+ return Array . isArray ( proxy )
356
+ ? proxy
357
+ : Object . entries ( proxy ) . map ( ( [ context , value ] ) => ( { context : [ context ] , ...value } ) ) ;
358
+ }
0 commit comments