Skip to content

Commit 3a66953

Browse files
committed
fix(@angular-devkit/build-angular): do not process regular expressions in proxy config when using Vite
This commit enables proxies to have a RegExp as context when using Vite. See: https://vitejs.dev/config/server-options#server-proxy Closes angular#26970
1 parent 910531a commit 3a66953

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

packages/angular_devkit/build_angular/src/builders/dev-server/tests/options/proxy-config_spec.ts

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
*/
88

99
import * as http from 'http';
10+
import { JasmineBuilderHarness } from '../../../../testing/jasmine-helpers';
1011
import { executeDevServer } from '../../index';
1112
import { executeOnceAndFetch } from '../execute-fetch';
1213
import { describeServeBuilder } from '../jasmine-helpers';
1314
import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup';
1415

15-
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => {
16+
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget, isVite) => {
1617
describe('option: "proxyConfig"', () => {
1718
beforeEach(async () => {
1819
setupTarget(harness);
@@ -232,9 +233,75 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
232233
}),
233234
);
234235
});
236+
237+
/**
238+
* ****************************************************************************************************
239+
* ********************************** Below only Vite specific tests **********************************
240+
* ****************************************************************************************************
241+
*/
242+
if (isVite) {
243+
viteOnlyTests(harness);
244+
}
235245
});
236246
});
237247

248+
/**
249+
* Vite specific tests
250+
*/
251+
function viteOnlyTests(harness: JasmineBuilderHarness<unknown>): void {
252+
it('proxies support regexp as context', async () => {
253+
harness.useTarget('serve', {
254+
...BASE_OPTIONS,
255+
proxyConfig: 'proxy.config.json',
256+
});
257+
258+
const proxyServer = createProxyServer();
259+
try {
260+
await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve));
261+
const proxyAddress = proxyServer.address() as import('net').AddressInfo;
262+
263+
await harness.writeFiles({
264+
'proxy.config.json': `
265+
{ "^/api/.*": { "target": "http://127.0.0.1:${proxyAddress.port}" } }
266+
`,
267+
});
268+
269+
const { result, response } = await executeOnceAndFetch(harness, '/api/test');
270+
271+
expect(result?.success).toBeTrue();
272+
expect(await response?.text()).toContain('TEST_API_RETURN');
273+
} finally {
274+
await new Promise<void>((resolve) => proxyServer.close(() => resolve()));
275+
}
276+
});
277+
278+
it('proxies support negated regexp as context', async () => {
279+
harness.useTarget('serve', {
280+
...BASE_OPTIONS,
281+
proxyConfig: 'proxy.config.json',
282+
});
283+
284+
const proxyServer = createProxyServer();
285+
try {
286+
await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve));
287+
const proxyAddress = proxyServer.address() as import('net').AddressInfo;
288+
289+
await harness.writeFiles({
290+
'proxy.config.json': `
291+
{ "^\\/(?!something).*": { "target": "http://127.0.0.1:${proxyAddress.port}" } }
292+
`,
293+
});
294+
295+
const { result, response } = await executeOnceAndFetch(harness, '/api/test');
296+
297+
expect(result?.success).toBeTrue();
298+
expect(await response?.text()).toContain('TEST_API_RETURN');
299+
} finally {
300+
await new Promise<void>((resolve) => proxyServer.close(() => resolve()));
301+
}
302+
});
303+
}
304+
238305
/**
239306
* Creates an HTTP Server used for proxy testing that provides a `/test` endpoint
240307
* that returns a 200 response with a body of `TEST_API_RETURN`. All other requests

packages/angular_devkit/build_angular/src/utils/load-proxy-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function normalizeProxyConfiguration(
128128

129129
// TODO: Consider upstreaming glob support
130130
for (const key of Object.keys(normalizedProxy)) {
131-
if (isDynamicPattern(key)) {
131+
if (key[0] !== '^' && isDynamicPattern(key)) {
132132
const { output } = parseGlob(key);
133133
normalizedProxy[`^${output}$`] = normalizedProxy[key];
134134
delete normalizedProxy[key];

0 commit comments

Comments
 (0)