Skip to content

Commit d1b2c30

Browse files
aleclarsonkatywingscspotcode
authored
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 847d314 Co-authored-by: Andrew Bradley <cspotcode@gmail.com> Co-authored-by: Alec Larson <1925840+aleclarson@users.noreply.github.com> Co-authored-by: Katja Lutz <mail@katjalutz.ch> Co-authored-by: Andrew Bradley <cspotcode@gmail.com>
1 parent 8a76c5c commit d1b2c30

File tree

4 files changed

+22
-37
lines changed

4 files changed

+22
-37
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- 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!
13+
1014
## [3.12.0] - 2021-08-24
1115

1216
- Add support for baseUrl override using TS_NODE_BASEURL env var #185 and #114. Thanks to @ejhayes and @information-security for these PRs!

src/__tests__/data/match-path-data.ts

+16-28
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ export const tests: ReadonlyArray<OneTest> = [
108108
existingFiles: [join("/root", "location", "mylib", "kalle.ts")],
109109
packageJson: { main: "./kalle.ts" },
110110
requestedModule: "lib/mylib",
111-
expectedPath: removeExtension(
112-
join("/root", "location", "mylib", "kalle.ts")
113-
),
111+
expectedPath: join("/root", "location", "mylib", "kalle.ts"),
114112
extensions: defaultExtensionsWhenRunningInTsNode,
115113
},
116114
{
@@ -121,22 +119,7 @@ export const tests: ReadonlyArray<OneTest> = [
121119
packageJson: { main: "./kalle.js" },
122120
requestedModule: "lib/mylib.js",
123121
extensions: [".ts", ".js"],
124-
expectedPath: removeExtension(
125-
join("/root", "location", "mylib.js", "kalle.js")
126-
),
127-
},
128-
{
129-
name:
130-
"should resolve from main field in package.json and correctly remove file extension",
131-
absoluteBaseUrl: "/root/",
132-
paths: { "lib/*": ["location/*"] },
133-
existingFiles: [join("/root", "location", "mylibjs", "kalle.js")],
134-
packageJson: { main: "./kalle.js" },
135-
extensions: [".ts", ".js"],
136-
requestedModule: "lib/mylibjs",
137-
expectedPath: removeExtension(
138-
join("/root", "location", "mylibjs", "kalle.js")
139-
),
122+
expectedPath: join("/root", "location", "mylib.js", "kalle.js"),
140123
},
141124
{
142125
name: "should resolve from list of fields by priority in package.json",
@@ -150,9 +133,7 @@ export const tests: ReadonlyArray<OneTest> = [
150133
],
151134
extensions: [".ts", ".js"],
152135
requestedModule: "lib/mylibjs",
153-
expectedPath: removeExtension(
154-
join("/root", "location", "mylibjs", "browser.js")
155-
),
136+
expectedPath: join("/root", "location", "mylibjs", "browser.js"),
156137
},
157138
{
158139
name: "should ignore field mappings to missing files in package.json",
@@ -166,9 +147,7 @@ export const tests: ReadonlyArray<OneTest> = [
166147
browser: "./nope.js",
167148
},
168149
extensions: [".ts", ".js"],
169-
expectedPath: removeExtension(
170-
join("/root", "location", "mylibjs", "kalle.js")
171-
),
150+
expectedPath: join("/root", "location", "mylibjs", "kalle.js"),
172151
},
173152
{
174153
name: "should ignore advanced field mappings in package.json",
@@ -184,9 +163,7 @@ export const tests: ReadonlyArray<OneTest> = [
184163
browser: { mylibjs: "./browser.js", "./kalle.js": "./browser.js" },
185164
},
186165
extensions: [".ts", ".js"],
187-
expectedPath: removeExtension(
188-
join("/root", "location", "mylibjs", "kalle.js")
189-
),
166+
expectedPath: join("/root", "location", "mylibjs", "kalle.js"),
190167
},
191168
{
192169
name: "should resolve to with the help of baseUrl when not explicitly set",
@@ -227,4 +204,15 @@ export const tests: ReadonlyArray<OneTest> = [
227204
expectedPath: undefined,
228205
extensions: defaultExtensionsWhenRunningInTsNode,
229206
},
207+
{
208+
name: "should resolve main file with cjs file extension",
209+
absoluteBaseUrl: "/root/",
210+
paths: {},
211+
existingFiles: [join("/root", "mylib", "index.cjs")],
212+
packageJson: {
213+
main: "./index.cjs",
214+
},
215+
requestedModule: "mylib",
216+
expectedPath: join("/root", "mylib", "index.cjs"),
217+
},
230218
];

src/match-path-async.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ function findFirstExistingPath(
148148
return doneCallback(err);
149149
}
150150
if (exists) {
151-
// Not sure why we don't just return the full path? Why strip it?
152151
return doneCallback(undefined, TryPath.getStrippedPath(tryPath));
153152
}
154153
if (index === tryPaths.length - 1) {
@@ -180,11 +179,7 @@ function findFirstExistingPath(
180179
return doneCallback(mainFieldErr);
181180
}
182181
if (mainFieldMappedFile) {
183-
// Not sure why we don't just return the full path? Why strip it?
184-
return doneCallback(
185-
undefined,
186-
Filesystem.removeExtension(mainFieldMappedFile)
187-
);
182+
return doneCallback(undefined, mainFieldMappedFile);
188183
}
189184

190185
// No field in package json was a valid option. Continue with the next path.

src/match-path-sync.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ function findFirstExistingPath(
118118
tryPath.type === "index"
119119
) {
120120
if (fileExists(tryPath.path)) {
121-
// Not sure why we don't just return the full path? Why strip it?
122121
return TryPath.getStrippedPath(tryPath);
123122
}
124123
} else if (tryPath.type === "package") {
@@ -131,8 +130,7 @@ function findFirstExistingPath(
131130
fileExists
132131
);
133132
if (mainFieldMappedFile) {
134-
// Not sure why we don't just return the full path? Why strip it?
135-
return Filesystem.removeExtension(mainFieldMappedFile);
133+
return mainFieldMappedFile;
136134
}
137135
}
138136
} else {

0 commit comments

Comments
 (0)