@@ -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,9 @@ 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 ) =>
74
+ href ? `${ _1 } ${ new URL ( _2 , href ) } ` : ""
75
+ ) ;
71
76
}
72
77
73
78
const npmRequests = new Map < string , Promise < string > > ( ) ;
@@ -89,7 +94,7 @@ export async function populateNpmCache(root: string, path: string): Promise<stri
89
94
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
95
const source = await response . text ( ) ;
91
96
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" ) ;
93
98
} else {
94
99
await writeFile ( outputPath , Buffer . from ( await response . arrayBuffer ( ) ) ) ;
95
100
}
0 commit comments