1
- 'use strict'
1
+ 'use strict' ;
2
2
3
- const path = require ( 'path' )
4
- const resolve = require ( 'resolve' )
5
- const tsconfigPaths = require ( 'tsconfig-paths' )
6
- const debug = require ( 'debug' )
3
+ const path = require ( 'path' ) ;
4
+ const resolve = require ( 'resolve' ) ;
5
+ const tsconfigPaths = require ( 'tsconfig-paths' ) ;
6
+ const debug = require ( 'debug' ) ;
7
7
8
- const log = debug ( 'eslint-import-resolver-typescript' )
8
+ const log = debug ( 'eslint-import-resolver-typescript' ) ;
9
9
10
10
/**
11
11
* @param {string } source the module to resolve; i.e './some-module'
12
12
* @param {string } file the importing file's full path; i.e. '/usr/local/bin/file.js'
13
13
*/
14
14
function resolveFile ( source , file , config ) {
15
- log ( 'looking for:' , source )
15
+ log ( 'looking for:' , source ) ;
16
16
17
17
// don't worry about core node modules
18
18
if ( resolve . isCore ( source ) ) {
19
- log ( 'matched core:' , source )
19
+ log ( 'matched core:' , source ) ;
20
20
21
21
return {
22
22
found : true ,
23
23
path : null ,
24
- }
24
+ } ;
25
25
}
26
26
27
+ let foundTsPath = null ;
28
+ const extensions = Object . keys ( require . extensions ) . concat (
29
+ '.ts' ,
30
+ '.tsx' ,
31
+ '.d.ts' ,
32
+ ) ;
33
+
27
34
// setup tsconfig-paths
28
- const searchStart = config . directory || process . cwd ( )
29
- const configLoaderResult = tsconfigPaths . loadConfig ( searchStart )
30
- if ( configLoaderResult . resultType !== 'success' ) {
31
- throw new Error ( `Unable to find tsconfig in ${ searchStart } : ${ configLoaderResult . message } ` )
32
- }
33
- const matchPath = tsconfigPaths . createMatchPath (
34
- configLoaderResult . absoluteBaseUrl ,
35
- configLoaderResult . paths ,
36
- )
35
+ const searchStart = config . directory || process . cwd ( ) ;
36
+ const configLoaderResult = tsconfigPaths . loadConfig ( searchStart ) ;
37
+ if ( configLoaderResult . resultType === 'success' ) {
38
+ const matchPath = tsconfigPaths . createMatchPath (
39
+ configLoaderResult . absoluteBaseUrl ,
40
+ configLoaderResult . paths ,
41
+ ) ;
37
42
38
- // look for files based on setup tsconfig "paths"
39
- const extensions = Object . keys ( require . extensions ) . concat ( '.ts' , '.tsx' , '.d.ts' )
40
- const foundTsPath = matchPath (
41
- source ,
42
- undefined ,
43
- undefined ,
44
- extensions ,
45
- )
43
+ // look for files based on setup tsconfig "paths"
44
+ foundTsPath = matchPath ( source , undefined , undefined , extensions ) ;
46
45
47
- if ( foundTsPath ) {
48
- log ( 'matched ts path:' , foundTsPath )
46
+ if ( foundTsPath ) {
47
+ log ( 'matched ts path:' , foundTsPath ) ;
48
+ }
49
+ } else {
50
+ log ( 'failed to init tsconfig-paths:' , configLoaderResult . message ) ;
51
+ // this can happen if the user has problems with their tsconfig
52
+ // or if it's valid, but they don't have baseUrl set
49
53
}
50
54
51
55
// note that even if we match via tsconfig-paths, we still need to do a final resolve
@@ -54,28 +58,35 @@ function resolveFile(source, file, config) {
54
58
foundNodePath = resolve . sync ( foundTsPath || source , {
55
59
extensions,
56
60
basedir : path . dirname ( path . resolve ( file ) ) ,
57
- } )
61
+ packageFilter,
62
+ } ) ;
58
63
} catch ( err ) {
59
64
foundNodePath = null ;
60
65
}
61
66
62
67
if ( foundNodePath ) {
63
- log ( 'matched node path:' , foundNodePath )
68
+ log ( 'matched node path:' , foundNodePath ) ;
64
69
65
70
return {
66
71
found : true ,
67
72
path : foundNodePath ,
68
- }
73
+ } ;
69
74
}
70
75
71
- log ( 'didnt find' , source )
76
+ log ( 'didnt find' , source ) ;
72
77
73
78
return {
74
- found : false
79
+ found : false ,
80
+ } ;
81
+ }
82
+ function packageFilter ( pkg ) {
83
+ if ( pkg [ 'jsnext:main' ] ) {
84
+ pkg [ 'main' ] = pkg [ 'jsnext:main' ] ;
75
85
}
86
+ return pkg ;
76
87
}
77
88
78
89
module . exports = {
79
90
interfaceVersion : 2 ,
80
91
resolve : resolveFile ,
81
- }
92
+ } ;
0 commit comments