From d1b2c30da1a02f57300efc6d6384c3ee84b26981 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Thu, 3 Mar 2022 17:27:33 -0500 Subject: [PATCH] fix: regard file extensions during path resolution (#133) (#193) Originally during the file path resolution, file extensions were removed without explicit reason. This commit changes the resolution logic to keep file extensions, with the goal to add support for modern non-js extensions like cjs. The change however keeps /xyz/index resolves as is as they still should resolve to /xyz. Refs 847d31475665166aa6f30dc01e759435f4fb13a4 Co-authored-by: Andrew Bradley Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com> Co-authored-by: Katja Lutz Co-authored-by: Andrew Bradley --- CHANGELOG.md | 4 +++ src/__tests__/data/match-path-data.ts | 44 ++++++++++----------------- src/match-path-async.ts | 7 +---- src/match-path-sync.ts | 4 +-- 4 files changed, 22 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d78e86..b7dc6d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Fixed + +- Include file extension in paths resolved from package.json "main" field. See PR [#135](https://github.com/dividab/tsconfig-paths/pull/135) and issue [#133](https://github.com/dividab/tsconfig-paths/issues/133). Thanks to [@katywings](https://github.com/katywings) for this fix! + ## [3.12.0] - 2021-08-24 - Add support for baseUrl override using TS_NODE_BASEURL env var #185 and #114. Thanks to @ejhayes and @information-security for these PRs! diff --git a/src/__tests__/data/match-path-data.ts b/src/__tests__/data/match-path-data.ts index fff2e23..24786da 100644 --- a/src/__tests__/data/match-path-data.ts +++ b/src/__tests__/data/match-path-data.ts @@ -108,9 +108,7 @@ export const tests: ReadonlyArray = [ existingFiles: [join("/root", "location", "mylib", "kalle.ts")], packageJson: { main: "./kalle.ts" }, requestedModule: "lib/mylib", - expectedPath: removeExtension( - join("/root", "location", "mylib", "kalle.ts") - ), + expectedPath: join("/root", "location", "mylib", "kalle.ts"), extensions: defaultExtensionsWhenRunningInTsNode, }, { @@ -121,22 +119,7 @@ export const tests: ReadonlyArray = [ packageJson: { main: "./kalle.js" }, requestedModule: "lib/mylib.js", extensions: [".ts", ".js"], - expectedPath: removeExtension( - join("/root", "location", "mylib.js", "kalle.js") - ), - }, - { - name: - "should resolve from main field in package.json and correctly remove file extension", - absoluteBaseUrl: "/root/", - paths: { "lib/*": ["location/*"] }, - existingFiles: [join("/root", "location", "mylibjs", "kalle.js")], - packageJson: { main: "./kalle.js" }, - extensions: [".ts", ".js"], - requestedModule: "lib/mylibjs", - expectedPath: removeExtension( - join("/root", "location", "mylibjs", "kalle.js") - ), + expectedPath: join("/root", "location", "mylib.js", "kalle.js"), }, { name: "should resolve from list of fields by priority in package.json", @@ -150,9 +133,7 @@ export const tests: ReadonlyArray = [ ], extensions: [".ts", ".js"], requestedModule: "lib/mylibjs", - expectedPath: removeExtension( - join("/root", "location", "mylibjs", "browser.js") - ), + expectedPath: join("/root", "location", "mylibjs", "browser.js"), }, { name: "should ignore field mappings to missing files in package.json", @@ -166,9 +147,7 @@ export const tests: ReadonlyArray = [ browser: "./nope.js", }, extensions: [".ts", ".js"], - expectedPath: removeExtension( - join("/root", "location", "mylibjs", "kalle.js") - ), + expectedPath: join("/root", "location", "mylibjs", "kalle.js"), }, { name: "should ignore advanced field mappings in package.json", @@ -184,9 +163,7 @@ export const tests: ReadonlyArray = [ browser: { mylibjs: "./browser.js", "./kalle.js": "./browser.js" }, }, extensions: [".ts", ".js"], - expectedPath: removeExtension( - join("/root", "location", "mylibjs", "kalle.js") - ), + expectedPath: join("/root", "location", "mylibjs", "kalle.js"), }, { name: "should resolve to with the help of baseUrl when not explicitly set", @@ -227,4 +204,15 @@ export const tests: ReadonlyArray = [ expectedPath: undefined, extensions: defaultExtensionsWhenRunningInTsNode, }, + { + name: "should resolve main file with cjs file extension", + absoluteBaseUrl: "/root/", + paths: {}, + existingFiles: [join("/root", "mylib", "index.cjs")], + packageJson: { + main: "./index.cjs", + }, + requestedModule: "mylib", + expectedPath: join("/root", "mylib", "index.cjs"), + }, ]; diff --git a/src/match-path-async.ts b/src/match-path-async.ts index f3e94c2..9673f2a 100644 --- a/src/match-path-async.ts +++ b/src/match-path-async.ts @@ -148,7 +148,6 @@ function findFirstExistingPath( return doneCallback(err); } if (exists) { - // Not sure why we don't just return the full path? Why strip it? return doneCallback(undefined, TryPath.getStrippedPath(tryPath)); } if (index === tryPaths.length - 1) { @@ -180,11 +179,7 @@ function findFirstExistingPath( return doneCallback(mainFieldErr); } if (mainFieldMappedFile) { - // Not sure why we don't just return the full path? Why strip it? - return doneCallback( - undefined, - Filesystem.removeExtension(mainFieldMappedFile) - ); + return doneCallback(undefined, mainFieldMappedFile); } // No field in package json was a valid option. Continue with the next path. diff --git a/src/match-path-sync.ts b/src/match-path-sync.ts index b315a3c..953c8a6 100644 --- a/src/match-path-sync.ts +++ b/src/match-path-sync.ts @@ -118,7 +118,6 @@ function findFirstExistingPath( tryPath.type === "index" ) { if (fileExists(tryPath.path)) { - // Not sure why we don't just return the full path? Why strip it? return TryPath.getStrippedPath(tryPath); } } else if (tryPath.type === "package") { @@ -131,8 +130,7 @@ function findFirstExistingPath( fileExists ); if (mainFieldMappedFile) { - // Not sure why we don't just return the full path? Why strip it? - return Filesystem.removeExtension(mainFieldMappedFile); + return mainFieldMappedFile; } } } else {