Skip to content

Commit 6a1649a

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 #26970
1 parent 910531a commit 6a1649a

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

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

+57-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { executeOnceAndFetch } from '../execute-fetch';
1212
import { describeServeBuilder } from '../jasmine-helpers';
1313
import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup';
1414

15-
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => {
15+
describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget, isVite) => {
1616
describe('option: "proxyConfig"', () => {
1717
beforeEach(async () => {
1818
setupTarget(harness);
@@ -232,6 +232,62 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
232232
}),
233233
);
234234
});
235+
236+
if (isVite) {
237+
describe('when using Vite', () => {
238+
it('proxies support regexp as context', async () => {
239+
harness.useTarget('serve', {
240+
...BASE_OPTIONS,
241+
proxyConfig: 'proxy.config.json',
242+
});
243+
244+
const proxyServer = createProxyServer();
245+
try {
246+
await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve));
247+
const proxyAddress = proxyServer.address() as import('net').AddressInfo;
248+
249+
await harness.writeFiles({
250+
'proxy.config.json': `
251+
{ "^/api/.*": { "target": "http://127.0.0.1:${proxyAddress.port}" } }
252+
`,
253+
});
254+
255+
const { result, response } = await executeOnceAndFetch(harness, '/api/test');
256+
257+
expect(result?.success).toBeTrue();
258+
expect(await response?.text()).toContain('TEST_API_RETURN');
259+
} finally {
260+
await new Promise<void>((resolve) => proxyServer.close(() => resolve()));
261+
}
262+
});
263+
264+
it('proxies support negated regexp as context', async () => {
265+
harness.useTarget('serve', {
266+
...BASE_OPTIONS,
267+
proxyConfig: 'proxy.config.json',
268+
});
269+
270+
const proxyServer = createProxyServer();
271+
try {
272+
await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve));
273+
const proxyAddress = proxyServer.address() as import('net').AddressInfo;
274+
275+
await harness.writeFiles({
276+
'proxy.config.json': `
277+
{ "^\\/(?!something).*": { "target": "http://127.0.0.1:${proxyAddress.port}" } }
278+
`,
279+
});
280+
281+
const { result, response } = await executeOnceAndFetch(harness, '/api/test');
282+
283+
expect(result?.success).toBeTrue();
284+
expect(await response?.text()).toContain('TEST_API_RETURN');
285+
} finally {
286+
await new Promise<void>((resolve) => proxyServer.close(() => resolve()));
287+
}
288+
});
289+
});
290+
}
235291
});
236292
});
237293

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

+1-1
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)