From b4eb77bd1a49096f0fa1076e65a8182819b1dc2c Mon Sep 17 00:00:00 2001 From: JJ <6438760+IgnusG@users.noreply.github.com> Date: Fri, 2 Jun 2023 17:34:17 +0200 Subject: [PATCH] Fix missing paths when using typescript's module nodenext feature (#216) * 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 https://github.com/dividab/tsconfig-paths/pull/213 * Add support for .mjs, .cjs and .jsx files --- src/__tests__/data/match-path-data.ts | 11 ++++ src/__tests__/try-path.test.ts | 73 +++++++++++++++++++++++++++ src/try-path.ts | 12 +++++ 3 files changed, 96 insertions(+) diff --git a/src/__tests__/data/match-path-data.ts b/src/__tests__/data/match-path-data.ts index 75b6288..5592699 100644 --- a/src/__tests__/data/match-path-data.ts +++ b/src/__tests__/data/match-path-data.ts @@ -227,4 +227,15 @@ export const tests: ReadonlyArray = [ 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, + }, ]; diff --git a/src/__tests__/try-path.test.ts b/src/__tests__/try-path.test.ts index 56be4da..5fd2e34 100644 --- a/src/__tests__/try-path.test.ts +++ b/src/__tests__/try-path.test.ts @@ -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"], diff --git a/src/try-path.ts b/src/try-path.ts index f6819fb..f720a9a 100644 --- a/src/try-path.ts +++ b/src/try-path.ts @@ -24,6 +24,7 @@ export function getPathsToTry( return undefined; } + const suffixRegex = /\.(c|m)?jsx?$/; const pathsToTry: Array = []; for (const entry of absolutePathMappings) { const starMatch = @@ -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"),