From 4fa37a84f310566c7e8443a7e68f187116ddf61d Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 6 Feb 2025 19:47:36 +0000 Subject: [PATCH] refactor(@angular/build): also add `server.preTransformRequests` https://github.com/angular/angular-cli/commit/5c1360179cec2f0fad6b2adb4a8e4d6930738976 moved `preTransformRequests` from the `server` to `dev` section. But vite, still uses the `server` section in such cases https://github.com/vitejs/vite/blob/bcdb51a1ac082f4e8ed6f820787d6745dfaa972d/packages/vite/src/node/server/index.ts#L673 and https://github.com/vitejs/vite/blob/bcdb51a1ac082f4e8ed6f820787d6745dfaa972d/packages/vite/src/node/server/middlewares/indexHtml.ts#L475 --- .../src/builders/dev-server/vite-server.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/angular/build/src/builders/dev-server/vite-server.ts b/packages/angular/build/src/builders/dev-server/vite-server.ts index d704e3a5161f..8bd8ab58fe87 100644 --- a/packages/angular/build/src/builders/dev-server/vite-server.ts +++ b/packages/angular/build/src/builders/dev-server/vite-server.ts @@ -734,6 +734,7 @@ function updateResultRecord( } } +// eslint-disable-next-line max-lines-per-function export async function setupServer( serverOptions: NormalizedDevServerOptions, outputFiles: Map, @@ -776,6 +777,15 @@ export async function setupServer( break; } + /** + * Required when using `externalDependencies` to prevent Vite load errors. + * + * @note Can be removed if Vite introduces native support for externals. + * @note Vite misresolves browser modules in SSR when accessing URLs with multiple segments + * (e.g., 'foo/bar'), as they are not correctly re-based from the base href. + */ + const preTransformRequests = + externalMetadata.explicitBrowser.length === 0 && ssrMode === ServerSsrMode.NoSsr; const cacheDir = join(serverOptions.cacheOptions.path, serverOptions.buildTarget.project, 'vite'); const configuration: InlineConfig = { configFile: false, @@ -806,15 +816,10 @@ export async function setupServer( preserveSymlinks, }, dev: { - // This is needed when `externalDependencies` is used to prevent Vite load errors. - // NOTE: If Vite adds direct support for externals, this can be removed. - // NOTE: Vite breaks the resolution of browser modules in SSR - // when accessing a url with two or more segments (e.g., 'foo/bar'), - // as they are not re-based from the base href. - preTransformRequests: - externalMetadata.explicitBrowser.length === 0 && ssrMode === ServerSsrMode.NoSsr, + preTransformRequests, }, server: { + preTransformRequests, warmup: { ssrFiles, },