From 7c3d2bcd0d80f6fadb48d68e3d31468ba4464dba Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Fri, 17 Oct 2025 14:51:22 +0000 Subject: [PATCH] fix(@angular/build): resolve Angular locale data namespace in esbuild A transient error can occur during `ng serve` when Vite's dependency pre-bundling is triggered for Angular locale data, showing an error like `[vite] (ssr) Error when evaluating SSR module...: There is a new version of the pre-bundle...`. Previously, the `angular:locale/data:` namespace was left unresolved by the build process for the dev server. This caused Vite to treat the namespace as a new dependency, triggering a pre-bundling step that led to the error. With this change, esbuild now resolves the `angular:locale/data:` namespace and replaces it with the direct module import path. While the module is still treated as an external dependency, providing the explicit path prevents Vite from unnecessarily triggering a new pre-bundling phase. This resolves the transient error. Closes #31498 --- .../src/builders/dev-server/vite/server.ts | 2 - .../src/tools/esbuild/i18n-locale-plugin.ts | 53 ++++++++++----- .../tools/vite/plugins/i18n-locale-plugin.ts | 64 ------------------- .../build/src/tools/vite/plugins/index.ts | 1 - 4 files changed, 36 insertions(+), 84 deletions(-) delete mode 100644 packages/angular/build/src/tools/vite/plugins/i18n-locale-plugin.ts 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 3e2b4f1fafd6..0d48ce5325e2 100644 --- a/packages/angular/build/src/builders/dev-server/vite/server.ts +++ b/packages/angular/build/src/builders/dev-server/vite/server.ts @@ -12,7 +12,6 @@ import type { Connect, InlineConfig, SSROptions, ServerOptions } from 'vite'; import type { ComponentStyleRecord } from '../../../tools/vite/middlewares'; import { ServerSsrMode, - createAngularLocaleDataPlugin, createAngularMemoryPlugin, createAngularSetupMiddlewaresPlugin, createAngularSsrTransformPlugin, @@ -221,7 +220,6 @@ export async function setupServer( define, ), plugins: [ - createAngularLocaleDataPlugin(), createAngularSetupMiddlewaresPlugin({ outputFiles, assets, diff --git a/packages/angular/build/src/tools/esbuild/i18n-locale-plugin.ts b/packages/angular/build/src/tools/esbuild/i18n-locale-plugin.ts index 30f4540dc3a8..ae94b62ca16d 100644 --- a/packages/angular/build/src/tools/esbuild/i18n-locale-plugin.ts +++ b/packages/angular/build/src/tools/esbuild/i18n-locale-plugin.ts @@ -6,7 +6,8 @@ * found in the LICENSE file at https://angular.dev/license */ -import type { Plugin } from 'esbuild'; +import type { Plugin, ResolveResult } from 'esbuild'; +import { createRequire } from 'node:module'; /** * The internal namespace used by generated locale import statements and Angular locale data plugin. @@ -27,17 +28,6 @@ export function createAngularLocaleDataPlugin(): Plugin { return { name: 'angular-locale-data', setup(build): void { - // If packages are configured to be external then leave the original angular locale import path. - // This happens when using the development server with caching enabled to allow Vite prebundling to work. - // There currently is no option on the esbuild resolve function to resolve while disabling the option. To - // workaround the inability to resolve the full locale location here, the Vite dev server prebundling also - // contains a plugin to allow the locales to be correctly resolved when prebundling. - // NOTE: If esbuild eventually allows controlling the external package options in a build.resolve call, this - // workaround can be removed. - if (build.initialOptions.packages === 'external') { - return; - } - build.onResolve({ filter: /^angular:locale\/data:/ }, async ({ path }) => { // Extract the locale from the path const rawLocaleTag = path.split(':', 3)[2]; @@ -60,6 +50,7 @@ export function createAngularLocaleDataPlugin(): Plugin { } let exact = true; + let localeRequire: NodeJS.Require | undefined; while (partialLocaleTag) { // Angular embeds the `en`/`en-US` locale into the framework and it does not need to be included again here. // The onLoad hook below for the locale data namespace has an `empty` loader that will prevent inclusion. @@ -73,11 +64,39 @@ export function createAngularLocaleDataPlugin(): Plugin { // Attempt to resolve the locale tag data within the Angular base module location const potentialPath = `${LOCALE_DATA_BASE_MODULE}/${partialLocaleTag}`; - const result = await build.resolve(potentialPath, { - kind: 'import-statement', - resolveDir: build.initialOptions.absWorkingDir, - }); - if (result.path) { + + // If packages are configured to be external then leave the original angular locale import path. + // This happens when using the development server with caching enabled to allow Vite prebundling to work. + // There currently is no option on the esbuild resolve function to resolve while disabling the option. + // NOTE: If esbuild eventually allows controlling the external package options in a build.resolve call, this + // workaround can be removed. + let result: ResolveResult | undefined; + const { packages, absWorkingDir } = build.initialOptions; + if (packages === 'external' && absWorkingDir) { + localeRequire ??= createRequire(absWorkingDir + '/'); + + try { + localeRequire.resolve(potentialPath); + + result = { + errors: [], + warnings: [], + external: true, + sideEffects: true, + namespace: '', + suffix: '', + pluginData: undefined, + path: potentialPath, + }; + } catch {} + } else { + result = await build.resolve(potentialPath, { + kind: 'import-statement', + resolveDir: absWorkingDir, + }); + } + + if (result?.path) { if (exact) { return result; } else { diff --git a/packages/angular/build/src/tools/vite/plugins/i18n-locale-plugin.ts b/packages/angular/build/src/tools/vite/plugins/i18n-locale-plugin.ts deleted file mode 100644 index 5cf3762245a5..000000000000 --- a/packages/angular/build/src/tools/vite/plugins/i18n-locale-plugin.ts +++ /dev/null @@ -1,64 +0,0 @@ -/** - * @license - * Copyright Google LLC All Rights Reserved. - * - * Use of this source code is governed by an MIT-style license that can be - * found in the LICENSE file at https://angular.dev/license - */ - -import type { Plugin } from 'vite'; - -/** - * The base module location used to search for locale specific data. - */ -const LOCALE_DATA_BASE_MODULE = '@angular/common/locales/global'; - -/** - * Creates a Vite plugin that resolves Angular locale data files from `@angular/common`. - * - * @returns A Vite plugin. - */ -export function createAngularLocaleDataPlugin(): Plugin { - return { - name: 'angular-locale-data', - enforce: 'pre', - async resolveId(source) { - if (!source.startsWith('angular:locale/data:')) { - return; - } - - // Extract the locale from the path - const originalLocale = source.split(':', 3)[2]; - - // Remove any private subtags since these will never match - let partialLocale = originalLocale.replace(/-x(-[a-zA-Z0-9]{1,8})+$/, ''); - - let exact = true; - while (partialLocale) { - const potentialPath = `${LOCALE_DATA_BASE_MODULE}/${partialLocale}`; - - const result = await this.resolve(potentialPath); - if (result) { - if (!exact) { - this.warn( - `Locale data for '${originalLocale}' cannot be found. Using locale data for '${partialLocale}'.`, - ); - } - - return result; - } - - // Remove the last subtag and try again with a less specific locale - const parts = partialLocale.split('-'); - partialLocale = parts.slice(0, -1).join('-'); - exact = false; - // The locales "en" and "en-US" are considered exact to retain existing behavior - if (originalLocale === 'en-US' && partialLocale === 'en') { - exact = true; - } - } - - return null; - }, - }; -} diff --git a/packages/angular/build/src/tools/vite/plugins/index.ts b/packages/angular/build/src/tools/vite/plugins/index.ts index 50a6ab6aa7c9..ef697aa7395a 100644 --- a/packages/angular/build/src/tools/vite/plugins/index.ts +++ b/packages/angular/build/src/tools/vite/plugins/index.ts @@ -7,7 +7,6 @@ */ export { createAngularMemoryPlugin } from './angular-memory-plugin'; -export { createAngularLocaleDataPlugin } from './i18n-locale-plugin'; export { createRemoveIdPrefixPlugin } from './id-prefix-plugin'; export { createAngularSetupMiddlewaresPlugin, ServerSsrMode } from './setup-middlewares-plugin'; export { createAngularSsrTransformPlugin } from './ssr-transform-plugin';