fix(browser): keep bare module locations unresolved (#676)
Signed-off-by: RudyCelekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import * as t from "@babel/types";
|
||||
import { AnyMap, eachMapping } from "@jridgewell/trace-mapping";
|
||||
|
||||
import { sanitizeBrowserUrl } from "../domain/browserObservation.js";
|
||||
import { isUrlLikeModuleSpecifier } from "../domain/webBundleAnalyzerAst.js";
|
||||
import { analyzeParsedJavaScriptSemantics } from "../domain/javascriptSemanticAnalysis.js";
|
||||
import { parseJavaScriptSource } from "../domain/javascriptSourceParser.js";
|
||||
import { hasValidSourceMapContents } from "../domain/sourceMapContents.js";
|
||||
@@ -352,6 +353,7 @@ const resolveOriginalSource = (
|
||||
specifier: string,
|
||||
base: string,
|
||||
): string | null => {
|
||||
if (!isUrlLikeModuleSpecifier(specifier)) return null;
|
||||
try {
|
||||
return sanitizeSource(new URL(specifier, base).href);
|
||||
} catch (cause: unknown) {
|
||||
|
||||
@@ -295,3 +295,31 @@ const inspection = (source: string) =>
|
||||
},
|
||||
limitations: [],
|
||||
});
|
||||
|
||||
it("keeps bare module locations unresolved without inventing URL-relative paths", () => {
|
||||
const result = analyzeCapturedWebBundle(
|
||||
inspection(`
|
||||
import "package-name";
|
||||
import("@scope/package");
|
||||
require("package-name");
|
||||
import "./actual.js";
|
||||
importScripts("worker.js");
|
||||
`),
|
||||
);
|
||||
for (const specifier of ["package-name", "@scope/package"])
|
||||
expect(
|
||||
result.observations.chunks.edges
|
||||
.filter((edge) => edge.specifier === specifier)
|
||||
.every((edge) => edge.resolved_url === null),
|
||||
).toBe(true);
|
||||
expect(
|
||||
result.observations.chunks.edges.find(
|
||||
(edge) => edge.specifier === "./actual.js",
|
||||
)?.resolved_url,
|
||||
).toBe(`${origin}/assets/actual.js`);
|
||||
expect(
|
||||
result.observations.chunks.edges.find(
|
||||
(edge) => edge.specifier === "worker.js",
|
||||
)?.resolved_url,
|
||||
).toBe(`${origin}/assets/worker.js`);
|
||||
});
|
||||
|
||||
@@ -44,6 +44,13 @@ export const objectString = (
|
||||
return t.isStringLiteral(value) ? value.value : undefined;
|
||||
};
|
||||
|
||||
/** Whether module syntax supplies a URL location without a package/import-map resolver. */
|
||||
export const isUrlLikeModuleSpecifier = (specifier: string): boolean =>
|
||||
specifier.startsWith("/") ||
|
||||
specifier.startsWith("./") ||
|
||||
specifier.startsWith("../") ||
|
||||
/^[A-Za-z][A-Za-z0-9+.-]*:/u.test(specifier);
|
||||
|
||||
export const resolveSpecifier = (
|
||||
specifier: string,
|
||||
base: string,
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
objectValue,
|
||||
propertyName,
|
||||
resolveSpecifier,
|
||||
isUrlLikeModuleSpecifier,
|
||||
stringArgument,
|
||||
} from "./webBundleAnalyzerAst.js";
|
||||
|
||||
@@ -198,7 +199,11 @@ const addEdge = (context: AddEdgeContext): void => {
|
||||
from_script_key: context.script.script_key,
|
||||
kind: context.kind,
|
||||
specifier: context.specifier,
|
||||
resolved_url: resolveSpecifier(context.specifier, context.script.url),
|
||||
resolved_url:
|
||||
context.kind === "worker_import" ||
|
||||
isUrlLikeModuleSpecifier(context.specifier)
|
||||
? resolveSpecifier(context.specifier, context.script.url)
|
||||
: null,
|
||||
location: location(context.script.script_key, context.node),
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -524,3 +524,29 @@ const input = (overrides: Record<string, unknown> = {}) => {
|
||||
...overrides,
|
||||
});
|
||||
};
|
||||
|
||||
it("keeps bare original module specifiers unresolved", async () => {
|
||||
const result = await fetchWebSourceMaps([request], input(), undefined, {
|
||||
fetch: () =>
|
||||
Promise.resolve(
|
||||
new Response(
|
||||
JSON.stringify({
|
||||
version: 3,
|
||||
names: [],
|
||||
sources: ["main.ts"],
|
||||
sourcesContent: ['import "package-name"; import "./actual.js";'],
|
||||
mappings: "AAAA",
|
||||
}),
|
||||
),
|
||||
),
|
||||
});
|
||||
const edges = result.items[0]?.original_module_edges;
|
||||
expect(
|
||||
edges?.find(({ specifier }) => specifier === "package-name")
|
||||
?.resolved_source,
|
||||
).toBeNull();
|
||||
expect(
|
||||
edges?.find(({ specifier }) => specifier === "./actual.js")
|
||||
?.resolved_source,
|
||||
).toContain("/assets/actual.js");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user