Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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