From 7b47c82cb1a95977e96bc4c3509a39dd35953bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Wed, 10 Jul 2024 18:12:40 +0200 Subject: [PATCH] preserve sourceMap URL --- src/npm.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/npm.ts b/src/npm.ts index 7b8a1e0d7..3fb9ae23b 100644 --- a/src/npm.ts +++ b/src/npm.ts @@ -36,7 +36,11 @@ export function formatNpmSpecifier({name, range, path}: NpmSpecifier): string { } /** Rewrites /npm/ import specifiers to be relative paths to /_npm/. */ -export function rewriteNpmImports(input: string, resolve: (specifier: string) => string = String): string { +export function rewriteNpmImports( + input: string, + resolve: (specifier: string) => string = String, + href?: string +): string { const body = parseProgram(input); const output = new Sourcemap(input); @@ -66,8 +70,9 @@ export function rewriteNpmImports(input: string, resolve: (specifier: string) => if (value !== resolved) output.replaceLeft(source.start, source.end, JSON.stringify(resolved)); } - // TODO Preserve the source map, but download it too. - return String(output).replace(/^\/\/# sourceMappingURL=.*$\n?/m, ""); + return String(output).replace(/^(\/\/# sourceMappingURL=)(.*)$\n?/m, (_, _1, _2) => + href ? `${_1}${new URL(_2, href)}` : "" + ); } const npmRequests = new Map>(); @@ -89,7 +94,7 @@ export async function populateNpmCache(root: string, path: string): Promise