diff --git a/packages/angular/build/src/builders/application/i18n.ts b/packages/angular/build/src/builders/application/i18n.ts index 839384421f1f..41232375cee8 100644 --- a/packages/angular/build/src/builders/application/i18n.ts +++ b/packages/angular/build/src/builders/application/i18n.ts @@ -45,7 +45,7 @@ export async function inlineI18n( warnings: string[]; prerenderedRoutes: PrerenderedRoutesRecord; }> { - const { i18nOptions, baseHref, cacheOptions } = options; + const { i18nOptions, baseHref, cacheOptions, workspaceRoot } = options; // Create the multi-threaded inliner with common options. const inliner = new I18nInliner( @@ -56,6 +56,7 @@ export async function inlineI18n( : maxInlinerWorkers, persistentCachePath: cacheOptions.enabled ? cacheOptions.path : undefined, localizeVersion: i18nOptions.localizeVersion, + workspaceRoot, }, workerPool, ); diff --git a/packages/angular/build/src/tools/esbuild/application-code-bundle.ts b/packages/angular/build/src/tools/esbuild/application-code-bundle.ts index 6cd2b09f5a67..ee546d116584 100644 --- a/packages/angular/build/src/tools/esbuild/application-code-bundle.ts +++ b/packages/angular/build/src/tools/esbuild/application-code-bundle.ts @@ -19,6 +19,7 @@ import { SERVER_APP_MANIFEST_FILENAME, SERVER_GENERATED_EXTERNALS, } from '../../utils/server-rendering/manifest'; +import { loadLocaleData } from '../i18n/locale-data'; import { AngularCompilationContext } from './angular/compilation-state'; import { createCompilerPlugin } from './angular/compiler-plugin'; import { ComponentStylesheetBundler } from './angular/component-stylesheets'; @@ -706,6 +707,22 @@ function getEsBuildCommonPolyfillsOptions( if (i18nOptions.shouldInline) { // Remove localize polyfill when i18n inline transformation have been applied to all the packages. polyfills = polyfills.filter((path) => !path.startsWith('@angular/localize')); + + if (polyfills.length === 0 && !options.watch) { + buildOptions.plugins.push({ + name: 'angular-locale-data-hash', + async setup({ initialOptions }) { + // The generated polyfills module only inlines the locale ID and locale data, not messages. + // Account for both in the shared output hash before esbuild assigns the filename. + const data = await Promise.all( + Array.from(i18nOptions.inlineLocales) + .sort() + .map(async (locale) => [locale, (await loadLocaleData(locale, workspaceRoot)).code]), + ); + initialOptions.footer = { js: `/**i18n:${calculateHash(JSON.stringify(data))}*/` }; + }, + }); + } } else if (i18nOptions.hasDefinedSourceLocale) { // When not inlining and a source locale is present, use the source locale data directly polyfills.unshift(`angular:locale/data:${i18nOptions.sourceLocale}`); diff --git a/packages/angular/build/src/tools/i18n/i18n-inliner-worker.ts b/packages/angular/build/src/tools/i18n/i18n-inliner-worker.ts index 778ca55bb814..45e3defe6d01 100644 --- a/packages/angular/build/src/tools/i18n/i18n-inliner-worker.ts +++ b/packages/angular/build/src/tools/i18n/i18n-inliner-worker.ts @@ -19,7 +19,7 @@ import { deserialize } from 'node:v8'; import { parseSync } from 'oxc-parser'; import { traversePostOrder } from '../oxc/traversal'; import { createSharedTranslationProxy } from './i18n-translation-reader'; -import { loadLocaleData } from './locale-data'; +import { type LoadedLocaleData, loadLocaleData } from './locale-data'; /** * The options passed to the inliner for each code request @@ -51,6 +51,7 @@ export interface InlineCodeRequest { * How to handle missing translations. */ missingTranslation?: 'error' | 'warning' | 'ignore'; + workspaceRoot?: string; } /** @@ -89,6 +90,7 @@ export interface InlineFileBatchRequest { * How to handle missing translations. */ missingTranslation?: 'error' | 'warning' | 'ignore'; + workspaceRoot?: string; /** * Whether the file data should be treated as ephemeral and not cached long-term in the Worker. @@ -152,6 +154,8 @@ const fileDataCache = new Map>(); */ const deserializedTranslations = new Map>>(); +const localeDataCache = new Map>(); + /** * The current inlining generation for this worker. */ @@ -286,6 +290,7 @@ export async function inlineFileBatch( locale, await loadTranslation(locale, translation), request.filename, + request.workspaceRoot, request.missingTranslation, ); @@ -319,6 +324,7 @@ export async function inlineCode(request: InlineCodeRequest): Promise | undefined, filename: string, + workspaceRoot = process.cwd(), missingTranslation: 'error' | 'warning' | 'ignore' = 'warning', ) { const magicString = new MagicString(code); @@ -502,7 +510,13 @@ async function inlineLocalize( } if (metadata.localeInsertSites.length > 0) { - const localeData = await loadLocaleData(locale); + const cacheKey = JSON.stringify([workspaceRoot, locale]); + let pendingLocaleData = localeDataCache.get(cacheKey); + if (!pendingLocaleData) { + pendingLocaleData = loadLocaleData(locale, workspaceRoot); + localeDataCache.set(cacheKey, pendingLocaleData); + } + const localeData = await pendingLocaleData; if (localeData.error) { diagnostics.push({ type: 'error', message: localeData.error }); } else if (localeData.warning) { diff --git a/packages/angular/build/src/tools/i18n/i18n-inliner.ts b/packages/angular/build/src/tools/i18n/i18n-inliner.ts index d4a4c4cd631f..dbee99ba311e 100644 --- a/packages/angular/build/src/tools/i18n/i18n-inliner.ts +++ b/packages/angular/build/src/tools/i18n/i18n-inliner.ts @@ -144,6 +144,7 @@ export interface I18nInlinerOptions { maxConcurrency?: number; persistentCachePath?: string; localizeVersion?: string; + workspaceRoot?: string; } /** @@ -308,7 +309,7 @@ export class I18nInliner { await this.initCache(); const generation = ++this.#generation; - const { missingTranslation, localizeVersion } = this.options; + const { missingTranslation, localizeVersion, workspaceRoot } = this.options; const localeList = Array.from(locales); if (localeList.length === 0) { @@ -353,6 +354,7 @@ export class I18nInliner { translation: translationIntegrity || translation, missingTranslation, localizeVersion, + workspaceRoot, }), ), ); @@ -572,6 +574,7 @@ export class I18nInliner { map: mapBlob, locales: new Map(batchEntries.map((e) => [e.locale, e.translation])), missingTranslation: this.options.missingTranslation, + workspaceRoot: this.options.workspaceRoot, ephemeral, activeLocales, generation, @@ -675,6 +678,7 @@ export class I18nInliner { filename: templateId, locale, missingTranslation: this.options.missingTranslation, + workspaceRoot: this.options.workspaceRoot, translation: await serializeTranslation( translation, translationIntegrity, diff --git a/packages/angular/build/src/tools/i18n/i18n-inliner_spec.ts b/packages/angular/build/src/tools/i18n/i18n-inliner_spec.ts index 2fbb6e02ce31..de3fab16c90f 100644 --- a/packages/angular/build/src/tools/i18n/i18n-inliner_spec.ts +++ b/packages/angular/build/src/tools/i18n/i18n-inliner_spec.ts @@ -8,6 +8,7 @@ import type { ɵParsedTranslation } from '@angular/localize'; import { transform } from 'esbuild'; +import assert from 'node:assert'; import fs from 'node:fs/promises'; import os from 'node:os'; import path from 'node:path'; @@ -1119,6 +1120,37 @@ describe('I18nInliner', () => { externalPool = undefined; }); + it('resolves and caches locale data separately for each workspace', async () => { + const baseTmpDir = process.env['TEST_TMPDIR']; + assert(baseTmpDir, 'TEST_TMPDIR is not set'); + const tempDir = await fs.mkdtemp(path.join(baseTmpDir, 'i18n-workspaces-')); + externalPool = new WorkerPool({ maxThreads: 1 }); + + try { + for (const name of ['first', 'second']) { + const workspaceRoot = path.join(tempDir, name); + const localeDir = path.join(workspaceRoot, 'node_modules/@angular/common/locales/global'); + await fs.mkdir(localeDir, { recursive: true }); + const localeData = `globalThis.localeData = "${name}";`; + await fs.writeFile(path.join(localeDir, 'fr.js'), localeData); + + inliner = new I18nInliner({ missingTranslation: 'error', workspaceRoot }, externalPool); + const { outputFiles, errors, warnings } = await inliner.inlineForLocale( + [browserFile('polyfills.js', '$localize.locale = "___NG_LOCALE_INSERT___";')], + 'fr', + undefined, + ); + + expect(errors).toEqual([]); + expect(warnings).toEqual([]); + expect(findFile(outputFiles, 'polyfills.js').text).toContain(localeData); + await inliner.close(); + } + } finally { + await fs.rm(tempDir, { recursive: true, force: true }); + } + }); + it('uses the supplied workerPool to execute transformation tasks', async () => { externalPool = new WorkerPool({ maxThreads: 1 }); const runSpy = spyOn(externalPool, 'run').and.callThrough(); diff --git a/packages/angular/build/src/tools/i18n/locale-data.ts b/packages/angular/build/src/tools/i18n/locale-data.ts index 0418830b69a8..874e5539f672 100644 --- a/packages/angular/build/src/tools/i18n/locale-data.ts +++ b/packages/angular/build/src/tools/i18n/locale-data.ts @@ -37,8 +37,6 @@ export interface LoadedLocaleData { error?: string; } -const localeDataCache = new Map>(); - /** * Resolves the path to the Angular locale data file for a given locale tag. * @@ -98,41 +96,28 @@ export function resolveLocaleDataPath( * @param projectRoot Optional project root for module resolution. * @returns A promise resolving to the loaded locale data script code and any diagnostic warnings. */ -export function loadLocaleData( +export async function loadLocaleData( rawLocaleTag: string, projectRoot?: string, ): Promise { - let cached = localeDataCache.get(rawLocaleTag); - if (!cached) { - cached = (async () => { - const projectResolve = createProjectResolver(projectRoot ?? process.cwd()); - const resolution = resolveLocaleDataPath(rawLocaleTag, (potentialPath) => { - try { - return projectResolve(potentialPath); - } catch { - return undefined; - } - }); - - if (resolution.error) { - return { error: resolution.error }; - } - - if (resolution.path) { - try { - const code = await readFile(resolution.path, 'utf8'); + const projectResolve = createProjectResolver(projectRoot ?? process.cwd()); + const resolution = resolveLocaleDataPath(rawLocaleTag, (potentialPath) => + projectResolve(potentialPath), + ); - return { code, warning: resolution.warning }; - } catch (e) { - return { error: `Failed to read locale data file: ${(e as Error).message}` }; - } - } + if (resolution.error) { + return { error: resolution.error }; + } - return { warning: resolution.warning }; - })(); + if (resolution.path) { + try { + const code = await readFile(resolution.path, 'utf8'); - localeDataCache.set(rawLocaleTag, cached); + return { code, warning: resolution.warning }; + } catch (e) { + return { error: `Failed to read locale data file: ${(e as Error).message}` }; + } } - return cached; + return { warning: resolution.warning }; } diff --git a/tests/e2e/tests/i18n/ivy-localize-hashes.ts b/tests/e2e/tests/i18n/ivy-localize-hashes.ts index 2f682ca76988..0a5f45391b81 100644 --- a/tests/e2e/tests/i18n/ivy-localize-hashes.ts +++ b/tests/e2e/tests/i18n/ivy-localize-hashes.ts @@ -6,9 +6,12 @@ * found in the LICENSE file at https://angular.dev/license */ -import * as fs from 'node:fs'; -import { appendToFile } from '../../utils/fs'; +import assert from 'node:assert/strict'; +import { readdirSync, readFileSync } from 'node:fs'; +import { getGlobalVariable } from '../../utils/env'; +import { replaceInFile } from '../../utils/fs'; import { ng } from '../../utils/process'; +import { updateJsonFile } from '../../utils/project'; import { langTranslations, setupI18nConfig } from './setup'; const OUTPUT_RE = /^(?(?:main|vendor|\d+))(?:\.|-)(?[a-z0-9]+)\.js$/i; @@ -17,11 +20,27 @@ export default async function () { // Setup i18n tests and config. await setupI18nConfig(); + const esbuild = getGlobalVariable('argv')['esbuild']; + if (esbuild) { + // Test the polyfills bundle that contains only the CLI-generated locale initialization. + await updateJsonFile('angular.json', (workspace) => { + workspace.projects['test-project'].architect['build'].options.polyfills = []; + }); + } + // Build each locale and record output file hashes const hashes = new Map(); + const polyfills = new Map(); await ng('build', '--output-hashing=all', '--configuration=development'); for (const { lang, outputPath } of langTranslations) { - for (const entry of fs.readdirSync(outputPath)) { + for (const entry of readdirSync(outputPath)) { + if (esbuild && /^polyfills-[a-z0-9]+\.js$/i.test(entry)) { + polyfills.set(lang, { + name: entry, + content: readFileSync(`${outputPath}/${entry}`, 'utf-8'), + }); + } + const match = entry.match(OUTPUT_RE); if (!match?.groups) { continue; @@ -36,13 +55,27 @@ export default async function () { throw new Error('No output entries found.'); } - // Alter content of a used translation file - await appendToFile('src/locale/messages.fr.xlf', '\n'); + if (esbuild) { + assert.equal(polyfills.size, langTranslations.length); + assert.equal(new Set(Array.from(polyfills.values(), ({ name }) => name)).size, 1); + } + + // Change a translated message without changing any application source. + await replaceInFile('src/locale/messages.fr.xlf', 'Bonjour', 'Salut'); // Build each locale and ensure hashes are different await ng('build', '--output-hashing=all', '--configuration=development'); for (const { lang, outputPath } of langTranslations) { - for (const entry of fs.readdirSync(outputPath)) { + const localePolyfills = polyfills.get(lang); + if (localePolyfills) { + assert.equal( + readFileSync(`${outputPath}/${localePolyfills.name}`, 'utf-8'), + localePolyfills.content, + `Expected the generated polyfills to stay cacheable for ${lang}.`, + ); + } + + for (const entry of readdirSync(outputPath)) { const match = entry.match(OUTPUT_RE); if (!match?.groups) { continue;