@@ -2,31 +2,41 @@ import { camelize, NOOP as noop } from '@vue/shared';
2
2
import { posix as path } from 'path-browserify' ;
3
3
import type * as ts from 'typescript' ;
4
4
import { generateGlobalTypes , getGlobalTypesFileName } from '../codegen/globalTypes' ;
5
- import { getAllExtensions } from '../languagePlugin' ;
6
5
import type { RawVueCompilerOptions , VueCompilerOptions , VueLanguagePlugin } from '../types' ;
7
6
import { hyphenateTag } from './shared' ;
8
7
9
- export type ParsedCommandLine = ts . ParsedCommandLine & {
8
+ interface ParseConfigHost extends Omit < ts . ParseConfigHost , 'readDirectory' > { }
9
+
10
+ export interface ParsedCommandLine extends Omit < ts . ParsedCommandLine , 'fileNames' > {
10
11
vueOptions : VueCompilerOptions ;
11
- } ;
12
+ }
12
13
13
14
export function createParsedCommandLineByJson (
14
15
ts : typeof import ( 'typescript' ) ,
15
- parseConfigHost : ts . ParseConfigHost & {
16
- writeFile ?( path : string , data : string ) : void ;
17
- } ,
16
+ host : ParseConfigHost ,
18
17
rootDir : string ,
19
18
json : any ,
20
19
configFileName ?: string ,
21
20
) : ParsedCommandLine {
22
- const proxyHost = proxyParseConfigHostForExtendConfigPaths ( parseConfigHost ) ;
23
- ts . parseJsonConfigFileContent ( json , proxyHost . host , rootDir , { } , configFileName ) ;
24
-
25
- const resolver = new CompilerOptionsResolver ( parseConfigHost . fileExists ) ;
21
+ const extendedPaths = new Set < string > ( ) ;
22
+ const proxyHost = {
23
+ ...host ,
24
+ readFile ( fileName : string ) {
25
+ if ( ! fileName . endsWith ( '/package.json' ) ) {
26
+ extendedPaths . add ( fileName ) ;
27
+ }
28
+ return host . readFile ( fileName ) ;
29
+ } ,
30
+ readDirectory ( ) {
31
+ return [ ] ;
32
+ } ,
33
+ } ;
34
+ const parsed = ts . parseJsonConfigFileContent ( json , proxyHost , rootDir , { } , configFileName ) ;
35
+ const resolver = new CompilerOptionsResolver ( host . fileExists ) ;
26
36
27
- for ( const extendPath of proxyHost . extendConfigPaths . reverse ( ) ) {
37
+ for ( const extendPath of [ ... extendedPaths ] . reverse ( ) ) {
28
38
try {
29
- const configFile = ts . readJsonConfigFile ( extendPath , proxyHost . host . readFile ) ;
39
+ const configFile = ts . readJsonConfigFile ( extendPath , host . readFile ) ;
30
40
const obj = ts . convertToObject ( configFile , [ ] ) ;
31
41
const rawOptions : RawVueCompilerOptions = obj ?. vueCompilerOptions ?? { } ;
32
42
resolver . addConfig ( rawOptions , path . dirname ( configFile . fileName ) ) ;
@@ -37,111 +47,62 @@ export function createParsedCommandLineByJson(
37
47
// ensure the rootDir is added to the config roots
38
48
resolver . addConfig ( { } , rootDir ) ;
39
49
40
- const resolvedVueOptions = resolver . build ( ) ;
41
- const parsed = ts . parseJsonConfigFileContent (
42
- json ,
43
- proxyHost . host ,
44
- rootDir ,
45
- { } ,
46
- configFileName ,
47
- undefined ,
48
- getAllExtensions ( resolvedVueOptions )
49
- . map ( extension => ( {
50
- extension : extension . slice ( 1 ) ,
51
- isMixedContent : true ,
52
- scriptKind : ts . ScriptKind . Deferred ,
53
- } ) ) ,
54
- ) ;
55
-
56
- // fix https://github.com/vuejs/language-tools/issues/1786
57
- // https://github.com/microsoft/TypeScript/issues/30457
58
- // patching ts server broke with outDir + rootDir + composite/incremental
59
- parsed . options . outDir = undefined ;
60
-
61
50
return {
62
51
...parsed ,
63
- vueOptions : resolvedVueOptions ,
52
+ vueOptions : resolver . build ( ) ,
64
53
} ;
65
54
}
66
55
67
56
export function createParsedCommandLine (
68
57
ts : typeof import ( 'typescript' ) ,
69
- parseConfigHost : ts . ParseConfigHost ,
70
- tsConfigPath : string ,
58
+ host : ParseConfigHost ,
59
+ configFileName : string ,
71
60
) : ParsedCommandLine {
72
61
try {
73
- const rootDir = path . dirname ( tsConfigPath ) ;
74
- const proxyHost = proxyParseConfigHostForExtendConfigPaths ( parseConfigHost ) ;
75
- const config = ts . readJsonConfigFile ( tsConfigPath , proxyHost . host . readFile ) ;
76
- ts . parseJsonSourceFileConfigFileContent ( config , proxyHost . host , rootDir , { } , tsConfigPath ) ;
77
-
78
- const resolver = new CompilerOptionsResolver ( parseConfigHost . fileExists ) ;
62
+ const extendedPaths = new Set < string > ( ) ;
63
+ const proxyHost = {
64
+ ...host ,
65
+ readFile ( fileName : string ) {
66
+ if ( ! fileName . endsWith ( '/package.json' ) ) {
67
+ extendedPaths . add ( fileName ) ;
68
+ }
69
+ return host . readFile ( fileName ) ;
70
+ } ,
71
+ readDirectory ( ) {
72
+ return [ ] ;
73
+ } ,
74
+ } ;
75
+ const config = ts . readJsonConfigFile ( configFileName , proxyHost . readFile ) ;
76
+ const parsed = ts . parseJsonSourceFileConfigFileContent (
77
+ config ,
78
+ proxyHost ,
79
+ path . dirname ( configFileName ) ,
80
+ { } ,
81
+ configFileName ,
82
+ ) ;
83
+ const resolver = new CompilerOptionsResolver ( host . fileExists ) ;
79
84
80
- for ( const extendPath of proxyHost . extendConfigPaths . reverse ( ) ) {
85
+ for ( const extendPath of [ ... extendedPaths ] . reverse ( ) ) {
81
86
try {
82
- const configFile = ts . readJsonConfigFile ( extendPath , proxyHost . host . readFile ) ;
87
+ const configFile = ts . readJsonConfigFile ( extendPath , host . readFile ) ;
83
88
const obj = ts . convertToObject ( configFile , [ ] ) ;
84
89
const rawOptions : RawVueCompilerOptions = obj ?. vueCompilerOptions ?? { } ;
85
90
resolver . addConfig ( rawOptions , path . dirname ( configFile . fileName ) ) ;
86
91
}
87
92
catch { }
88
93
}
89
94
90
- const resolvedVueOptions = resolver . build ( ) ;
91
- const parsed = ts . parseJsonSourceFileConfigFileContent (
92
- config ,
93
- proxyHost . host ,
94
- path . dirname ( tsConfigPath ) ,
95
- { } ,
96
- tsConfigPath ,
97
- undefined ,
98
- getAllExtensions ( resolvedVueOptions )
99
- . map ( extension => ( {
100
- extension : extension . slice ( 1 ) ,
101
- isMixedContent : true ,
102
- scriptKind : ts . ScriptKind . Deferred ,
103
- } ) ) ,
104
- ) ;
105
-
106
- // fix https://github.com/vuejs/language-tools/issues/1786
107
- // https://github.com/microsoft/TypeScript/issues/30457
108
- // patching ts server broke with outDir + rootDir + composite/incremental
109
- parsed . options . outDir = undefined ;
110
-
111
95
return {
112
96
...parsed ,
113
- vueOptions : resolvedVueOptions ,
114
- } ;
115
- }
116
- catch {
117
- // console.warn('Failed to resolve tsconfig path:', tsConfigPath, err);
118
- return {
119
- fileNames : [ ] ,
120
- options : { } ,
121
- vueOptions : getDefaultCompilerOptions ( ) ,
122
- errors : [ ] ,
97
+ vueOptions : resolver . build ( ) ,
123
98
} ;
124
99
}
125
- }
100
+ catch { }
126
101
127
- function proxyParseConfigHostForExtendConfigPaths ( parseConfigHost : ts . ParseConfigHost ) {
128
- const extendConfigPaths : string [ ] = [ ] ;
129
- const host = new Proxy ( parseConfigHost , {
130
- get ( target , key ) {
131
- if ( key === 'readFile' ) {
132
- return ( fileName : string ) => {
133
- if ( ! fileName . endsWith ( '/package.json' ) && ! extendConfigPaths . includes ( fileName ) ) {
134
- extendConfigPaths . push ( fileName ) ;
135
- }
136
- return target . readFile ( fileName ) ;
137
- } ;
138
- }
139
- return target [ key as keyof typeof target ] ;
140
- } ,
141
- } ) ;
142
102
return {
143
- host,
144
- extendConfigPaths,
103
+ options : { } ,
104
+ errors : [ ] ,
105
+ vueOptions : getDefaultCompilerOptions ( ) ,
145
106
} ;
146
107
}
147
108
0 commit comments