Skip to content

Commit 9580df8

Browse files
committed
refactor(@angular/build): add typed helper for worker pool execution in i18n inliner
Add a strongly-typed private helper method #runWorkerTask to I18nInliner to encapsulate worker pool execution. The helper maps worker task names ('inlineFileBatch' and 'inlineCode') to their exact request and result types exported from the worker module. This eliminates manual type assertions and untyped return values while centralizing worker pool task dispatch.
1 parent 18e4b93 commit 9580df8

2 files changed

Lines changed: 64 additions & 49 deletions

File tree

packages/angular/build/src/tools/esbuild/i18n-inliner-worker.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { createSharedTranslationProxy } from './i18n-translation-reader';
2121
/**
2222
* The options passed to the inliner for each code request
2323
*/
24-
interface InlineCodeRequest {
24+
export interface InlineCodeRequest {
2525
/**
2626
* The code that should be processed.
2727
*/
@@ -45,10 +45,18 @@ interface InlineCodeRequest {
4545
translation?: Blob | SharedArrayBuffer;
4646
}
4747

48+
/**
49+
* The response returned from a code request.
50+
*/
51+
export interface InlineCodeResult {
52+
output: string;
53+
messages: { type: 'error' | 'warning'; message: string }[];
54+
}
55+
4856
/**
4957
* The options passed to the inliner for a batch file request
5058
*/
51-
interface InlineFileBatchRequest {
59+
export interface InlineFileBatchRequest {
5260
/**
5361
* The filename that should be processed.
5462
*/
@@ -91,7 +99,7 @@ interface InlineFileBatchRequest {
9199
/**
92100
* The result for a single locale within a batch file request.
93101
*/
94-
interface InlineLocaleResult {
102+
export interface InlineLocaleResult {
95103
locale: string;
96104
code?: string;
97105
map?: string;
@@ -101,7 +109,7 @@ interface InlineLocaleResult {
101109
/**
102110
* The response returned from a batch file request.
103111
*/
104-
type InlineFileBatchResult =
112+
export type InlineFileBatchResult =
105113
| {
106114
file: string;
107115
unmodified: true;
@@ -292,7 +300,7 @@ export async function inlineFileBatch(
292300
* @param request An InlineRequest object representing the options for inlining
293301
* @returns An object containing the inlined code.
294302
*/
295-
export async function inlineCode(request: InlineCodeRequest) {
303+
export async function inlineCode(request: InlineCodeRequest): Promise<InlineCodeResult> {
296304
const metadata = extractLocalizeMetadata(request.filename, request.code);
297305
const result = await inlineLocalize(
298306
request.code,

packages/angular/build/src/tools/esbuild/i18n-inliner.ts

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,25 @@ import { calculateHash, createContentHash, initializeHash } from '../../utils/ha
1414
import { WorkerPool } from '../../utils/worker-pool';
1515
import { type BuildOutputFile, BuildOutputFileType, createOutputFile } from './bundler-files';
1616
import { type Cache, type PersistentCacheStore, createPersistentCacheStore } from './cache';
17+
import type {
18+
InlineCodeRequest,
19+
InlineCodeResult,
20+
InlineFileBatchRequest,
21+
InlineFileBatchResult,
22+
} from './i18n-inliner-worker';
1723
import { encodeTranslationToBuffer } from './i18n-translation-encoder';
1824

25+
interface WorkerTaskMap {
26+
inlineFileBatch: {
27+
request: InlineFileBatchRequest;
28+
result: InlineFileBatchResult;
29+
};
30+
inlineCode: {
31+
request: InlineCodeRequest;
32+
result: InlineCodeResult;
33+
};
34+
}
35+
1936
/**
2037
* A keyword used to indicate if a JavaScript file may require inlining of translations.
2138
* This keyword is used to avoid processing files that would not otherwise need i18n processing.
@@ -492,28 +509,15 @@ export class I18nInliner {
492509
for (let i = 0; i < entries.length; i += localesPerBatch) {
493510
const batchEntries = entries.slice(i, i + localesPerBatch);
494511
const task = (async () => {
495-
const batchResult = (await this.#workerPool.run(
496-
{
497-
filename,
498-
code: codeBlob,
499-
map: mapBlob,
500-
locales: new Map(batchEntries.map((e) => [e.locale, e.translation])),
501-
ephemeral,
502-
activeLocales,
503-
generation,
504-
},
505-
{ name: 'inlineFileBatch' },
506-
)) as
507-
| {
508-
file: string;
509-
unmodified: true;
510-
messages: { type: 'error' | 'warning'; message: string }[];
511-
}
512-
| {
513-
file: string;
514-
unmodified?: false;
515-
results: Array<TransformedFileResult & { locale: string }>;
516-
};
512+
const batchResult = await this.#runWorkerTask('inlineFileBatch', {
513+
filename,
514+
code: codeBlob,
515+
map: mapBlob,
516+
locales: new Map(batchEntries.map((e) => [e.locale, e.translation])),
517+
ephemeral,
518+
activeLocales,
519+
generation,
520+
});
517521

518522
if (batchResult.unmodified) {
519523
const unmodifiedResult: TransformedFileResult = {
@@ -535,19 +539,18 @@ export class I18nInliner {
535539
for (const res of batchResult.results) {
536540
const matchingEntry = batchEntries.find((e) => e.locale === res.locale);
537541
const cacheKey = matchingEntry?.cacheKey;
542+
const fileResult: TransformedFileResult = {
543+
file: filename,
544+
code: res.code,
545+
map: res.map,
546+
messages: res.messages,
547+
};
538548

539549
if (this.#transformedFileCache && cacheKey) {
540-
cachePromises.push(
541-
this.#transformedFileCache.put(cacheKey, {
542-
file: filename,
543-
code: res.code,
544-
map: res.map,
545-
messages: res.messages,
546-
}),
547-
);
550+
cachePromises.push(this.#transformedFileCache.put(cacheKey, fileResult));
548551
}
549552

550-
fileResultsByLocale.get(res.locale)?.set(filename, res);
553+
fileResultsByLocale.get(res.locale)?.set(filename, fileResult);
551554
}
552555
await Promise.allSettled(cachePromises);
553556
}
@@ -560,6 +563,13 @@ export class I18nInliner {
560563
await Promise.all(workerTasks);
561564
}
562565

566+
#runWorkerTask<T extends keyof WorkerTaskMap>(
567+
name: T,
568+
request: WorkerTaskMap[T]['request'],
569+
): Promise<WorkerTaskMap[T]['result']> {
570+
return this.#workerPool.run(request, { name }) as Promise<WorkerTaskMap[T]['result']>;
571+
}
572+
563573
/**
564574
* Performs inlining of translations for the provided locale and translations.
565575
*
@@ -599,19 +609,16 @@ export class I18nInliner {
599609
};
600610
}
601611

602-
const { output, messages } = await this.#workerPool.run(
603-
{
604-
code: templateCode,
605-
filename: templateId,
606-
locale,
607-
translation: await serializeTranslation(
608-
translation,
609-
translationIntegrity,
610-
this.#translationCache,
611-
),
612-
},
613-
{ name: 'inlineCode' },
614-
);
612+
const { output, messages } = await this.#runWorkerTask('inlineCode', {
613+
code: templateCode,
614+
filename: templateId,
615+
locale,
616+
translation: await serializeTranslation(
617+
translation,
618+
translationIntegrity,
619+
this.#translationCache,
620+
),
621+
});
615622

616623
const errors: string[] = [];
617624
const warnings: string[] = [];

0 commit comments

Comments
 (0)