From 64672851f7f384293efd6cdcad54ad3453a30cd8 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 | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/npm.ts b/src/npm.ts index 7b8a1e0d7..0bf1dce66 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,7 @@ 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) => `${_1}${new URL(_2, href)}`); } const npmRequests = new Map>(); @@ -89,7 +92,7 @@ export async function populateNpmCache(root: string, path: string): Promise