@@ -28,7 +28,7 @@ interface ModuleTSXConfig {
2828 * If not provided, it will default to using "https://esm.sh/" as the base URL for bare specifiers.
2929 * @default "https://esm.sh/"
3030 */
31- resolveBareSpecifier ?: string | ( ( specifier : string ) => string | Promise < string > ) ;
31+ resolveBareSpecifier ?: string | ( ( specifier : string ) => string ) ;
3232}
3333
3434interface ModuleTSXEventMap {
@@ -50,7 +50,7 @@ export class ModuleTSX extends EventTarget implements IModuleTSX {
5050 public readonly baseUrl : string ;
5151 public readonly importMap : ImportMapData ;
5252 public readonly fetch : ( url : string ) => Promise < Response > ;
53- public readonly resolveBareSpecifier ?: string | ( ( specifier : string ) => string | Promise < string > ) ;
53+ public readonly resolveBareSpecifier : ( specifier : string ) => string ;
5454 private readonly sourceTracker = new SourceTransformTracker < ResourceType > ( ) ;
5555 private readonly fetchText = async ( url : string ) => {
5656 return this . fetch ( url ) . then ( ( res ) => res . text ( ) ) ;
@@ -61,7 +61,10 @@ export class ModuleTSX extends EventTarget implements IModuleTSX {
6161 this . baseUrl = config ?. baseUrl ?? location . href ;
6262 this . importMap = config ?. importMap ?? parseImportMaps ( ) ;
6363 this . fetch = config ?. fetch ?? fetchResponse ;
64- this . resolveBareSpecifier = config ?. resolveBareSpecifier ;
64+ this . resolveBareSpecifier =
65+ typeof config ?. resolveBareSpecifier === "function"
66+ ? config ?. resolveBareSpecifier
67+ : ( specifier : string ) => ( config ?. resolveBareSpecifier ?? "https://esm.sh/" ) + specifier ;
6568 }
6669
6770 private emit < T extends keyof ModuleTSXEventMap > ( type : T , detail ?: ModuleTSXEventMap [ T ] [ "detail" ] ) {
@@ -84,11 +87,7 @@ export class ModuleTSX extends EventTarget implements IModuleTSX {
8487 if ( mappedSpecifier ) {
8588 id = mappedSpecifier ;
8689 } else {
87- if ( typeof this . resolveBareSpecifier === "function" ) {
88- id = await this . resolveBareSpecifier ( id ) ;
89- } else {
90- id = ( this . resolveBareSpecifier ?? "https://esm.sh/" ) + id ;
91- }
90+ id = this . resolveBareSpecifier ( id ) ;
9291 }
9392 }
9493 const url = isRelativeSpecifier ( id ) ? new URL ( id , this . baseUrl ) . href : id ;
@@ -172,16 +171,17 @@ export class ModuleTSX extends EventTarget implements IModuleTSX {
172171 // const code = this.cssStrategy === "link" ? "" : await this.fetchCode(url);
173172 return await this . transformSourceModule ( "css" , fullURL , await this . fetchText ( fullURL ) ) ;
174173 } ;
175- const toEsmSh = ( specifier : string ) => {
174+ const toCDNUrl = ( specifier : string ) => {
176175 // this avoid we accidentally convert a package named xxx.css to a css file on esm.sh
177176 const subpath = specifier . startsWith ( "@" )
178177 ? // @scope /pkg/subpath -> /subpath
179178 specifier . split ( "/" ) . slice ( 2 ) . join ( "/" )
180179 : // pkg/subpath -> /subpath
181180 specifier . split ( "/" ) . slice ( 1 ) . join ( "/" ) ;
182181
183- const url = `https://esm.sh/ ${ specifier } ` ;
182+ const url = this . resolveBareSpecifier ( specifier ) ;
184183 if ( subpath . endsWith ( ".css" ) ) {
184+ // if the subpath (not the package name) ends with .css, we treat it as a css file
185185 return getCssUrl ( url ) ;
186186 }
187187 return url ;
@@ -216,9 +216,9 @@ export class ModuleTSX extends EventTarget implements IModuleTSX {
216216 } else if ( specifier . startsWith ( "node:" ) ) {
217217 return `https://raw.esm.sh/@jspm/core/nodelibs/browser/${ specifier . slice ( 5 ) } .js` ;
218218 } else if ( specifier . startsWith ( "npm:" ) ) {
219- return toEsmSh ( specifier . slice ( 4 ) ) ;
219+ return toCDNUrl ( specifier . slice ( 4 ) ) ;
220220 } else if ( isBareSpecifier ( specifier ) ) {
221- return toEsmSh ( specifier ) ;
221+ return toCDNUrl ( specifier ) ;
222222 } else {
223223 // Fallback: return the original specifier
224224 return specifier ;
0 commit comments