@@ -23,18 +23,19 @@ export function createParsedCommandLineByJson(
23
23
const proxyHost = proxyParseConfigHostForExtendConfigPaths ( parseConfigHost ) ;
24
24
ts . parseJsonConfigFileContent ( json , proxyHost . host , rootDir , { } , configFileName ) ;
25
25
26
- let vueOptions : Partial < VueCompilerOptions > = { } ;
26
+ const resolver = new CompilerOptionsResolver ( ) ;
27
27
28
28
for ( const extendPath of proxyHost . extendConfigPaths . reverse ( ) ) {
29
29
try {
30
- vueOptions = {
31
- ... vueOptions ,
32
- ... getPartialVueCompilerOptions ( ts , ts . readJsonConfigFile ( extendPath , proxyHost . host . readFile ) ) ,
33
- } ;
30
+ const configFile = ts . readJsonConfigFile ( extendPath , proxyHost . host . readFile ) ;
31
+ const obj = ts . convertToObject ( configFile , [ ] ) ;
32
+ const rawOptions : RawVueCompilerOptions = obj ?. vueCompilerOptions ?? { } ;
33
+ resolver . addConfig ( rawOptions , path . dirname ( configFile . fileName ) ) ;
34
34
} catch ( err ) { }
35
35
}
36
36
37
- const resolvedVueOptions = resolveVueCompilerOptions ( vueOptions ) ;
37
+ const resolvedVueOptions = resolver . build ( ) ;
38
+
38
39
if ( skipGlobalTypesSetup ) {
39
40
resolvedVueOptions . __setupedGlobalTypes = true ;
40
41
}
@@ -78,18 +79,19 @@ export function createParsedCommandLine(
78
79
const config = ts . readJsonConfigFile ( tsConfigPath , proxyHost . host . readFile ) ;
79
80
ts . parseJsonSourceFileConfigFileContent ( config , proxyHost . host , path . dirname ( tsConfigPath ) , { } , tsConfigPath ) ;
80
81
81
- let vueOptions : Partial < VueCompilerOptions > = { } ;
82
+ const resolver = new CompilerOptionsResolver ( ) ;
82
83
83
84
for ( const extendPath of proxyHost . extendConfigPaths . reverse ( ) ) {
84
85
try {
85
- vueOptions = {
86
- ... vueOptions ,
87
- ... getPartialVueCompilerOptions ( ts , ts . readJsonConfigFile ( extendPath , proxyHost . host . readFile ) ) ,
88
- } ;
86
+ const configFile = ts . readJsonConfigFile ( extendPath , proxyHost . host . readFile ) ;
87
+ const obj = ts . convertToObject ( configFile , [ ] ) ;
88
+ const rawOptions : RawVueCompilerOptions = obj ?. vueCompilerOptions ?? { } ;
89
+ resolver . addConfig ( rawOptions , path . dirname ( configFile . fileName ) ) ;
89
90
} catch ( err ) { }
90
91
}
91
92
92
- const resolvedVueOptions = resolveVueCompilerOptions ( vueOptions ) ;
93
+ const resolvedVueOptions = resolver . build ( ) ;
94
+
93
95
if ( skipGlobalTypesSetup ) {
94
96
resolvedVueOptions . __setupedGlobalTypes = true ;
95
97
}
@@ -126,7 +128,7 @@ export function createParsedCommandLine(
126
128
return {
127
129
fileNames : [ ] ,
128
130
options : { } ,
129
- vueOptions : resolveVueCompilerOptions ( { } ) ,
131
+ vueOptions : getDefaultCompilerOptions ( ) ,
130
132
errors : [ ] ,
131
133
} ;
132
134
}
@@ -153,76 +155,115 @@ function proxyParseConfigHostForExtendConfigPaths(parseConfigHost: ts.ParseConfi
153
155
} ;
154
156
}
155
157
156
- function getPartialVueCompilerOptions (
157
- ts : typeof import ( 'typescript' ) ,
158
- tsConfigSourceFile : ts . TsConfigSourceFile
159
- ) {
160
-
161
- const folder = path . dirname ( tsConfigSourceFile . fileName ) ;
162
- const obj = ts . convertToObject ( tsConfigSourceFile , [ ] ) ;
163
- const rawOptions : RawVueCompilerOptions = obj ?. vueCompilerOptions ?? { } ;
164
- const result : Partial < VueCompilerOptions > = {
165
- ...rawOptions as any ,
166
- } ;
167
- const target = rawOptions . target ?? 'auto' ;
158
+ export class CompilerOptionsResolver {
159
+ options : Omit < RawVueCompilerOptions , 'target' | 'plugin' > = { } ;
160
+ fallbackTarget : number | undefined ;
161
+ target : number | undefined ;
162
+ plugins : VueLanguagePlugin [ ] = [ ] ;
168
163
169
- if ( target === 'auto' ) {
170
- const resolvedPath = resolvePath ( 'vue/package.json' ) ;
171
- if ( resolvedPath ) {
172
- const vuePackageJson = require ( resolvedPath ) ;
173
- const versionNumbers = vuePackageJson . version . split ( '.' ) ;
174
- result . target = Number ( versionNumbers [ 0 ] + '.' + versionNumbers [ 1 ] ) ;
164
+ addConfig ( options : RawVueCompilerOptions , rootDir : string ) {
165
+ for ( const key in options ) {
166
+ switch ( key ) {
167
+ case 'target' :
168
+ const target = options . target ! ;
169
+ if ( typeof target === 'string' ) {
170
+ this . target = findVueVersion ( rootDir ) ;
171
+ }
172
+ else {
173
+ this . target = target ;
174
+ }
175
+ break ;
176
+ case 'plugins' :
177
+ this . plugins = ( options . plugins ?? [ ] )
178
+ . map < VueLanguagePlugin > ( ( pluginPath : string ) => {
179
+ try {
180
+ const resolvedPath = resolvePath ( pluginPath , rootDir ) ;
181
+ if ( resolvedPath ) {
182
+ const plugin = require ( resolvedPath ) ;
183
+ plugin . __moduleName = pluginPath ;
184
+ return plugin ;
185
+ }
186
+ else {
187
+ console . warn ( '[Vue] Load plugin failed:' , pluginPath ) ;
188
+ }
189
+ }
190
+ catch ( error ) {
191
+ console . warn ( '[Vue] Resolve plugin path failed:' , pluginPath , error ) ;
192
+ }
193
+ return [ ] ;
194
+ } ) ;
195
+ break ;
196
+ default :
197
+ // @ts -expect-error
198
+ this . options [ key ] = options [ key ] ;
199
+ break ;
200
+ }
175
201
}
176
- else {
177
- // console.warn('Load vue/package.json failed from', folder );
202
+ if ( this . target === undefined ) {
203
+ this . fallbackTarget = findVueVersion ( rootDir ) ;
178
204
}
179
205
}
180
- else {
181
- result . target = target ;
182
- }
183
- if ( rawOptions . plugins ) {
184
- const plugins = rawOptions . plugins
185
- . map < VueLanguagePlugin > ( ( pluginPath : string ) => {
186
- try {
187
- const resolvedPath = resolvePath ( pluginPath ) ;
188
- if ( resolvedPath ) {
189
- const plugin = require ( resolvedPath ) ;
190
- plugin . __moduleName = pluginPath ;
191
- return plugin ;
192
- }
193
- else {
194
- console . warn ( '[Vue] Load plugin failed:' , pluginPath ) ;
206
+
207
+ build ( defaults ?: VueCompilerOptions ) : VueCompilerOptions {
208
+ const target = this . target ?? this . fallbackTarget ;
209
+ defaults ??= getDefaultCompilerOptions ( target , this . options . lib ) ;
210
+ return {
211
+ ...defaults ,
212
+ ...this . options ,
213
+ plugins : this . plugins ,
214
+ macros : {
215
+ ...defaults . macros ,
216
+ ...this . options . macros ,
217
+ } ,
218
+ composables : {
219
+ ...defaults . composables ,
220
+ ...this . options . composables ,
221
+ } ,
222
+ // https://github.com/vuejs/vue-next/blob/master/packages/compiler-dom/src/transforms/vModel.ts#L49-L51
223
+ // https://vuejs.org/guide/essentials/forms.html#form-input-bindings
224
+ experimentalModelPropName : Object . fromEntries ( Object . entries (
225
+ this . options . experimentalModelPropName ?? defaults . experimentalModelPropName ?? {
226
+ '' : {
227
+ input : true
228
+ } ,
229
+ value : {
230
+ input : { type : 'text' } ,
231
+ textarea : true ,
232
+ select : true
195
233
}
196
234
}
197
- catch ( error ) {
198
- console . warn ( '[Vue] Resolve plugin path failed:' , pluginPath , error ) ;
199
- }
200
- return [ ] ;
201
- } ) ;
202
-
203
- result . plugins = plugins ;
235
+ ) . map ( ( [ k , v ] ) => [ camelize ( k ) , v ] ) ) ,
236
+ } ;
204
237
}
238
+ }
205
239
206
- return result ;
240
+ function findVueVersion ( rootDir : string ) {
241
+ const resolvedPath = resolvePath ( 'vue/package.json' , rootDir ) ;
242
+ if ( resolvedPath ) {
243
+ const vuePackageJson = require ( resolvedPath ) ;
244
+ const versionNumbers = vuePackageJson . version . split ( '.' ) ;
245
+ return Number ( versionNumbers [ 0 ] + '.' + versionNumbers [ 1 ] ) ;
246
+ }
247
+ else {
248
+ // console.warn('Load vue/package.json failed from', folder);
249
+ }
250
+ }
207
251
208
- function resolvePath ( scriptPath : string ) {
209
- try {
210
- if ( require ?. resolve ) {
211
- return require . resolve ( scriptPath , { paths : [ folder ] } ) ;
212
- }
213
- else {
214
- // console.warn('failed to resolve path:', scriptPath, 'require.resolve is not supported in web');
215
- }
252
+ function resolvePath ( scriptPath : string , root : string ) {
253
+ try {
254
+ if ( require ?. resolve ) {
255
+ return require . resolve ( scriptPath , { paths : [ root ] } ) ;
216
256
}
217
- catch ( error ) {
218
- // console.warn(error );
257
+ else {
258
+ // console.warn('failed to resolve path:', scriptPath, 'require.resolve is not supported in web' );
219
259
}
220
260
}
261
+ catch ( error ) {
262
+ // console.warn(error);
263
+ }
221
264
}
222
265
223
- function getDefaultOptions ( options : Partial < VueCompilerOptions > ) : VueCompilerOptions {
224
- const target = options . target ?? 3.3 ;
225
- const lib = options . lib ?? 'vue' ;
266
+ export function getDefaultCompilerOptions ( target = 99 , lib = 'vue' ) : VueCompilerOptions {
226
267
return {
227
268
target,
228
269
lib,
@@ -258,38 +299,15 @@ function getDefaultOptions(options: Partial<VueCompilerOptions>): VueCompilerOpt
258
299
experimentalResolveStyleCssClasses : 'scoped' ,
259
300
experimentalModelPropName : null !
260
301
} ;
261
- } ;
302
+ }
262
303
263
- export function resolveVueCompilerOptions (
264
- options : Partial < VueCompilerOptions > ,
265
- defaults : VueCompilerOptions = getDefaultOptions ( options )
266
- ) : VueCompilerOptions {
304
+ /**
305
+ * @deprecated use `getDefaultCompilerOptions` instead
306
+ */
307
+ export function resolveVueCompilerOptions ( options : Partial < VueCompilerOptions > ) : VueCompilerOptions {
267
308
return {
268
- ...defaults ,
309
+ ...getDefaultCompilerOptions ( options . target , options . lib ) ,
269
310
...options ,
270
- macros : {
271
- ...defaults . macros ,
272
- ...options . macros ,
273
- } ,
274
- composables : {
275
- ...defaults . composables ,
276
- ...options . composables ,
277
- } ,
278
-
279
- // https://github.com/vuejs/vue-next/blob/master/packages/compiler-dom/src/transforms/vModel.ts#L49-L51
280
- // https://vuejs.org/guide/essentials/forms.html#form-input-bindings
281
- experimentalModelPropName : Object . fromEntries ( Object . entries (
282
- options . experimentalModelPropName ?? defaults . experimentalModelPropName ?? {
283
- '' : {
284
- input : true
285
- } ,
286
- value : {
287
- input : { type : 'text' } ,
288
- textarea : true ,
289
- select : true
290
- }
291
- }
292
- ) . map ( ( [ k , v ] ) => [ camelize ( k ) , v ] ) ) ,
293
311
} ;
294
312
}
295
313
0 commit comments