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

Commit

Permalink
fix: resolve .js to .ts in aliased paths (#18)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroki Osame <[email protected]>
Co-authored-by: Marco Schumacher <[email protected]>
  • Loading branch information
privatenumber and schummar authored Aug 7, 2022
1 parent 4d460dd commit bede8d7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 25 deletions.
67 changes: 42 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ Module._resolveFilename = function (request, parent, isMain, options) {
&& !parent?.filename.includes(nodeModulesPath)
) {
const possiblePaths = tsconfigPathsMatcher(request);

for (const possiblePath of possiblePaths) {
const tsFilename = resolveTsFilename.call(this, possiblePath, parent, isMain, options);
if (tsFilename) {
return tsFilename;
}

try {
return resolveFilename.call(
this,
Expand All @@ -152,32 +158,43 @@ Module._resolveFilename = function (request, parent, isMain, options) {
}
}

/**
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
*/
if (parent && isTsFilePatten.test(parent.filename)) {
const tsPath = resolveTsPath(request);

if (tsPath) {
try {
return resolveFilename.call(
this,
tsPath,
parent,
isMain,
options,
);
} catch (error) {
const { code } = error as any;
if (
code !== 'MODULE_NOT_FOUND'
&& code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
) {
throw error;
}
}
}
const tsFilename = resolveTsFilename.call(this, request, parent, isMain, options);
if (tsFilename) {
return tsFilename;
}

return resolveFilename.call(this, request, parent, isMain, options);
};

/**
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
*/
function resolveTsFilename(
this: ThisType<typeof resolveFilename>,
request: string,
parent: any,
isMain: boolean,
options?: any,
) {
const tsPath = resolveTsPath(request);

if (parent && isTsFilePatten.test(parent.filename) && tsPath) {
try {
return resolveFilename.call(
this,
tsPath,
parent,
isMain,
options,
);
} catch (error) {
const { code } = error as any;
if (
code !== 'MODULE_NOT_FOUND'
&& code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
) {
throw error;
}
}
}
}
3 changes: 3 additions & 0 deletions tests/fixtures/tsconfig/src/paths-prefix-match-js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import value from 'p/nested-resolve-target.js';

console.log(value);
7 changes: 7 additions & 0 deletions tests/specs/typescript/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
expect(nodeProcess.stdout).toBe('nested-resolve-target');
});

test('resolves paths via .js', async () => {
const nodeProcess = await node.load('./src/paths-prefix-match-js.ts', {
cwd: './tsconfig',
});
expect(nodeProcess.stdout).toBe('nested-resolve-target');
});

describe('dependency', ({ test }) => {
test('resolve current directory', async () => {
const nodeProcess = await node.load('./dependency-resolve-current-directory', {
Expand Down

0 comments on commit bede8d7

Please sign in to comment.