@@ -14,8 +14,25 @@ import { calculateHash, createContentHash, initializeHash } from '../../utils/ha
1414import { WorkerPool } from '../../utils/worker-pool' ;
1515import { type BuildOutputFile , BuildOutputFileType , createOutputFile } from './bundler-files' ;
1616import { type Cache , type PersistentCacheStore , createPersistentCacheStore } from './cache' ;
17+ import type {
18+ InlineCodeRequest ,
19+ InlineCodeResult ,
20+ InlineFileBatchRequest ,
21+ InlineFileBatchResult ,
22+ } from './i18n-inliner-worker' ;
1723import { 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