Skip to content

Commit 894dd13

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 894dd13

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

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

Lines changed: 64 additions & 1 deletion
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,69 @@ describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupT
232232
}),
233233
);
234234
});
235+
236+
/**
237+
* ****************************************************************************************************
238+
* ********************************** Below only Vite specific tests **********************************
239+
* ****************************************************************************************************
240+
*/
241+
if (!isVite) {
242+
return;
243+
}
244+
245+
describe('when using Vite', () => {
246+
it('proxies support regexp as context', async () => {
247+
harness.useTarget('serve', {
248+
...BASE_OPTIONS,
249+
proxyConfig: 'proxy.config.json',
250+
});
251+
252+
const proxyServer = createProxyServer();
253+
try {
254+
await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve));
255+
const proxyAddress = proxyServer.address() as import('net').AddressInfo;
256+
257+
await harness.writeFiles({
258+
'proxy.config.json': `
259+
{ "^/api/.*": { "target": "http://127.0.0.1:${proxyAddress.port}" } }
260+
`,
261+
});
262+
263+
const { result, response } = await executeOnceAndFetch(harness, '/api/test');
264+
265+
expect(result?.success).toBeTrue();
266+
expect(await response?.text()).toContain('TEST_API_RETURN');
267+
} finally {
268+
await new Promise<void>((resolve) => proxyServer.close(() => resolve()));
269+
}
270+
});
271+
272+
it('proxies support negated regexp as context', async () => {
273+
harness.useTarget('serve', {
274+
...BASE_OPTIONS,
275+
proxyConfig: 'proxy.config.json',
276+
});
277+
278+
const proxyServer = createProxyServer();
279+
try {
280+
await new Promise<void>((resolve) => proxyServer.listen(0, '127.0.0.1', resolve));
281+
const proxyAddress = proxyServer.address() as import('net').AddressInfo;
282+
283+
await harness.writeFiles({
284+
'proxy.config.json': `
285+
{ "^\\/(?!something).*": { "target": "http://127.0.0.1:${proxyAddress.port}" } }
286+
`,
287+
});
288+
289+
const { result, response } = await executeOnceAndFetch(harness, '/api/test');
290+
291+
expect(result?.success).toBeTrue();
292+
expect(await response?.text()).toContain('TEST_API_RETURN');
293+
} finally {
294+
await new Promise<void>((resolve) => proxyServer.close(() => resolve()));
295+
}
296+
});
297+
});
235298
});
236299
});
237300

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)