@@ -126,10 +126,13 @@ const WINDOWS_SEP_REGEXP = new RegExp(`\\${path.win32.sep}`, 'g');
126
126
127
127
export class SourceFileCache extends Map < string , ts . SourceFile > {
128
128
readonly modifiedFiles = new Set < string > ( ) ;
129
+ readonly babelFileCache = new Map < string , string > ( ) ;
129
130
130
131
invalidate ( files : Iterable < string > ) : void {
131
132
this . modifiedFiles . clear ( ) ;
132
133
for ( let file of files ) {
134
+ this . babelFileCache . delete ( file ) ;
135
+
133
136
// Normalize separators to allow matching TypeScript Host paths
134
137
if ( USING_WINDOWS ) {
135
138
file = file . replace ( WINDOWS_SEP_REGEXP , path . posix . sep ) ;
@@ -223,7 +226,7 @@ export function createCompilerPlugin(
223
226
224
227
let previousBuilder : ts . EmitAndSemanticDiagnosticsBuilderProgram | undefined ;
225
228
let previousAngularProgram : NgtscProgram | undefined ;
226
- const babelMemoryCache = new Map < string , string > ( ) ;
229
+ const babelDataCache = new Map < string , string > ( ) ;
227
230
228
231
build . onStart ( async ( ) => {
229
232
const result : OnStartResult = { } ;
@@ -401,28 +404,38 @@ export function createCompilerPlugin(
401
404
} ;
402
405
}
403
406
407
+ const data = typescriptResult . content ?? '' ;
408
+ // The pre-transformed data is used as a cache key. Since the cache is memory only,
409
+ // the options cannot change and do not need to be represented in the key. If the
410
+ // cache is later stored to disk, then the options that affect transform output
411
+ // would need to be added to the key as well.
412
+ let contents = babelDataCache . get ( data ) ;
413
+ if ( contents === undefined ) {
414
+ contents = await transformWithBabel ( args . path , data , pluginOptions ) ;
415
+ babelDataCache . set ( data , contents ) ;
416
+ }
417
+
404
418
return {
405
- contents : await transformWithBabelCached (
406
- args . path ,
407
- typescriptResult . content ?? '' ,
408
- pluginOptions ,
409
- babelMemoryCache ,
410
- ) ,
419
+ contents,
411
420
loader : 'js' ,
412
421
} ;
413
422
} ,
414
423
) ;
415
424
416
425
build . onLoad ( { filter : / \. [ c m ] ? j s $ / } , async ( args ) => {
417
- const data = await fs . readFile ( args . path , 'utf-8' ) ;
426
+ // The filename is currently used as a cache key. Since the cache is memory only,
427
+ // the options cannot change and do not need to be represented in the key. If the
428
+ // cache is later stored to disk, then the options that affect transform output
429
+ // would need to be added to the key as well as a check for any change of content.
430
+ let contents = pluginOptions . sourceFileCache ?. babelFileCache . get ( args . path ) ;
431
+ if ( contents === undefined ) {
432
+ const data = await fs . readFile ( args . path , 'utf-8' ) ;
433
+ contents = await transformWithBabel ( args . path , data , pluginOptions ) ;
434
+ pluginOptions . sourceFileCache ?. babelFileCache . set ( args . path , contents ) ;
435
+ }
418
436
419
437
return {
420
- contents : await transformWithBabelCached (
421
- args . path ,
422
- data ,
423
- pluginOptions ,
424
- babelMemoryCache ,
425
- ) ,
438
+ contents,
426
439
loader : 'js' ,
427
440
} ;
428
441
} ) ;
@@ -524,32 +537,3 @@ async function transformWithBabel(
524
537
525
538
return result ?. code ?? data ;
526
539
}
527
-
528
- /**
529
- * Transforms JavaScript file data using the babel transforms setup in transformWithBabel. The
530
- * supplied cache will be used to avoid repeating the transforms for data that has previously
531
- * been transformed such as in a previous rebuild cycle.
532
- * @param filename The file path of the data to be transformed.
533
- * @param data The file data that will be transformed.
534
- * @param pluginOptions Compiler plugin options that will be used to control the transformation.
535
- * @param cache A cache of previously transformed data that will be used to avoid repeat transforms.
536
- * @returns A promise containing the transformed data.
537
- */
538
- async function transformWithBabelCached (
539
- filename : string ,
540
- data : string ,
541
- pluginOptions : CompilerPluginOptions ,
542
- cache : Map < string , string > ,
543
- ) : Promise < string > {
544
- // The pre-transformed data is used as a cache key. Since the cache is memory only,
545
- // the options cannot change and do not need to be represented in the key. If the
546
- // cache is later stored to disk, then the options that affect transform output
547
- // would need to be added to the key as well.
548
- let result = cache . get ( data ) ;
549
- if ( result === undefined ) {
550
- result = await transformWithBabel ( filename , data , pluginOptions ) ;
551
- cache . set ( data , result ) ;
552
- }
553
-
554
- return result ;
555
- }
0 commit comments