1- 'use strict'
1+ 'use strict' ;
22
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' ) ;
77
8- const log = debug ( 'eslint-import-resolver-typescript' )
8+ const log = debug ( 'eslint-import-resolver-typescript' ) ;
99
1010/**
1111 * @param {string } source the module to resolve; i.e './some-module'
1212 * @param {string } file the importing file's full path; i.e. '/usr/local/bin/file.js'
1313 */
1414function resolveFile ( source , file , config ) {
15- log ( 'looking for:' , source )
15+ log ( 'looking for:' , source ) ;
1616
1717 // don't worry about core node modules
1818 if ( resolve . isCore ( source ) ) {
19- log ( 'matched core:' , source )
19+ log ( 'matched core:' , source ) ;
2020
2121 return {
2222 found : true ,
2323 path : null ,
24- }
24+ } ;
2525 }
2626
27+ let foundTsPath = null ;
28+ const extensions = Object . keys ( require . extensions ) . concat (
29+ '.ts' ,
30+ '.tsx' ,
31+ '.d.ts' ,
32+ ) ;
33+
2734 // 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+ ) ;
3742
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 ) ;
4645
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
4953 }
5054
5155 // 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) {
5458 foundNodePath = resolve . sync ( foundTsPath || source , {
5559 extensions,
5660 basedir : path . dirname ( path . resolve ( file ) ) ,
57- } )
61+ packageFilter,
62+ } ) ;
5863 } catch ( err ) {
5964 foundNodePath = null ;
6065 }
6166
6267 if ( foundNodePath ) {
63- log ( 'matched node path:' , foundNodePath )
68+ log ( 'matched node path:' , foundNodePath ) ;
6469
6570 return {
6671 found : true ,
6772 path : foundNodePath ,
68- }
73+ } ;
6974 }
7075
71- log ( 'didnt find' , source )
76+ log ( 'didnt find' , source ) ;
7277
7378 return {
74- found : false
79+ found : false ,
80+ } ;
81+ }
82+ function packageFilter ( pkg ) {
83+ if ( pkg [ 'jsnext:main' ] ) {
84+ pkg [ 'main' ] = pkg [ 'jsnext:main' ] ;
7585 }
86+ return pkg ;
7687}
7788
7889module . exports = {
7990 interfaceVersion : 2 ,
8091 resolve : resolveFile ,
81- }
92+ } ;
0 commit comments