Skip to content

Commit 1e73cce

Browse files
committed
feat: remove .ts/.tsx/.d.ts first, and then fallback to @types/*
1 parent 1dff2c7 commit 1e73cce

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

Diff for: index.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ function resolveFile(source, file, config) {
2525
}
2626

2727
let foundTsPath = null;
28-
const extensions = Object.keys(require.extensions).concat(
29-
'.ts',
30-
'.tsx',
31-
'.d.ts',
32-
);
28+
const extensions = Object.keys(require.extensions);
29+
30+
extensions.unshift('.ts', '.tsx', '.d.ts');
3331

3432
// setup tsconfig-paths
3533
const searchStart = config.directory || process.cwd();
@@ -54,25 +52,28 @@ function resolveFile(source, file, config) {
5452

5553
// note that even if we match via tsconfig-paths, we still need to do a final resolve
5654
let foundNodePath;
57-
const basedir = path.dirname(path.resolve(file))
5855
try {
5956
foundNodePath = resolve.sync(foundTsPath || source, {
6057
extensions,
61-
basedir,
58+
basedir: path.dirname(path.resolve(file)),
6259
packageFilter,
6360
});
6461
} catch (err) {
6562
foundNodePath = null;
6663
}
6764

68-
if (foundNodePath && /\.jsx?$/.test(foundNodePath)) {
69-
try {
70-
foundNodePath = resolve.sync(foundTsPath || source, {
71-
extensions: ['.d.ts'],
72-
basedir,
73-
packageFilter,
74-
});
75-
} catch (err) {}
65+
// naive attempt at @types/* resolution,
66+
// iff path is neither absolute nor relative
67+
if (
68+
/\.jsx?$/.test(foundNodePath) &&
69+
!/^@types[\/\\]/.test(source) &&
70+
!path.isAbsolute(source) &&
71+
source[0] !== '.'
72+
) {
73+
const definitely = resolveFile('@types/' + source, file, config);
74+
if (definitely.found) {
75+
return definitely;
76+
}
7677
}
7778

7879
if (foundNodePath) {

0 commit comments

Comments
 (0)