Skip to content

Commit 7d3fd22

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): support dev server proxy pathRewrite field in Vite-based server
The development server proxy configuration file for Webpack supports a `pathRewrite` field that is not directly supported by the underlying Vite development server when using the application or esbuild- browser builders. To provide equivalent support, especially for JSON file-based proxy configurations, the `pathRewrite` field is now converted internally to a proxy `rewrite` function.
1 parent 6eee74c commit 7d3fd22

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

packages/angular_devkit/build_angular/src/builders/dev-server/load-proxy-config.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ export async function loadProxyConfiguration(
9595
* @param proxy A proxy configuration object.
9696
*/
9797
function normalizeProxyConfiguration(
98-
proxy: Record<string, unknown> | object[],
99-
): Record<string, unknown> {
100-
let normalizedProxy: Record<string, unknown> | undefined;
98+
proxy: Record<string, object> | object[],
99+
): Record<string, object> {
100+
let normalizedProxy: Record<string, object> | undefined;
101101

102102
if (Array.isArray(proxy)) {
103103
// Construct an object-form proxy configuration from the array
@@ -135,9 +135,44 @@ function normalizeProxyConfiguration(
135135
}
136136
}
137137

138+
// Replace `pathRewrite` field with a `rewrite` function
139+
for (const proxyEntry of Object.values(normalizedProxy)) {
140+
if (
141+
'pathRewrite' in proxyEntry &&
142+
proxyEntry.pathRewrite &&
143+
typeof proxyEntry.pathRewrite === 'object'
144+
) {
145+
// Preprocess path rewrite entries
146+
const pathRewriteEntries: [RegExp, string][] = [];
147+
for (const [pattern, value] of Object.entries(
148+
proxyEntry.pathRewrite as Record<string, string>,
149+
)) {
150+
pathRewriteEntries.push([new RegExp(pattern), value]);
151+
}
152+
153+
(proxyEntry as Record<string, unknown>).rewrite = pathRewriter.bind(
154+
undefined,
155+
pathRewriteEntries,
156+
);
157+
158+
delete proxyEntry.pathRewrite;
159+
}
160+
}
161+
138162
return normalizedProxy;
139163
}
140164

165+
function pathRewriter(pathRewriteEntries: [RegExp, string][], path: string): string {
166+
for (const [pattern, value] of pathRewriteEntries) {
167+
const updated = path.replace(pattern, value);
168+
if (path !== updated) {
169+
return updated;
170+
}
171+
}
172+
173+
return path;
174+
}
175+
141176
/**
142177
* Calculates the line and column for an error offset in the content of a JSON file.
143178
* @param location The offset error location from the beginning of the content.

0 commit comments

Comments
 (0)