fix(javascript): preserve Node exports fallback stop reasons (#606)
* fix(javascript): preserve Node exports fallback stop reasons Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> * test(browser): preserve unknown with-scope loader bindings Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com> --------- Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
@@ -448,7 +448,8 @@ const packageExport = (
|
|||||||
return { status: "invalid" };
|
return { status: "invalid" };
|
||||||
const flattened = exportTargets(root, packageExportConditions(moduleKind));
|
const flattened = exportTargets(root, packageExportConditions(moduleKind));
|
||||||
if (flattened.kind === "invalid") return { status: "invalid" };
|
if (flattened.kind === "invalid") return { status: "invalid" };
|
||||||
const first = flattened.values[0];
|
const first =
|
||||||
|
flattened.kind === "unmatched" ? undefined : flattened.values[0];
|
||||||
return first === undefined
|
return first === undefined
|
||||||
? { status: "unmatched", declared: Object.keys(root) }
|
? { status: "unmatched", declared: Object.keys(root) }
|
||||||
: { status: "value", value: first };
|
: { status: "value", value: first };
|
||||||
@@ -456,38 +457,45 @@ const packageExport = (
|
|||||||
|
|
||||||
type ExportTargets =
|
type ExportTargets =
|
||||||
| { readonly kind: "targets"; readonly values: readonly string[] }
|
| { readonly kind: "targets"; readonly values: readonly string[] }
|
||||||
|
| { readonly kind: "unmatched" }
|
||||||
| { readonly kind: "invalid" };
|
| { readonly kind: "invalid" };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Flatten one exports target into the ordered strings Node would consider.
|
* Resolve a target in Node's declared order. Explicit null blocks an active
|
||||||
* A condition object stops at its first active key, so a null or unmatched
|
* condition, whereas an unmatched nested object permits the next condition.
|
||||||
* nested target ends the search rather than falling through to a later key. An
|
* An array skips unmatched and invalid entries, stops at its first string, and
|
||||||
* array instead skips the entries it cannot resolve and concatenates the rest.
|
* preserves the final invalid-versus-null refusal if no string is selected.
|
||||||
*/
|
*/
|
||||||
const exportTargets = (
|
const exportTargets = (
|
||||||
value: unknown,
|
value: unknown,
|
||||||
conditions: ReadonlySet<string>,
|
conditions: ReadonlySet<string>,
|
||||||
): ExportTargets => {
|
): ExportTargets => {
|
||||||
if (typeof value === "string")
|
if (typeof value === "string")
|
||||||
return value.length > 0
|
return value.startsWith("./")
|
||||||
? { kind: "targets", values: [value] }
|
? { kind: "targets", values: [value] }
|
||||||
: { kind: "invalid" };
|
: { kind: "invalid" };
|
||||||
if (value === null) return { kind: "targets", values: [] };
|
if (value === null) return { kind: "targets", values: [] };
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
const values: string[] = [];
|
let invalid = false;
|
||||||
for (const entry of value) {
|
for (const entry of value) {
|
||||||
const nested = exportTargets(entry, conditions);
|
const nested = exportTargets(entry, conditions);
|
||||||
if (nested.kind === "invalid") return nested;
|
if (nested.kind === "invalid") {
|
||||||
values.push(...nested.values);
|
invalid = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (nested.kind === "unmatched") continue;
|
||||||
|
invalid = false;
|
||||||
|
if (nested.values.length > 0) return nested;
|
||||||
}
|
}
|
||||||
return { kind: "targets", values };
|
return invalid ? { kind: "invalid" } : { kind: "targets", values: [] };
|
||||||
}
|
}
|
||||||
if (typeof value !== "object") return { kind: "invalid" };
|
if (typeof value !== "object") return { kind: "invalid" };
|
||||||
for (const [condition, target] of Object.entries(value)) {
|
for (const [condition, target] of Object.entries(value)) {
|
||||||
if (!conditions.has(condition)) continue;
|
if (!conditions.has(condition)) continue;
|
||||||
return exportTargets(target, conditions);
|
const nested = exportTargets(target, conditions);
|
||||||
|
if (nested.kind !== "unmatched") return nested;
|
||||||
}
|
}
|
||||||
return { kind: "targets", values: [] };
|
return { kind: "unmatched" };
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { mkdir, writeFile } from "node:fs/promises";
|
|||||||
import { join } from "node:path";
|
import { join } from "node:path";
|
||||||
import { promisify } from "node:util";
|
import { promisify } from "node:util";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { z } from "zod";
|
||||||
import {
|
import {
|
||||||
resolveArtifactPathByContext,
|
resolveArtifactPathByContext,
|
||||||
type ArtifactPathResolution,
|
type ArtifactPathResolution,
|
||||||
@@ -18,29 +19,34 @@ const TARGETS = [
|
|||||||
"array.cjs",
|
"array.cjs",
|
||||||
"nested.cjs",
|
"nested.cjs",
|
||||||
];
|
];
|
||||||
const SCRIPT =
|
const nodeExportOutcomeSchema = z.strictObject({
|
||||||
'import { createRequire } from "node:module"; console.log((await import("example")).default ?? createRequire(import.meta.url)("example"))';
|
target: z.string().nullable(),
|
||||||
/** Resolve the bare `example` specifier through the Node resolver that owns it. */
|
error_code: z.string().nullable(),
|
||||||
const nodeTarget = async (
|
});
|
||||||
cwd: string,
|
|
||||||
moduleKind: "import" | "require",
|
/** Resolve through the real Node resolver, retaining its refusal reason. */
|
||||||
): Promise<string | null> => {
|
const nodeTarget = async (cwd: string, moduleKind: "import" | "require") => {
|
||||||
try {
|
const expression =
|
||||||
const { stdout } = await execute(
|
moduleKind === "import"
|
||||||
process.execPath,
|
? '(await import("example")).default'
|
||||||
[
|
: 'createRequire(import.meta.url)("example")';
|
||||||
"--input-type=module",
|
const { stdout } = await execute(
|
||||||
"-e",
|
process.execPath,
|
||||||
moduleKind === "import"
|
[
|
||||||
? 'console.log((await import("example")).default)'
|
"--input-type=module",
|
||||||
: 'import { createRequire } from "node:module"; console.log(createRequire(import.meta.url)("example"))',
|
"-e",
|
||||||
],
|
`
|
||||||
{ cwd },
|
import { createRequire } from "node:module";
|
||||||
);
|
try {
|
||||||
return stdout.trim();
|
console.log(JSON.stringify({target: ${expression}, error_code: null}));
|
||||||
} catch {
|
} catch (error) {
|
||||||
return null;
|
console.log(JSON.stringify({target: null, error_code: error.code}));
|
||||||
}
|
}
|
||||||
|
`,
|
||||||
|
],
|
||||||
|
{ cwd },
|
||||||
|
);
|
||||||
|
return nodeExportOutcomeSchema.parse(JSON.parse(stdout));
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* Install one real package on disk and mirror it into an artifact inventory,
|
* Install one real package on disk and mirror it into an artifact inventory,
|
||||||
@@ -52,6 +58,7 @@ const compareWithNode = async (
|
|||||||
present: readonly string[] = TARGETS,
|
present: readonly string[] = TARGETS,
|
||||||
): Promise<{
|
): Promise<{
|
||||||
readonly node: string | null;
|
readonly node: string | null;
|
||||||
|
readonly node_error: string | null;
|
||||||
readonly outcome: ArtifactPathResolution;
|
readonly outcome: ArtifactPathResolution;
|
||||||
}> => {
|
}> => {
|
||||||
const root = await createTestTempDirectory("rea-exports-");
|
const root = await createTestTempDirectory("rea-exports-");
|
||||||
@@ -83,8 +90,10 @@ const compareWithNode = async (
|
|||||||
},
|
},
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
|
const node = await nodeTarget(root, moduleKind);
|
||||||
return {
|
return {
|
||||||
node: await nodeTarget(root, moduleKind),
|
node: node.target,
|
||||||
|
node_error: node.error_code,
|
||||||
outcome: resolveArtifactPathByContext({
|
outcome: resolveArtifactPathByContext({
|
||||||
declaredPath: "example",
|
declaredPath: "example",
|
||||||
sourcePath: "main.js",
|
sourcePath: "main.js",
|
||||||
@@ -171,6 +180,13 @@ describe("nested and array package export targets", () => {
|
|||||||
"nested.cjs",
|
"nested.cjs",
|
||||||
],
|
],
|
||||||
[{ import: { default: "./default.cjs" } }, "default.cjs"],
|
[{ import: { default: "./default.cjs" } }, "default.cjs"],
|
||||||
|
[
|
||||||
|
{ import: { browser: "./specific.cjs" }, default: "./default.cjs" },
|
||||||
|
"default.cjs",
|
||||||
|
],
|
||||||
|
[{ import: [42, "./array.cjs"] }, "array.cjs"],
|
||||||
|
[{ import: ["./specific.cjs", 42] }, "specific.cjs"],
|
||||||
|
[{ import: ["../outside.cjs", "./array.cjs"] }, "array.cjs"],
|
||||||
[{ import: { node: { default: "./default.cjs" } } }, "default.cjs"],
|
[{ import: { node: { default: "./default.cjs" } } }, "default.cjs"],
|
||||||
[{ import: [{ node: "./nested.cjs" }, "./array.cjs"] }, "nested.cjs"],
|
[{ import: [{ node: "./nested.cjs" }, "./array.cjs"] }, "nested.cjs"],
|
||||||
[{ import: [{ browser: "./x.cjs" }, "./array.cjs"] }, "array.cjs"],
|
[{ import: [{ browser: "./x.cjs" }, "./array.cjs"] }, "array.cjs"],
|
||||||
@@ -226,3 +242,29 @@ describe("unmatched package export conditions", () => {
|
|||||||
expect(outcome.limitations[0]).toContain("not valid package JSON");
|
expect(outcome.limitations[0]).toContain("not valid package JSON");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("package exports fallback refusal reasons", () => {
|
||||||
|
it.each([
|
||||||
|
[{ import: [42, 7] }, "ERR_INVALID_PACKAGE_TARGET", "unavailable"],
|
||||||
|
[{ import: [null, null] }, "ERR_PACKAGE_PATH_NOT_EXPORTED", "external"],
|
||||||
|
[{ import: [42, null] }, "ERR_PACKAGE_PATH_NOT_EXPORTED", "external"],
|
||||||
|
[{ import: [null, 42] }, "ERR_INVALID_PACKAGE_TARGET", "unavailable"],
|
||||||
|
[
|
||||||
|
{ import: { node: null }, default: "./default.cjs" },
|
||||||
|
"ERR_PACKAGE_PATH_NOT_EXPORTED",
|
||||||
|
"external",
|
||||||
|
],
|
||||||
|
] as const)(
|
||||||
|
"matches Node refusal for %j",
|
||||||
|
async (exportsMap, code, status) => {
|
||||||
|
const { node, node_error, outcome } = await compareWithNode(
|
||||||
|
exportsMap,
|
||||||
|
"import",
|
||||||
|
);
|
||||||
|
expect(node).toBeNull();
|
||||||
|
expect(node_error).toBe(code);
|
||||||
|
expect(outcome.resolved_path).toBeNull();
|
||||||
|
expect(outcome.resolution_status).toBe(status);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user