@@ -54,8 +54,8 @@ export class TSService {
54
54
private readonly fileWatchCallbacks = new Map < string , ( ) => void > ( ) ;
55
55
56
56
public constructor ( tsconfigPath : string , extraFileExtensions : string [ ] ) {
57
- this . watch = this . createWatch ( tsconfigPath , extraFileExtensions ) ;
58
57
this . extraFileExtensions = extraFileExtensions ;
58
+ this . watch = this . createWatch ( tsconfigPath , extraFileExtensions ) ;
59
59
}
60
60
61
61
public getProgram ( code : string , filePath : string ) : ts . Program {
@@ -84,6 +84,7 @@ export class TSService {
84
84
tsconfigPath : string ,
85
85
extraFileExtensions : string [ ]
86
86
) : ts . WatchOfConfigFile < ts . BuilderProgram > {
87
+ const normalizedTsconfigPath = normalizeFileName ( tsconfigPath ) ;
87
88
const watchCompilerHost = ts . createWatchCompilerHost (
88
89
tsconfigPath ,
89
90
{
@@ -103,18 +104,31 @@ export class TSService {
103
104
( ) => {
104
105
// Not reported in reportWatchStatus.
105
106
} ,
106
- undefined ,
107
- extraFileExtensions . map ( ( extension ) => ( {
108
- extension,
109
- isMixedContent : true ,
110
- scriptKind : ts . ScriptKind . Deferred ,
111
- } ) )
107
+ undefined
108
+ // extraFileExtensions.map((extension) => ({
109
+ // extension,
110
+ // isMixedContent: true,
111
+ // scriptKind: ts.ScriptKind.Deferred,
112
+ // }))
112
113
) ;
113
114
const original = {
114
115
// eslint-disable-next-line @typescript-eslint/unbound-method -- Store original
115
116
readFile : watchCompilerHost . readFile ,
116
117
// eslint-disable-next-line @typescript-eslint/unbound-method -- Store original
117
118
fileExists : watchCompilerHost . fileExists ,
119
+ // eslint-disable-next-line @typescript-eslint/unbound-method -- Store original
120
+ readDirectory : watchCompilerHost . readDirectory ,
121
+ } ;
122
+ watchCompilerHost . readDirectory = ( ...args ) => {
123
+ const results = original . readDirectory . call ( watchCompilerHost , ...args ) ;
124
+
125
+ return [
126
+ ...new Set (
127
+ results . map ( ( result ) =>
128
+ toVirtualTSXlFileName ( result , extraFileExtensions )
129
+ )
130
+ ) ,
131
+ ] ;
118
132
} ;
119
133
watchCompilerHost . readFile = ( fileName , ...args ) => {
120
134
const realFileName = toRealFileName ( fileName , extraFileExtensions ) ;
@@ -127,19 +141,40 @@ export class TSService {
127
141
} ) ;
128
142
}
129
143
130
- const code = original . readFile . call ( this , realFileName , ...args ) ;
131
- return (
132
- code &&
133
- transformExtraFile ( code , {
134
- filePath : normalized ,
135
- current : false ,
136
- } )
144
+ const code = original . readFile . call (
145
+ watchCompilerHost ,
146
+ realFileName ,
147
+ ...args
137
148
) ;
149
+ if ( ! code ) {
150
+ return code ;
151
+ }
152
+ if ( normalizedTsconfigPath === normalized ) {
153
+ const configJson = ts . parseConfigFileTextToJson ( realFileName , code ) ;
154
+ if ( ! configJson . config ) {
155
+ return code ;
156
+ }
157
+ let include = undefined ;
158
+
159
+ if ( configJson . config . include ) {
160
+ include = [ configJson . config . include ]
161
+ . flat ( )
162
+ . map ( ( s ) => toVirtualTSXlFileName ( s , extraFileExtensions ) ) ;
163
+ }
164
+ return JSON . stringify ( {
165
+ ...configJson . config ,
166
+ include,
167
+ } ) ;
168
+ }
169
+ return transformExtraFile ( code , {
170
+ filePath : normalized ,
171
+ current : false ,
172
+ } ) ;
138
173
} ;
139
174
// Modify it so that it can be determined that the virtual file actually exists.
140
175
watchCompilerHost . fileExists = ( fileName , ...args ) =>
141
176
original . fileExists . call (
142
- this ,
177
+ watchCompilerHost ,
143
178
toRealFileName ( fileName , extraFileExtensions ) ,
144
179
...args
145
180
) ;
@@ -197,6 +232,19 @@ function getFileNamesIncludingVirtualTSX(
197
232
return [ fileName ] ;
198
233
}
199
234
235
+ /** If the given filename has extra file extensions, returns the real virtual filename. */
236
+ function toVirtualTSXlFileName (
237
+ fileName : string ,
238
+ extraFileExtensions : string [ ]
239
+ ) {
240
+ for ( const extraFileExtension of extraFileExtensions ) {
241
+ if ( fileName . endsWith ( extraFileExtension ) ) {
242
+ return `${ fileName } .tsx` ;
243
+ }
244
+ }
245
+ return fileName ;
246
+ }
247
+
200
248
/** If the given filename is a virtual filename (.vue.tsx), returns the real filename. */
201
249
function toRealFileName ( fileName : string , extraFileExtensions : string [ ] ) {
202
250
for ( const extraFileExtension of extraFileExtensions ) {
0 commit comments