Skip to content

Commit 6467285

Browse files
committed
preserve sourceMap URL
1 parent 474220b commit 6467285

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/npm.ts

+7-4
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,7 @@ 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) => `${_1}${new URL(_2, href)}`);
7174
}
7275

7376
const npmRequests = new Map<string, Promise<string>>();
@@ -89,7 +92,7 @@ export async function populateNpmCache(root: string, path: string): Promise<stri
8992
if (/^application\/javascript(;|$)/i.test(response.headers.get("content-type")!)) {
9093
const source = await response.text();
9194
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");
9396
} else {
9497
await writeFile(outputPath, Buffer.from(await response.arrayBuffer()));
9598
}

0 commit comments

Comments
 (0)