Skip to content

Commit

Permalink
feat: resolve .ts/.tsx/.d.ts first, and then fallback to @types/*
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jun 30, 2019
1 parent 18eafa2 commit b11ede3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
24 changes: 18 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ function resolveFile(source, file, config) {
}

let foundTsPath = null;
const extensions = Object.keys(require.extensions).concat(
'.ts',
'.tsx',
'.d.ts',
);
const extensions = Object.keys(require.extensions);

extensions.unshift('.ts', '.tsx', '.d.ts');

// setup tsconfig-paths
const searchStart = config.directory || process.cwd();
Expand Down Expand Up @@ -64,6 +62,20 @@ function resolveFile(source, file, config) {
foundNodePath = null;
}

// naive attempt at @types/* resolution,
// iff path is neither absolute nor relative
if (
/\.jsx?$/.test(foundNodePath) &&
!/^@types[\/\\]/.test(source) &&
!path.isAbsolute(source) &&
source[0] !== '.'
) {
const definitely = resolveFile('@types/' + source, file, config);
if (definitely.found) {
return definitely;
}
}

if (foundNodePath) {
log('matched node path:', foundNodePath);

Expand All @@ -73,7 +85,7 @@ function resolveFile(source, file, config) {
};
}

log('didnt find', source);
log("didn't find", source);

return {
found: false,
Expand Down
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b11ede3

Please sign in to comment.