Skip to content

Commit 7389612

Browse files
authored
refactor: move proxy normalization (#3359)
* fix: move proxy normalization * fix: move proxy normalization
1 parent cde0fdd commit 7389612

File tree

2 files changed

+69
-66
lines changed

2 files changed

+69
-66
lines changed

lib/Server.js

-66
Original file line numberDiff line numberDiff line change
@@ -170,72 +170,6 @@ class Server {
170170
setupProxyFeature() {
171171
const { createProxyMiddleware } = require('http-proxy-middleware');
172172

173-
/**
174-
* Assume a proxy configuration specified as:
175-
* proxy: {
176-
* 'context': { options }
177-
* }
178-
* OR
179-
* proxy: {
180-
* 'context': 'target'
181-
* }
182-
*/
183-
if (!Array.isArray(this.options.proxy)) {
184-
if (Object.prototype.hasOwnProperty.call(this.options.proxy, 'target')) {
185-
this.options.proxy = [this.options.proxy];
186-
} else {
187-
this.options.proxy = Object.keys(this.options.proxy).map((context) => {
188-
let proxyOptions;
189-
// For backwards compatibility reasons.
190-
const correctedContext = context
191-
.replace(/^\*$/, '**')
192-
.replace(/\/\*$/, '');
193-
194-
if (typeof this.options.proxy[context] === 'string') {
195-
proxyOptions = {
196-
context: correctedContext,
197-
target: this.options.proxy[context],
198-
};
199-
} else {
200-
proxyOptions = Object.assign({}, this.options.proxy[context]);
201-
proxyOptions.context = correctedContext;
202-
}
203-
204-
const getLogLevelForProxy = (level) => {
205-
if (level === 'none') {
206-
return 'silent';
207-
}
208-
209-
if (level === 'log') {
210-
return 'info';
211-
}
212-
213-
if (level === 'verbose') {
214-
return 'debug';
215-
}
216-
217-
return level;
218-
};
219-
220-
const configs = getCompilerConfigArray(this.compiler);
221-
const configWithDevServer =
222-
configs.find((config) => config.devServer) || configs[0];
223-
224-
if (typeof proxyOptions.logLevel === 'undefined') {
225-
proxyOptions.logLevel = getLogLevelForProxy(
226-
configWithDevServer.infrastructureLogging.level
227-
);
228-
}
229-
230-
if (typeof proxyOptions.logProvider === 'undefined') {
231-
proxyOptions.logProvider = () => this.logger;
232-
}
233-
234-
return proxyOptions;
235-
});
236-
}
237-
}
238-
239173
const getProxyMiddleware = (proxyConfig) => {
240174
const context = proxyConfig.context || proxyConfig.path;
241175

lib/utils/normalizeOptions.js

+69
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ function normalizeOptions(compiler, options, logger) {
157157
};
158158
}
159159

160+
// https option
160161
if (options.https) {
161162
const getCertificate = require('./getCertificate');
162163

@@ -189,6 +190,74 @@ function normalizeOptions(compiler, options, logger) {
189190
options.https.key = options.https.key || fakeCert;
190191
options.https.cert = options.https.cert || fakeCert;
191192
}
193+
194+
/**
195+
* Assume a proxy configuration specified as:
196+
* proxy: {
197+
* 'context': { options }
198+
* }
199+
* OR
200+
* proxy: {
201+
* 'context': 'target'
202+
* }
203+
*/
204+
if (typeof options.proxy !== 'undefined') {
205+
if (!Array.isArray(options.proxy)) {
206+
if (Object.prototype.hasOwnProperty.call(options.proxy, 'target')) {
207+
options.proxy = [options.proxy];
208+
} else {
209+
options.proxy = Object.keys(options.proxy).map((context) => {
210+
let proxyOptions;
211+
// For backwards compatibility reasons.
212+
const correctedContext = context
213+
.replace(/^\*$/, '**')
214+
.replace(/\/\*$/, '');
215+
216+
if (typeof options.proxy[context] === 'string') {
217+
proxyOptions = {
218+
context: correctedContext,
219+
target: options.proxy[context],
220+
};
221+
} else {
222+
proxyOptions = Object.assign({}, options.proxy[context]);
223+
proxyOptions.context = correctedContext;
224+
}
225+
226+
const getLogLevelForProxy = (level) => {
227+
if (level === 'none') {
228+
return 'silent';
229+
}
230+
231+
if (level === 'log') {
232+
return 'info';
233+
}
234+
235+
if (level === 'verbose') {
236+
return 'debug';
237+
}
238+
239+
return level;
240+
};
241+
242+
const configs = getCompilerConfigArray(compiler);
243+
const configWithDevServer =
244+
configs.find((config) => config.devServer) || configs[0];
245+
246+
if (typeof proxyOptions.logLevel === 'undefined') {
247+
proxyOptions.logLevel = getLogLevelForProxy(
248+
configWithDevServer.infrastructureLogging.level
249+
);
250+
}
251+
252+
if (typeof proxyOptions.logProvider === 'undefined') {
253+
proxyOptions.logProvider = () => logger;
254+
}
255+
256+
return proxyOptions;
257+
});
258+
}
259+
}
260+
}
192261
}
193262

194263
module.exports = normalizeOptions;

0 commit comments

Comments
 (0)