@@ -36,7 +36,11 @@ export function formatNpmSpecifier({name, range, path}: NpmSpecifier): string {
36
36
}
37
37
38
38
/** 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 {
40
44
const body = parseProgram ( input ) ;
41
45
const output = new Sourcemap ( input ) ;
42
46
@@ -66,8 +70,7 @@ export function rewriteNpmImports(input: string, resolve: (specifier: string) =>
66
70
if ( value !== resolved ) output . replaceLeft ( source . start , source . end , JSON . stringify ( resolved ) ) ;
67
71
}
68
72
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 ) => `${ _1 } ${ new URL ( _2 , href ) } ` ) ;
71
74
}
72
75
73
76
const npmRequests = new Map < string , Promise < string > > ( ) ;
@@ -89,7 +92,7 @@ export async function populateNpmCache(root: string, path: string): Promise<stri
89
92
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" ) ! ) ) {
90
93
const source = await response . text ( ) ;
91
94
const resolver = await getDependencyResolver ( root , path , source ) ;
92
- await writeFile ( outputPath , rewriteNpmImports ( source , resolver ) , "utf-8" ) ;
95
+ await writeFile ( outputPath , rewriteNpmImports ( source , resolver , href ) , "utf-8" ) ;
93
96
} else {
94
97
await writeFile ( outputPath , Buffer . from ( await response . arrayBuffer ( ) ) ) ;
95
98
}
0 commit comments