fix(reference): resolve extensionless imports to .mts and .cts files (#4223)
* fix(reference): resolve extensionless imports to .mts and .cts files Include .mts and .cts extension probe slots in RESOLUTION_SUFFIXES so that relative imports and requires targeting ESM and CJS TypeScript files without an explicit extension resolve to internal relationships instead of remaining unresolved. * test(reference): restore regression coverage for extensionless .mts and .cts imports Restore tests covering extensionless imports, requires, and directory indexes to .mts and .cts files so that probe suffix omissions are caught by automated test regression.
This commit is contained in:
1 parent
467f5572ac
commit
8194695144
2 files changed
+66
No files matched your search
@@ -253,3 +253,67 @@ it("matches package manifests by exact or unique canonical directory identity",
|
||||
`${decomposedTwin}/exact-decomposed.js`,
|
||||
]);
|
||||
});
|
||||
|
||||
it("resolves extensionless imports and requires to .mts and .cts files and directory indexes", () => {
|
||||
const files = [
|
||||
[
|
||||
"src/index.ts",
|
||||
'import "./esm";\nimport "./pkg-esm";\nimport cjs = require("./cjs");\nimport pkgCjs = require("./pkg-cjs");\n',
|
||||
],
|
||||
["src/esm.mts", "export const esm = 1;\n"],
|
||||
["src/pkg-esm/index.mts", "export const pkgEsm = 2;\n"],
|
||||
["src/cjs.cts", "module.exports = { cjs: 3 };\n"],
|
||||
["src/pkg-cjs/index.cts", "module.exports = { pkgCjs: 4 };\n"],
|
||||
] as const;
|
||||
const read: ReferenceSourceRead = {
|
||||
root: "/selected/source",
|
||||
entries: files.map(([path, content]) => ({
|
||||
status: "read",
|
||||
kind: "file",
|
||||
path,
|
||||
bytes: Buffer.from(content),
|
||||
size: Buffer.byteLength(content),
|
||||
})),
|
||||
bytesRead: files.reduce(
|
||||
(sum, [, content]) => sum + Buffer.byteLength(content),
|
||||
0,
|
||||
),
|
||||
limitations: [],
|
||||
};
|
||||
const parsed = parseReferenceSourceEntries(
|
||||
read,
|
||||
new Set(files.map(([path]) => path)),
|
||||
);
|
||||
expect(parsed.relationships).toEqual(
|
||||
expect.arrayContaining([
|
||||
{
|
||||
from_path: "src/index.ts",
|
||||
to: "src/esm.mts",
|
||||
kind: "imports",
|
||||
resolution: "internal",
|
||||
parse_state: "parsed",
|
||||
},
|
||||
{
|
||||
from_path: "src/index.ts",
|
||||
to: "src/pkg-esm/index.mts",
|
||||
kind: "imports",
|
||||
resolution: "internal",
|
||||
parse_state: "parsed",
|
||||
},
|
||||
{
|
||||
from_path: "src/index.ts",
|
||||
to: "src/cjs.cts",
|
||||
kind: "requires",
|
||||
resolution: "internal",
|
||||
parse_state: "parsed",
|
||||
},
|
||||
{
|
||||
from_path: "src/index.ts",
|
||||
to: "src/pkg-cjs/index.cts",
|
||||
kind: "requires",
|
||||
resolution: "internal",
|
||||
parse_state: "parsed",
|
||||
},
|
||||
]),
|
||||
);
|
||||
});
|
||||
@@ -157,6 +157,8 @@ const readPackageMains = (
|
||||
const RESOLUTION_SUFFIXES: readonly (readonly string[])[] = [
|
||||
[""],
|
||||
[".ts"],
|
||||
[".mts"],
|
||||
[".cts"],
|
||||
[".js"],
|
||||
[".mjs"],
|
||||
[".cjs"],
|
||||
|
||||
Reference in new issue
Block a user