Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit bede8d7

Browse files
fix: resolve .js to .ts in aliased paths (#18)
Co-authored-by: Hiroki Osame <[email protected]> Co-authored-by: Marco Schumacher <[email protected]>
1 parent 4d460dd commit bede8d7

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

src/index.ts

+42-25
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,13 @@ Module._resolveFilename = function (request, parent, isMain, options) {
139139
&& !parent?.filename.includes(nodeModulesPath)
140140
) {
141141
const possiblePaths = tsconfigPathsMatcher(request);
142+
142143
for (const possiblePath of possiblePaths) {
144+
const tsFilename = resolveTsFilename.call(this, possiblePath, parent, isMain, options);
145+
if (tsFilename) {
146+
return tsFilename;
147+
}
148+
143149
try {
144150
return resolveFilename.call(
145151
this,
@@ -152,32 +158,43 @@ Module._resolveFilename = function (request, parent, isMain, options) {
152158
}
153159
}
154160

155-
/**
156-
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
157-
*/
158-
if (parent && isTsFilePatten.test(parent.filename)) {
159-
const tsPath = resolveTsPath(request);
160-
161-
if (tsPath) {
162-
try {
163-
return resolveFilename.call(
164-
this,
165-
tsPath,
166-
parent,
167-
isMain,
168-
options,
169-
);
170-
} catch (error) {
171-
const { code } = error as any;
172-
if (
173-
code !== 'MODULE_NOT_FOUND'
174-
&& code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
175-
) {
176-
throw error;
177-
}
178-
}
179-
}
161+
const tsFilename = resolveTsFilename.call(this, request, parent, isMain, options);
162+
if (tsFilename) {
163+
return tsFilename;
180164
}
181165

182166
return resolveFilename.call(this, request, parent, isMain, options);
183167
};
168+
169+
/**
170+
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
171+
*/
172+
function resolveTsFilename(
173+
this: ThisType<typeof resolveFilename>,
174+
request: string,
175+
parent: any,
176+
isMain: boolean,
177+
options?: any,
178+
) {
179+
const tsPath = resolveTsPath(request);
180+
181+
if (parent && isTsFilePatten.test(parent.filename) && tsPath) {
182+
try {
183+
return resolveFilename.call(
184+
this,
185+
tsPath,
186+
parent,
187+
isMain,
188+
options,
189+
);
190+
} catch (error) {
191+
const { code } = error as any;
192+
if (
193+
code !== 'MODULE_NOT_FOUND'
194+
&& code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
195+
) {
196+
throw error;
197+
}
198+
}
199+
}
200+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import value from 'p/nested-resolve-target.js';
2+
3+
console.log(value);

tests/specs/typescript/tsconfig.ts

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
5050
expect(nodeProcess.stdout).toBe('nested-resolve-target');
5151
});
5252

53+
test('resolves paths via .js', async () => {
54+
const nodeProcess = await node.load('./src/paths-prefix-match-js.ts', {
55+
cwd: './tsconfig',
56+
});
57+
expect(nodeProcess.stdout).toBe('nested-resolve-target');
58+
});
59+
5360
describe('dependency', ({ test }) => {
5461
test('resolve current directory', async () => {
5562
const nodeProcess = await node.load('./dependency-resolve-current-directory', {

0 commit comments

Comments
 (0)