@@ -6,7 +6,7 @@ import { Transformer, Options } from '../typings';
6
6
7
7
type CompilerOptions = Options . Typescript [ 'compilerOptions' ] ;
8
8
9
- function createFormatDiagnosticsHost ( cwd : string ) {
9
+ function createFormatDiagnosticsHost ( cwd : string ) : ts . FormatDiagnosticsHost {
10
10
return {
11
11
getCanonicalFileName : ( fileName : string ) => fileName ,
12
12
getCurrentDirectory : ( ) => cwd ,
@@ -63,28 +63,20 @@ function isValidSvelteImportDiagnostic(filename: string, diagnostic: any) {
63
63
return existsSync ( importeePath ) === false ;
64
64
}
65
65
66
- const TS_TRANSFORMERS = {
67
- before : [
68
- ( context : any ) => {
69
- const visit = ( node : ts . Node ) : ts . VisitResult < ts . Node > => {
70
- if ( ts . isImportDeclaration ( node ) ) {
71
- const importedFilename = node . moduleSpecifier . getText ( ) . slice ( 1 , - 1 ) ;
72
- // istanbul ignore else
73
- if ( isSvelteFile ( importedFilename ) ) {
74
- return ts . createImportDeclaration (
75
- node . decorators ,
76
- node . modifiers ,
77
- node . importClause ,
78
- node . moduleSpecifier ,
79
- ) ;
80
- }
81
- }
82
- return ts . visitEachChild ( node , child => visit ( child ) , context ) ;
83
- } ;
84
-
85
- return ( node : any ) => ts . visitNode ( node , visit ) ;
86
- } ,
87
- ] ,
66
+ const importTransformer : ts . TransformerFactory < ts . SourceFile > = context => {
67
+ const visit : ts . Visitor = node => {
68
+ if ( ts . isImportDeclaration ( node ) ) {
69
+ return ts . createImportDeclaration (
70
+ node . decorators ,
71
+ node . modifiers ,
72
+ node . importClause ,
73
+ node . moduleSpecifier ,
74
+ ) ;
75
+ }
76
+ return ts . visitEachChild ( node , child => visit ( child ) , context ) ;
77
+ } ;
78
+
79
+ return node => ts . visitNode ( node , visit ) ;
88
80
} ;
89
81
90
82
function compileFileFromMemory (
@@ -96,62 +88,52 @@ function compileFileFromMemory(
96
88
97
89
const realHost = ts . createCompilerHost ( compilerOptions , true ) ;
98
90
const dummyFilePath = filename ;
99
- const dummySourceFile = ts . createSourceFile (
100
- dummyFilePath ,
101
- code ,
102
- ts . ScriptTarget . Latest ,
103
- ) ;
104
91
105
- const host = {
106
- fileExists : ( filePath : string ) =>
92
+ const host : ts . CompilerHost = {
93
+ fileExists : filePath =>
107
94
filePath === dummyFilePath || realHost . fileExists ( filePath ) ,
108
- directoryExists :
109
- realHost . directoryExists && realHost . directoryExists . bind ( realHost ) ,
110
- getCurrentDirectory : realHost . getCurrentDirectory . bind ( realHost ) ,
111
- getDirectories : realHost . getDirectories . bind ( realHost ) ,
112
- getCanonicalFileName : ( fileName : string ) =>
113
- realHost . getCanonicalFileName ( fileName ) ,
114
- getNewLine : realHost . getNewLine . bind ( realHost ) ,
115
- getDefaultLibFileName : realHost . getDefaultLibFileName . bind ( realHost ) ,
95
+ getCanonicalFileName : fileName => realHost . getCanonicalFileName ( fileName ) ,
116
96
getSourceFile : (
117
- fileName : string ,
118
- languageVersion : ts . ScriptTarget ,
119
- onError : ( ) => any ,
120
- shouldCreateNewSourceFile : boolean ,
97
+ fileName ,
98
+ languageVersion ,
99
+ onError ,
100
+ shouldCreateNewSourceFile ,
121
101
) =>
122
102
fileName === dummyFilePath
123
- ? dummySourceFile
103
+ ? ts . createSourceFile ( dummyFilePath , code , languageVersion )
124
104
: realHost . getSourceFile (
125
105
fileName ,
126
106
languageVersion ,
127
107
onError ,
128
108
shouldCreateNewSourceFile ,
129
109
) ,
130
- readFile : ( filePath : string ) =>
110
+ readFile : filePath =>
131
111
// istanbul ignore next
132
112
filePath === dummyFilePath ? content : realHost . readFile ( filePath ) ,
133
- useCaseSensitiveFileNames : ( ) => realHost . useCaseSensitiveFileNames ( ) ,
134
- writeFile : ( fileName : string , data : string ) => {
113
+ writeFile : ( fileName , data ) => {
135
114
if ( fileName . endsWith ( '.map' ) ) {
136
115
map = data ;
137
116
} else {
138
117
code = data ;
139
118
}
140
119
} ,
120
+ directoryExists :
121
+ realHost . directoryExists && realHost . directoryExists . bind ( realHost ) ,
122
+ getCurrentDirectory : realHost . getCurrentDirectory . bind ( realHost ) ,
123
+ getDirectories : realHost . getDirectories . bind ( realHost ) ,
124
+ getNewLine : realHost . getNewLine . bind ( realHost ) ,
125
+ getDefaultLibFileName : realHost . getDefaultLibFileName . bind ( realHost ) ,
126
+ resolveModuleNames :
127
+ realHost . resolveModuleNames && realHost . resolveModuleNames . bind ( realHost ) ,
128
+ useCaseSensitiveFileNames : realHost . useCaseSensitiveFileNames . bind (
129
+ realHost ,
130
+ ) ,
141
131
} ;
142
132
143
- const program = ts . createProgram (
144
- [ dummyFilePath ] ,
145
- compilerOptions ,
146
- ( host as any ) as ts . CompilerHost ,
147
- ) ;
148
- const emitResult = program . emit (
149
- undefined ,
150
- undefined ,
151
- undefined ,
152
- undefined ,
153
- TS_TRANSFORMERS ,
154
- ) ;
133
+ const program = ts . createProgram ( [ dummyFilePath ] , compilerOptions , host ) ;
134
+ const emitResult = program . emit ( undefined , undefined , undefined , undefined , {
135
+ before : [ importTransformer ] ,
136
+ } ) ;
155
137
156
138
// collect diagnostics without svelte import errors
157
139
const diagnostics = [
@@ -170,19 +152,19 @@ const transformer: Transformer<Options.Typescript> = ({
170
152
// default options
171
153
const compilerOptionsJSON = {
172
154
moduleResolution : 'node' ,
173
- sourceMap : true ,
174
- strict : true ,
175
155
target : 'es6' ,
176
156
} ;
157
+
177
158
let basePath = process . cwd ( ) ;
178
159
179
160
if ( options . tsconfigFile !== false || options . tsconfigDirectory ) {
180
161
const fileDirectory = ( options . tsconfigDirectory ||
181
162
dirname ( filename ) ) as string ;
182
- const tsconfigFile = ( options . tsconfigFile ||
183
- ts . findConfigFile ( fileDirectory , ts . sys . fileExists ) ) as string ;
163
+ const tsconfigFile =
164
+ options . tsconfigFile ||
165
+ ts . findConfigFile ( fileDirectory , ts . sys . fileExists ) ;
184
166
185
- if ( tsconfigFile ) {
167
+ if ( typeof tsconfigFile === 'string' ) {
186
168
basePath = dirname ( tsconfigFile ) ;
187
169
188
170
const { error, config } = ts . readConfigFile (
0 commit comments