@@ -3,17 +3,21 @@ import type * as ts from 'typescript';
33import * as path from 'path-browserify' ;
44import type { RawVueCompilerOptions , VueCompilerOptions , VueLanguagePlugin } from '../types' ;
55import { getAllExtensions } from '../languagePlugin' ;
6+ import { generateGlobalTypes } from '../codegen/globalTypes' ;
67
78export type ParsedCommandLine = ts . ParsedCommandLine & {
89 vueOptions : VueCompilerOptions ;
910} ;
1011
1112export function createParsedCommandLineByJson (
1213 ts : typeof import ( 'typescript' ) ,
13- parseConfigHost : ts . ParseConfigHost ,
14+ parseConfigHost : ts . ParseConfigHost & {
15+ writeFile ?( path : string , data : string ) : void ;
16+ } ,
1417 rootDir : string ,
1518 json : any ,
16- configFileName = rootDir + '/jsconfig.json'
19+ configFileName = rootDir + '/jsconfig.json' ,
20+ skipGlobalTypesSetup = false
1721) : ParsedCommandLine {
1822
1923 const proxyHost = proxyParseConfigHostForExtendConfigPaths ( parseConfigHost ) ;
@@ -31,6 +35,12 @@ export function createParsedCommandLineByJson(
3135 }
3236
3337 const resolvedVueOptions = resolveVueCompilerOptions ( vueOptions ) ;
38+ if ( skipGlobalTypesSetup ) {
39+ resolvedVueOptions . __setupedGlobalTypes = true ;
40+ }
41+ else {
42+ resolvedVueOptions . __setupedGlobalTypes = setupGlobalTypes ( rootDir , resolvedVueOptions , parseConfigHost ) ;
43+ }
3444 const parsed = ts . parseJsonConfigFileContent (
3545 json ,
3646 proxyHost . host ,
@@ -60,7 +70,8 @@ export function createParsedCommandLineByJson(
6070export function createParsedCommandLine (
6171 ts : typeof import ( 'typescript' ) ,
6272 parseConfigHost : ts . ParseConfigHost ,
63- tsConfigPath : string
73+ tsConfigPath : string ,
74+ skipGlobalTypesSetup = false
6475) : ParsedCommandLine {
6576 try {
6677 const proxyHost = proxyParseConfigHostForExtendConfigPaths ( parseConfigHost ) ;
@@ -79,6 +90,12 @@ export function createParsedCommandLine(
7990 }
8091
8192 const resolvedVueOptions = resolveVueCompilerOptions ( vueOptions ) ;
93+ if ( skipGlobalTypesSetup ) {
94+ resolvedVueOptions . __setupedGlobalTypes = true ;
95+ }
96+ else {
97+ resolvedVueOptions . __setupedGlobalTypes = setupGlobalTypes ( path . dirname ( tsConfigPath ) , resolvedVueOptions , parseConfigHost ) ;
98+ }
8299 const parsed = ts . parseJsonSourceFileConfigFileContent (
83100 config ,
84101 proxyHost . host ,
@@ -260,3 +277,28 @@ export function resolveVueCompilerOptions(vueOptions: Partial<VueCompilerOptions
260277 ) . map ( ( [ k , v ] ) => [ camelize ( k ) , v ] ) ) ,
261278 } ;
262279}
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