@@ -10,6 +10,7 @@ import {
1010 isRelativeSpecifier ,
1111} from "./specifier.ts" ;
1212import { addReactImport , needsReactImport } from "./react.ts" ;
13+ import { SourceTransformTracker } from "./source-tracker.ts" ;
1314import { createSourceFile , printSourceFile , transform } from "./ts.ts" ;
1415
1516interface ModuleTSXConfig {
@@ -38,6 +39,7 @@ export class ModuleTSX extends EventTarget implements IModuleTSX {
3839 public readonly baseUrl : string ;
3940 public readonly importMap : ImportMapData ;
4041 public readonly fetch : ( url : string ) => Promise < Response > ;
42+ private readonly sourceTracker = new SourceTransformTracker < ResourceType > ( ) ;
4143 private readonly fetchText = async ( url : string ) => {
4244 return this . fetch ( url ) . then ( ( res ) => res . text ( ) ) ;
4345 } ;
@@ -89,15 +91,19 @@ export class ModuleTSX extends EventTarget implements IModuleTSX {
8991
9092 /** Transform module source code and return a blob URL with the transformed content */
9193 private async transformSourceModule ( sourceType : ResourceType , sourceUrl : string , sourceCode : string ) {
92- if ( this . _sourceMap . has ( sourceUrl ) ) {
93- return this . _sourceMap . get ( sourceUrl ) ! ;
94+ const cachedBlobUrl = this . sourceTracker . get ( sourceType , sourceUrl ) ;
95+ if ( cachedBlobUrl ) {
96+ return cachedBlobUrl ;
9497 }
95- const loader = this . getLoaderByResourceType ( sourceType ) ;
96- const code = `import.meta.url=${ JSON . stringify ( sourceUrl ) } ;\n` + ( await loader ( sourceUrl , sourceCode ) ) ;
97- const blob = new Blob ( [ code ] , { type : "text/javascript" } ) ;
98- const blobUrl = URL . createObjectURL ( blob ) ;
99- this . _track ( sourceUrl , blobUrl ) ;
100- return blobUrl ;
98+
99+ return this . sourceTracker . runWithDedup ( sourceType , sourceUrl , async ( ) => {
100+ const loader = this . getLoaderByResourceType ( sourceType ) ;
101+ const code = `import.meta.url=${ JSON . stringify ( sourceUrl ) } ;\n` + ( await loader ( sourceUrl , sourceCode ) ) ;
102+ const blob = new Blob ( [ code ] , { type : "text/javascript" } ) ;
103+ const blobUrl = URL . createObjectURL ( blob ) ;
104+ this . sourceTracker . set ( sourceType , sourceUrl , blobUrl ) ;
105+ return blobUrl ;
106+ } ) ;
101107 }
102108
103109 private getLoaderByResourceType ( type : ResourceType ) : Loader {
@@ -211,12 +217,6 @@ export class ModuleTSX extends EventTarget implements IModuleTSX {
211217 return resolved ;
212218 }
213219
214- private _sourceMap : Map < string , string > = new Map < string , string > ( ) ;
215- private _blobMap : Map < string , string > = new Map < string , string > ( ) ;
216- private _track ( sourceUrl : string , blobUrl : string ) {
217- this . _sourceMap . set ( sourceUrl , blobUrl ) ;
218- this . _blobMap . set ( blobUrl , sourceUrl ) ;
219- }
220220}
221221
222222type ResourceType = "esm" | "css" | "css-module" ;
0 commit comments