@@ -25,10 +25,8 @@ function resolveFile(source, file, config) {
25
25
}
26
26
27
27
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 ) ,
32
30
) ;
33
31
34
32
// setup tsconfig-paths
@@ -64,6 +62,25 @@ function resolveFile(source, file, config) {
64
62
foundNodePath = null ;
65
63
}
66
64
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
+
67
84
if ( foundNodePath ) {
68
85
log ( 'matched node path:' , foundNodePath ) ;
69
86
@@ -73,19 +90,35 @@ function resolveFile(source, file, config) {
73
90
} ;
74
91
}
75
92
76
- log ( 'didnt find' , source ) ;
93
+ log ( "didn't find" , source ) ;
77
94
78
95
return {
79
96
found : false ,
80
97
} ;
81
98
}
99
+
82
100
function 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 ;
86
103
return pkg ;
87
104
}
88
105
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
+
89
122
module . exports = {
90
123
interfaceVersion : 2 ,
91
124
resolve : resolveFile ,
0 commit comments