@@ -3,17 +3,21 @@ import type * as ts from 'typescript';
3
3
import * as path from 'path-browserify' ;
4
4
import type { RawVueCompilerOptions , VueCompilerOptions , VueLanguagePlugin } from '../types' ;
5
5
import { getAllExtensions } from '../languagePlugin' ;
6
+ import { generateGlobalTypes } from '../codegen/globalTypes' ;
6
7
7
8
export type ParsedCommandLine = ts . ParsedCommandLine & {
8
9
vueOptions : VueCompilerOptions ;
9
10
} ;
10
11
11
12
export function createParsedCommandLineByJson (
12
13
ts : typeof import ( 'typescript' ) ,
13
- parseConfigHost : ts . ParseConfigHost ,
14
+ parseConfigHost : ts . ParseConfigHost & {
15
+ writeFile ?( path : string , data : string ) : void ;
16
+ } ,
14
17
rootDir : string ,
15
18
json : any ,
16
- configFileName = rootDir + '/jsconfig.json'
19
+ configFileName = rootDir + '/jsconfig.json' ,
20
+ skipGlobalTypesSetup = false
17
21
) : ParsedCommandLine {
18
22
19
23
const proxyHost = proxyParseConfigHostForExtendConfigPaths ( parseConfigHost ) ;
@@ -31,6 +35,12 @@ export function createParsedCommandLineByJson(
31
35
}
32
36
33
37
const resolvedVueOptions = resolveVueCompilerOptions ( vueOptions ) ;
38
+ if ( skipGlobalTypesSetup ) {
39
+ resolvedVueOptions . __setupedGlobalTypes = true ;
40
+ }
41
+ else {
42
+ resolvedVueOptions . __setupedGlobalTypes = setupGlobalTypes ( rootDir , resolvedVueOptions , parseConfigHost ) ;
43
+ }
34
44
const parsed = ts . parseJsonConfigFileContent (
35
45
json ,
36
46
proxyHost . host ,
@@ -60,7 +70,8 @@ export function createParsedCommandLineByJson(
60
70
export function createParsedCommandLine (
61
71
ts : typeof import ( 'typescript' ) ,
62
72
parseConfigHost : ts . ParseConfigHost ,
63
- tsConfigPath : string
73
+ tsConfigPath : string ,
74
+ skipGlobalTypesSetup = false
64
75
) : ParsedCommandLine {
65
76
try {
66
77
const proxyHost = proxyParseConfigHostForExtendConfigPaths ( parseConfigHost ) ;
@@ -79,6 +90,12 @@ export function createParsedCommandLine(
79
90
}
80
91
81
92
const resolvedVueOptions = resolveVueCompilerOptions ( vueOptions ) ;
93
+ if ( skipGlobalTypesSetup ) {
94
+ resolvedVueOptions . __setupedGlobalTypes = true ;
95
+ }
96
+ else {
97
+ resolvedVueOptions . __setupedGlobalTypes = setupGlobalTypes ( path . dirname ( tsConfigPath ) , resolvedVueOptions , parseConfigHost ) ;
98
+ }
82
99
const parsed = ts . parseJsonSourceFileConfigFileContent (
83
100
config ,
84
101
proxyHost . host ,
@@ -260,3 +277,28 @@ export function resolveVueCompilerOptions(vueOptions: Partial<VueCompilerOptions
260
277
) . map ( ( [ k , v ] ) => [ camelize ( k ) , v ] ) ) ,
261
278
} ;
262
279
}
280
+
281
+ export function setupGlobalTypes ( rootDir : string , vueOptions : VueCompilerOptions , host : {
282
+ fileExists ( path : string ) : boolean ;
283
+ writeFile ?( path : string , data : string ) : void ;
284
+ } ) {
285
+ if ( ! host . writeFile ) {
286
+ return false ;
287
+ }
288
+ try {
289
+ let dir = rootDir ;
290
+ while ( ! host . fileExists ( path . resolve ( dir , `node_modules/${ vueOptions . lib } /package.json` ) ) ) {
291
+ const parentDir = path . resolve ( dir , '..' ) ;
292
+ if ( dir === parentDir ) {
293
+ throw 0 ;
294
+ }
295
+ dir = parentDir ;
296
+ }
297
+ const globalTypesPath = path . resolve ( dir , `node_modules/.vue-global-types/${ vueOptions . lib } _${ vueOptions . target } _${ vueOptions . strictTemplates } .d.ts` ) ;
298
+ const globalTypesContents = generateGlobalTypes ( 'global' , vueOptions . lib , vueOptions . target , vueOptions . strictTemplates ) ;
299
+ host . writeFile ( globalTypesPath , globalTypesContents ) ;
300
+ return true ;
301
+ } catch {
302
+ return false ;
303
+ }
304
+ }
0 commit comments