@@ -25,10 +25,8 @@ 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' ,
28+ const extensions = [ '.ts' , '.tsx' , '.d.ts' ] . concat (
29+ Object . keys ( require . extensions ) ,
3230 ) ;
3331
3432 // setup tsconfig-paths
@@ -64,6 +62,25 @@ function resolveFile(source, file, config) {
6462 foundNodePath = null ;
6563 }
6664
65+ // naive attempt at @types /* resolution,
66+ // if path is neither absolute nor relative
67+ if (
68+ ( / \. j s x ? $ / . test ( foundNodePath ) ||
69+ ( config . alwaysTryTypes && ! foundNodePath ) ) &&
70+ ! / ^ @ t y p e s [ / \\ ] / . test ( source ) &&
71+ ! path . isAbsolute ( source ) &&
72+ source [ 0 ] !== '.'
73+ ) {
74+ const definitelyTyped = resolveFile (
75+ '@types' + path . sep + mangleScopedPackage ( source ) ,
76+ file ,
77+ config ,
78+ ) ;
79+ if ( definitelyTyped . found ) {
80+ return definitelyTyped ;
81+ }
82+ }
83+
6784 if ( foundNodePath ) {
6885 log ( 'matched node path:' , foundNodePath ) ;
6986
@@ -73,19 +90,35 @@ function resolveFile(source, file, config) {
7390 } ;
7491 }
7592
76- log ( 'didnt find' , source ) ;
93+ log ( "didn't find" , source ) ;
7794
7895 return {
7996 found : false ,
8097 } ;
8198}
99+
82100function packageFilter ( pkg ) {
83- if ( pkg [ 'jsnext:main' ] ) {
84- pkg [ 'main' ] = pkg [ 'jsnext:main' ] ;
85- }
101+ pkg . main =
102+ pkg . types || pkg . typings || pkg . module || pkg [ 'jsnext:main' ] || pkg . main ;
86103 return pkg ;
87104}
88105
106+ /**
107+ * For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`.
108+ *
109+ * @param {string } moduleName
110+ * @returns {string }
111+ */
112+ function mangleScopedPackage ( moduleName ) {
113+ if ( moduleName [ 0 ] === '@' ) {
114+ const replaceSlash = moduleName . replace ( path . sep , '__' ) ;
115+ if ( replaceSlash !== moduleName ) {
116+ return replaceSlash . slice ( 1 ) ; // Take off the "@"
117+ }
118+ }
119+ return moduleName ;
120+ }
121+
89122module . exports = {
90123 interfaceVersion : 2 ,
91124 resolve : resolveFile ,
0 commit comments