diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/load-proxy-config.ts b/packages/angular_devkit/build_angular/src/builders/dev-server/load-proxy-config.ts index 14f7c7140f4b..3f9206c2f9e4 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/load-proxy-config.ts +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/load-proxy-config.ts @@ -95,9 +95,9 @@ export async function loadProxyConfiguration( * @param proxy A proxy configuration object. */ function normalizeProxyConfiguration( - proxy: Record | object[], -): Record { - let normalizedProxy: Record | undefined; + proxy: Record | object[], +): Record { + let normalizedProxy: Record | undefined; if (Array.isArray(proxy)) { // Construct an object-form proxy configuration from the array @@ -135,9 +135,44 @@ function normalizeProxyConfiguration( } } + // Replace `pathRewrite` field with a `rewrite` function + for (const proxyEntry of Object.values(normalizedProxy)) { + if ( + 'pathRewrite' in proxyEntry && + proxyEntry.pathRewrite && + typeof proxyEntry.pathRewrite === 'object' + ) { + // Preprocess path rewrite entries + const pathRewriteEntries: [RegExp, string][] = []; + for (const [pattern, value] of Object.entries( + proxyEntry.pathRewrite as Record, + )) { + pathRewriteEntries.push([new RegExp(pattern), value]); + } + + (proxyEntry as Record).rewrite = pathRewriter.bind( + undefined, + pathRewriteEntries, + ); + + delete proxyEntry.pathRewrite; + } + } + return normalizedProxy; } +function pathRewriter(pathRewriteEntries: [RegExp, string][], path: string): string { + for (const [pattern, value] of pathRewriteEntries) { + const updated = path.replace(pattern, value); + if (path !== updated) { + return updated; + } + } + + return path; +} + /** * Calculates the line and column for an error offset in the content of a JSON file. * @param location The offset error location from the beginning of the content.