fix(web): recover type-wrapped source-map requires (#12304)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 12:11:43 +00:00
1 parent b1479de3dd
commit cf6116a284
2 files changed
+48 -5

No files matched your search

+5 -5
View File
@@ -13,6 +13,7 @@ import {
import { AnalysisError } from "../domain/analysisErrorBase.js";
import { err, ok, type Result } from "../domain/result.js";
import { isUrlLikeModuleSpecifier } from "../domain/webBundleAnalyzerAst.js";
import { unwrapJavaScriptExpression } from "../domain/javascript/javascriptAstValues.js";
import { analyzeParsedJavaScriptReferences } from "../domain/javascript/javascriptSemanticAnalysis.js";
import { traverseJavaScriptAst } from "../domain/javascript/javascriptSemanticTraversal.js";
import {
@@ -1633,14 +1634,13 @@ const originalDependency = (
? null
: { kind: "dynamic_import", specifier };
}
if (
(t.isCallExpression(node) || t.isOptionalCallExpression(node)) &&
t.isIdentifier(node.callee, { name: "require" })
) {
if (t.isCallExpression(node) || t.isOptionalCallExpression(node)) {
const callee = unwrapJavaScriptExpression(node.callee).node;
if (!t.isIdentifier(callee, { name: "require" })) return null;
const specifier = stringValue(node.arguments[0]);
return specifier === undefined
? null
: { kind: "require", specifier, requireCallee: node.callee };
: { kind: "require", specifier, requireCallee: callee };
}
// `import x = require("m")` is only legal at module top level and always
// refers to the host loader, so it needs no unbound-`require` check. An
@@ -85,3 +85,46 @@ it("retains literal dependencies from HTTP source-map originals", async () => {
});
}
});
it("retains type-wrapped source-map requires with an unbound loader", async () => {
const origin = "https://fixture.example";
const url = `${origin}/dist/app.js.map`;
const source = [
'(require as typeof require)("./asserted.cjs");',
'require!("./non-null.cjs");',
"function local(require: (id: string) => unknown) {",
' (require as typeof require)("./shadowed.cjs");',
"}",
].join("\n");
const text = JSON.stringify({
version: 3,
names: [],
mappings: "AAAA",
sources: ["../src/main.cts"],
sourcesContent: [source],
});
const result = await fetchWebSourceMapsOk(
[{ scriptKey: `scr_${"1".repeat(64)}`, declaredUrl: url, fetchUrl: url }],
analyzeWebBundleInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "fixture",
allowed_origins: [origin],
fetch_source_maps: true,
}),
undefined,
{ fetch: async () => new Response(text) },
);
expect(result.items[0]).toMatchObject({
status: "included",
original_sources: [{ artifact: { text: source } }],
original_module_edges: ["./asserted.cjs", "./non-null.cjs"].map(
(specifier) => ({
declaration_index: 0,
from_source: `${origin}/src/main.cts`,
kind: "require",
specifier,
resolved_source: new URL(specifier, `${origin}/src/main.cts`).href,
}),
),
});
});