Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): support dev server proxy pathRewr…
Browse files Browse the repository at this point in the history
…ite 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.

(cherry picked from commit 7d3fd22)
  • Loading branch information
clydin authored and alan-agius4 committed Sep 11, 2023
1 parent 275a9ba commit e3a40a4
Showing 1 changed file with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ export async function loadProxyConfiguration(
* @param proxy A proxy configuration object.
*/
function normalizeProxyConfiguration(
proxy: Record<string, unknown> | object[],
): Record<string, unknown> {
let normalizedProxy: Record<string, unknown> | undefined;
proxy: Record<string, object> | object[],
): Record<string, object> {
let normalizedProxy: Record<string, object> | undefined;

if (Array.isArray(proxy)) {
// Construct an object-form proxy configuration from the array
Expand Down Expand Up @@ -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<string, string>,
)) {
pathRewriteEntries.push([new RegExp(pattern), value]);
}

(proxyEntry as Record<string, unknown>).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.
Expand Down

0 comments on commit e3a40a4

Please sign in to comment.