Skip to content

Commit

Permalink
Fix missing paths when using typescript's module nodenext feature (#216)
Browse files Browse the repository at this point in the history
* Fix missing paths when using typescript's module nodenext feature

This resolution requires relative/mapped import paths to end with .js

Reference https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#esm-nodejs

* Add tests from #213

#213

* Add support for .mjs, .cjs and .jsx files
  • Loading branch information
IgnusG committed Jun 2, 2023
1 parent 19eba12 commit b4eb77b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/__tests__/data/match-path-data.ts
Expand Up @@ -227,4 +227,15 @@ export const tests: ReadonlyArray<OneTest> = [
expectedPath: join("/root", "mylib", "index.cjs"),
extensions: defaultExtensionsWhenRunningInTsNode,
},
{
name: "should resolve .ts from .js alias",
absoluteBaseUrl: "/root/",
paths: {
"@/*": ["src/*"],
},
existingFiles: [join("/root", "src", "foo.ts")],
requestedModule: "@/foo.js",
expectedPath: join("/root", "src", "foo"),
extensions: defaultExtensionsWhenRunningInTsNode,
},
];
73 changes: 73 additions & 0 deletions src/__tests__/try-path.test.ts
Expand Up @@ -95,6 +95,79 @@ describe("mapping-entry", () => {
]);
});

it.each(["js", "jsx", "mjs", "cjs"])(
"should include paths with ending .%s removed that matches requested module",
(extension) => {
const result = getPathsToTry(
[".ts", ".tsx"],
[
{
pattern: "longest/pre/fix/*",
paths: [join("/absolute", "base", "url", "foo2", "*")],
},
],
`longest/pre/fix/bar.${extension}`
);

expect(result).toEqual(
expect.arrayContaining([
{
type: "extension",
path: join("/absolute", "base", "url", "foo2", "bar.ts"),
},
{
type: "extension",
path: join("/absolute", "base", "url", "foo2", "bar.tsx"),
},
])
);
}
);

it.each([
["mjs", "mts"],
["cjs", "cts"],
])(
"should include paths with ending .%s removed that matches requested module with extension .%s",
(requestedModuleExtension, expectedExtension) => {
const result = getPathsToTry(
[".ts", ".tsx", `.${expectedExtension}`, `.${expectedExtension}x`],
[
{
pattern: "longest/pre/fix/*",
paths: [join("/absolute", "base", "url", "foo2", "*")],
},
],
`longest/pre/fix/bar.${requestedModuleExtension}`
);

expect(result).toEqual(
expect.arrayContaining([
{
type: "extension",
path: join(
"/absolute",
"base",
"url",
"foo2",
`bar.${expectedExtension}`
),
},
{
type: "extension",
path: join(
"/absolute",
"base",
"url",
"foo2",
`bar.${expectedExtension}x`
),
},
])
);
}
);

it("should resolve paths starting with a slash", () => {
const result = getPathsToTry(
[".ts"],
Expand Down
12 changes: 12 additions & 0 deletions src/try-path.ts
Expand Up @@ -24,6 +24,7 @@ export function getPathsToTry(
return undefined;
}

const suffixRegex = /\.(c|m)?jsx?$/;
const pathsToTry: Array<TryPath> = [];
for (const entry of absolutePathMappings) {
const starMatch =
Expand All @@ -39,6 +40,17 @@ export function getPathsToTry(
(e) => ({ type: "extension", path: physicalPath + e } as TryPath)
)
);
if (physicalPath.match(suffixRegex)) {
pathsToTry.push(
...extensions.map(
(e) =>
({
type: "extension",
path: physicalPath.replace(suffixRegex, "") + e,
} as TryPath)
)
);
}
pathsToTry.push({
type: "package",
path: path.join(physicalPath, "/package.json"),
Expand Down

0 comments on commit b4eb77b

Please sign in to comment.