@@ -36,7 +36,11 @@ export function formatNpmSpecifier({name, range, path}: NpmSpecifier): string {
3636}
3737
3838/** Rewrites /npm/ import specifiers to be relative paths to /_npm/. */
39- export function rewriteNpmImports ( input : string , resolve : ( specifier : string ) => string = String ) : string {
39+ export function rewriteNpmImports (
40+ input : string ,
41+ resolve : ( specifier : string ) => string = String ,
42+ href ?: string
43+ ) : string {
4044 const body = parseProgram ( input ) ;
4145 const output = new Sourcemap ( input ) ;
4246
@@ -66,8 +70,9 @@ export function rewriteNpmImports(input: string, resolve: (specifier: string) =>
6670 if ( value !== resolved ) output . replaceLeft ( source . start , source . end , JSON . stringify ( resolved ) ) ;
6771 }
6872
69- // TODO Preserve the source map, but download it too.
70- return String ( output ) . replace ( / ^ \/ \/ # s o u r c e M a p p i n g U R L = .* $ \n ? / m, "" ) ;
73+ return String ( output ) . replace ( / ^ ( \/ \/ # s o u r c e M a p p i n g U R L = ) ( .* ) $ \n ? / m, ( _ , _1 , _2 ) =>
74+ href ? `${ _1 } ${ new URL ( _2 , href ) } ` : ""
75+ ) ;
7176}
7277
7378const npmRequests = new Map < string , Promise < string > > ( ) ;
@@ -89,7 +94,7 @@ export async function populateNpmCache(root: string, path: string): Promise<stri
8994 if ( / ^ a p p l i c a t i o n \/ j a v a s c r i p t ( ; | $ ) / i. test ( response . headers . get ( "content-type" ) ! ) ) {
9095 const source = await response . text ( ) ;
9196 const resolver = await getDependencyResolver ( root , path , source ) ;
92- await writeFile ( outputPath , rewriteNpmImports ( source , resolver ) , "utf-8" ) ;
97+ await writeFile ( outputPath , rewriteNpmImports ( source , resolver , href ) , "utf-8" ) ;
9398 } else {
9499 await writeFile ( outputPath , Buffer . from ( await response . arrayBuffer ( ) ) ) ;
95100 }
0 commit comments