Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/angular/build/src/builders/application/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -56,6 +56,7 @@ export async function inlineI18n(
: maxInlinerWorkers,
persistentCachePath: cacheOptions.enabled ? cacheOptions.path : undefined,
localizeVersion: i18nOptions.localizeVersion,
workspaceRoot,
},
workerPool,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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]),
);
Comment thread
splincode marked this conversation as resolved.
Resolved
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}`);
Expand Down
18 changes: 16 additions & 2 deletions packages/angular/build/src/tools/i18n/i18n-inliner-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,6 +51,7 @@ export interface InlineCodeRequest {
* How to handle missing translations.
*/
missingTranslation?: 'error' | 'warning' | 'ignore';
workspaceRoot?: string;
}

/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -152,6 +154,8 @@ const fileDataCache = new Map<string, Promise<CachedFileData>>();
*/
const deserializedTranslations = new Map<string, Promise<Record<string, ɵParsedTranslation>>>();

const localeDataCache = new Map<string, Promise<LoadedLocaleData>>();

/**
* The current inlining generation for this worker.
*/
Expand Down Expand Up @@ -286,6 +290,7 @@ export async function inlineFileBatch(
locale,
await loadTranslation(locale, translation),
request.filename,
request.workspaceRoot,
request.missingTranslation,
);

Expand Down Expand Up @@ -319,6 +324,7 @@ export async function inlineCode(request: InlineCodeRequest): Promise<InlineCode
request.locale,
await loadTranslation(request.locale, request.translation),
request.filename,
request.workspaceRoot,
request.missingTranslation,
);

Expand Down Expand Up @@ -480,6 +486,7 @@ function escapeTemplatePart(part: string): string {
* @param locale The target locale identifier.
* @param translation The translation messages dictionary, or undefined for untranslated locale.
* @param filename The name of the file being transformed.
* @param workspaceRoot The workspace root used to resolve locale data.
* @param missingTranslation How to handle missing translations.
* @returns The transformed code, optional remapped source map, and diagnostics.
*/
Expand All @@ -490,6 +497,7 @@ async function inlineLocalize(
locale: string,
translation: Record<string, ɵParsedTranslation> | undefined,
filename: string,
workspaceRoot = process.cwd(),
missingTranslation: 'error' | 'warning' | 'ignore' = 'warning',
) {
const magicString = new MagicString(code);
Expand All @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion packages/angular/build/src/tools/i18n/i18n-inliner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export interface I18nInlinerOptions {
maxConcurrency?: number;
persistentCachePath?: string;
localizeVersion?: string;
workspaceRoot?: string;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -353,6 +354,7 @@ export class I18nInliner {
translation: translationIntegrity || translation,
missingTranslation,
localizeVersion,
workspaceRoot,
}),
),
);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -675,6 +678,7 @@ export class I18nInliner {
filename: templateId,
locale,
missingTranslation: this.options.missingTranslation,
workspaceRoot: this.options.workspaceRoot,
translation: await serializeTranslation(
translation,
translationIntegrity,
Expand Down
32 changes: 32 additions & 0 deletions packages/angular/build/src/tools/i18n/i18n-inliner_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
47 changes: 16 additions & 31 deletions packages/angular/build/src/tools/i18n/locale-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ export interface LoadedLocaleData {
error?: string;
}

const localeDataCache = new Map<string, Promise<LoadedLocaleData>>();

/**
* Resolves the path to the Angular locale data file for a given locale tag.
*
Expand Down Expand Up @@ -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<LoadedLocaleData> {
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 };
}
45 changes: 39 additions & 6 deletions tests/e2e/tests/i18n/ivy-localize-hashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /^(?<name>(?:main|vendor|\d+))(?:\.|-)(?<hash>[a-z0-9]+)\.js$/i;
Expand All @@ -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<string, string>();
const polyfills = new Map<string, { name: string; content: string }>();
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;
Expand All @@ -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;
Expand Down
Loading