Skip to content

Commit 7b47c82

Browse files
committed
preserve sourceMap URL
1 parent 474220b commit 7b47c82

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/npm.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/^\/\/# sourceMappingURL=.*$\n?/m, "");
73+
return String(output).replace(/^(\/\/# sourceMappingURL=)(.*)$\n?/m, (_, _1, _2) =>
74+
href ? `${_1}${new URL(_2, href)}` : ""
75+
);
7176
}
7277

7378
const npmRequests = new Map<string, Promise<string>>();
@@ -89,7 +94,7 @@ export async function populateNpmCache(root: string, path: string): Promise<stri
8994
if (/^application\/javascript(;|$)/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

Comments
 (0)