@@ -190,6 +190,20 @@ func legacyESM(ctx *rex.Context, buildStorage storage.Storage, buildVersionPrefi
190190 ctx .SetHeader ("Content-Type" , ctJavaScript )
191191 case ".ts" , ".mts" :
192192 ctx .SetHeader ("Content-Type" , ctTypeScript )
193+ // resolve hostname in typescript definition files if the origin is not "https://esm.sh"
194+ if endsWith (pathname , ".d.ts" , ".d.mts" ) {
195+ origin := getOrigin (ctx )
196+ if origin != "https://esm.sh" {
197+ defer f .Close ()
198+ data , err := io .ReadAll (f )
199+ if err != nil {
200+ return rex .Status (500 , "Failed to read data from storage" )
201+ }
202+ data = bytes .ReplaceAll (data , []byte ("https://esm.sh/v" ), []byte (origin + "/v" ))
203+ data = bytes .ReplaceAll (data , []byte (config .LegacyServer + "/v" ), []byte (origin + "/v" ))
204+ return data
205+ }
206+ }
193207 case ".map" :
194208 ctx .SetHeader ("Content-Type" , ctJSON )
195209 case ".css" :
@@ -273,19 +287,20 @@ func legacyESM(ctx *rex.Context, buildStorage storage.Storage, buildVersionPrefi
273287 if err != nil {
274288 return rex .Status (500 , "Failed to fetch data from the legacy esm.sh server" )
275289 }
276- if endsWith (pathname , ".d.ts" , ".d.mts" ) {
277- origin := getOrigin (ctx )
278- if origin != "https://esm.sh" {
279- data = bytes .ReplaceAll (data , []byte ("https://esm.sh" ), []byte (origin ))
280- }
281- data = bytes .ReplaceAll (data , []byte (config .LegacyServer ), []byte (origin ))
282- }
283290 err = buildStorage .Put (savePath , bytes .NewReader (data ))
284291 if err != nil {
285292 return rex .Status (500 , "Storage error: " + err .Error ())
286293 }
287294 ctx .SetHeader ("Content-Type" , res .Header .Get ("Content-Type" ))
288295 ctx .SetHeader ("Control-Cache" , ccImmutable )
296+ // resolve hostname in typescript definition files if the origin is not "https://esm.sh"
297+ if endsWith (pathname , ".d.ts" , ".d.mts" ) {
298+ origin := getOrigin (ctx )
299+ if origin != "https://esm.sh" {
300+ data = bytes .ReplaceAll (data , []byte ("https://esm.sh/v" ), []byte (origin + "/v" ))
301+ data = bytes .ReplaceAll (data , []byte (config .LegacyServer + "/v" ), []byte (origin + "/v" ))
302+ }
303+ }
289304 return data
290305 } else {
291306 code , err := io .ReadAll (res .Body )
0 commit comments