Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve npm sourcemaps #1511

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 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,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<string, Promise<string>>();
Expand All @@ -89,7 +94,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