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,51 @@ 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
+ tsconfigFile = isAbsolute ( tsconfigFile )
74
+ ? tsconfigFile
75
+ : join ( basePath , tsconfigFile ) ;
76
+
77
+ basePath = dirname ( tsconfigFile ) ;
78
+
79
+ const { error, config } = ts . readConfigFile ( tsconfigFile , ts . sys . readFile ) ;
80
+
81
+ if ( error ) {
82
+ throw new Error ( formatDiagnostics ( error , basePath ) ) ;
83
+ }
84
+
85
+ // Do this so TS will not search for initial files which might take a while
86
+ config . include = [ ] ;
87
+
88
+ let { errors, options } = ts . parseJsonConfigFileContent (
89
+ config ,
90
+ ts . sys ,
91
+ basePath ,
92
+ compilerOptionsJSON ,
93
+ tsconfigFile ,
94
+ ) ;
95
+ // Filter out "no files found error"
96
+ errors = errors . filter ( ( d ) => d . code !== 18003 ) ;
97
+
98
+ return { errors, options } ;
99
+ }
100
+
56
101
const transformer : Transformer < Options . Typescript > = ( {
57
102
content,
58
103
filename,
@@ -65,37 +110,12 @@ const transformer: Transformer<Options.Typescript> = ({
65
110
} ;
66
111
67
112
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
- }
92
-
93
113
Object . assign ( compilerOptionsJSON , options . compilerOptions ) ;
94
114
95
- const {
96
- errors ,
97
- options : convertedCompilerOptions ,
98
- } = ts . convertCompilerOptionsFromJson ( compilerOptionsJSON , basePath ) ;
115
+ const { errors , options : convertedCompilerOptions } =
116
+ options . tsconfigFile !== false || options . tsconfigDirectory
117
+ ? loadTsconfig ( compilerOptionsJSON , filename , options )
118
+ : ts . convertCompilerOptionsFromJson ( compilerOptionsJSON , basePath ) ;
99
119
100
120
if ( errors . length ) {
101
121
throw new Error ( formatDiagnostics ( errors , basePath ) ) ;
0 commit comments