1
- import { dirname } from 'path' ;
1
+ import { dirname , isAbsolute , join } from 'path' ;
2
2
3
3
import ts from 'typescript' ;
4
4
@@ -53,6 +53,53 @@ const importTransformer: ts.TransformerFactory<ts.SourceFile> = (context) => {
53
53
return ( node ) => ts . visitNode ( node , visit ) ;
54
54
} ;
55
55
56
+ export function loadTsconfig (
57
+ compilerOptionsJSON : any ,
58
+ filename : string ,
59
+ tsOptions : Options . Typescript ,
60
+ ) {
61
+ if ( typeof tsOptions . tsconfigFile === 'boolean' ) {
62
+ return { errors : [ ] , options : compilerOptionsJSON } ;
63
+ }
64
+
65
+ let basePath = process . cwd ( ) ;
66
+
67
+ const fileDirectory = ( tsOptions . tsconfigDirectory ||
68
+ dirname ( filename ) ) as string ;
69
+
70
+ let tsconfigFile =
71
+ tsOptions . tsconfigFile ||
72
+ ts . findConfigFile ( fileDirectory , ts . sys . fileExists ) ;
73
+
74
+ tsconfigFile = isAbsolute ( tsconfigFile )
75
+ ? tsconfigFile
76
+ : join ( basePath , tsconfigFile ) ;
77
+
78
+ basePath = dirname ( tsconfigFile ) ;
79
+
80
+ const { error, config } = ts . readConfigFile ( tsconfigFile , ts . sys . readFile ) ;
81
+
82
+ if ( error ) {
83
+ throw new Error ( formatDiagnostics ( error , basePath ) ) ;
84
+ }
85
+
86
+ // Do this so TS will not search for initial files which might take a while
87
+ config . include = [ ] ;
88
+
89
+ let { errors, options } = ts . parseJsonConfigFileContent (
90
+ config ,
91
+ ts . sys ,
92
+ basePath ,
93
+ compilerOptionsJSON ,
94
+ tsconfigFile ,
95
+ ) ;
96
+
97
+ // Filter out "no files found error"
98
+ errors = errors . filter ( ( d ) => d . code !== 18003 ) ;
99
+
100
+ return { errors, options } ;
101
+ }
102
+
56
103
const transformer : Transformer < Options . Typescript > = ( {
57
104
content,
58
105
filename,
@@ -64,38 +111,14 @@ const transformer: Transformer<Options.Typescript> = ({
64
111
target : 'es6' ,
65
112
} ;
66
113
67
- let basePath = process . cwd ( ) ;
68
-
69
- if ( options . tsconfigFile !== false || options . tsconfigDirectory ) {
70
- const fileDirectory = ( options . tsconfigDirectory ||
71
- dirname ( filename ) ) as string ;
72
-
73
- const tsconfigFile =
74
- options . tsconfigFile ||
75
- ts . findConfigFile ( fileDirectory , ts . sys . fileExists ) ;
76
-
77
- if ( typeof tsconfigFile === 'string' ) {
78
- basePath = dirname ( tsconfigFile ) ;
79
-
80
- const { error, config } = ts . readConfigFile (
81
- tsconfigFile ,
82
- ts . sys . readFile ,
83
- ) ;
84
-
85
- if ( error ) {
86
- throw new Error ( formatDiagnostics ( error , basePath ) ) ;
87
- }
88
-
89
- Object . assign ( compilerOptionsJSON , config . compilerOptions ) ;
90
- }
91
- }
114
+ const basePath = process . cwd ( ) ;
92
115
93
116
Object . assign ( compilerOptionsJSON , options . compilerOptions ) ;
94
117
95
- const {
96
- errors ,
97
- options : convertedCompilerOptions ,
98
- } = ts . convertCompilerOptionsFromJson ( compilerOptionsJSON , basePath ) ;
118
+ const { errors , options : convertedCompilerOptions } =
119
+ options . tsconfigFile !== false || options . tsconfigDirectory
120
+ ? loadTsconfig ( compilerOptionsJSON , filename , options )
121
+ : ts . convertCompilerOptionsFromJson ( compilerOptionsJSON , basePath ) ;
99
122
100
123
if ( errors . length ) {
101
124
throw new Error ( formatDiagnostics ( errors , basePath ) ) ;
0 commit comments