Skip to content

Commit

Permalink
preserve sourceMap URL
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed Jul 10, 2024
1 parent 474220b commit 6467285
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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<string, Promise<string>>();
Expand All @@ -89,7 +92,7 @@ export async function populateNpmCache(root: string, path: string): Promise<stri
if (/^application\/javascript(;|$)/i.test(response.headers.get("content-type")!)) {
const source = await response.text();
const resolver = await getDependencyResolver(root, path, source);
await writeFile(outputPath, rewriteNpmImports(source, resolver), "utf-8");
await writeFile(outputPath, rewriteNpmImports(source, resolver, href), "utf-8");
} else {
await writeFile(outputPath, Buffer.from(await response.arrayBuffer()));
}
Expand Down

0 comments on commit 6467285

Please sign in to comment.