@@ -38,7 +38,7 @@ export function createParsedCommandLineByJson(
3838 { } ,
3939 configFileName ,
4040 ) ;
41- const resolver = new CompilerOptionsResolver ( host . fileExists ) ;
41+ const resolver = new CompilerOptionsResolver ( ts , host . readFile ) ;
4242
4343 for ( const extendPath of [ ...extendedPaths ] . reverse ( ) ) {
4444 try {
@@ -85,7 +85,7 @@ export function createParsedCommandLine(
8585 { } ,
8686 configFileName ,
8787 ) ;
88- const resolver = new CompilerOptionsResolver ( host . fileExists ) ;
88+ const resolver = new CompilerOptionsResolver ( ts , host . readFile ) ;
8989
9090 for ( const extendPath of [ ...extendedPaths ] . reverse ( ) ) {
9191 try {
@@ -118,15 +118,16 @@ export class CompilerOptionsResolver {
118118 plugins : VueLanguagePlugin [ ] = [ ] ;
119119
120120 constructor (
121- public fileExists ?: ( path : string ) => boolean ,
121+ public ts : typeof import ( 'typescript' ) ,
122+ public readFile : ( fileName : string ) => string | undefined ,
122123 ) { }
123124
124125 addConfig ( options : RawVueCompilerOptions , rootDir : string ) {
125126 for ( const key in options ) {
126127 switch ( key ) {
127128 case 'target' :
128129 if ( options [ key ] === 'auto' ) {
129- this . target = findVueVersion ( rootDir ) ;
130+ this . target = this . resolveVueVersion ( rootDir ) ;
130131 }
131132 else {
132133 this . target = options [ key ] ;
@@ -154,7 +155,8 @@ export class CompilerOptionsResolver {
154155 this . plugins = ( options . plugins ?? [ ] )
155156 . flatMap < VueLanguagePlugin > ( ( pluginPath : string ) => {
156157 try {
157- const resolvedPath = resolvePath ( pluginPath , rootDir ) ;
158+ const resolve = ( require as NodeJS . Require | undefined ) ?. resolve ;
159+ const resolvedPath = resolve ?.( pluginPath , { paths : [ rootDir ] } ) ;
158160 if ( resolvedPath ) {
159161 const plugin = require ( resolvedPath ) ;
160162 plugin . __moduleName = pluginPath ;
@@ -177,7 +179,7 @@ export class CompilerOptionsResolver {
177179 }
178180 }
179181 if ( options . target === undefined ) {
180- this . target ??= findVueVersion ( rootDir ) ;
182+ this . target ??= this . resolveVueVersion ( rootDir ) ;
181183 }
182184 }
183185
@@ -216,31 +218,24 @@ export class CompilerOptionsResolver {
216218
217219 return resolvedOptions ;
218220 }
219- }
220-
221- function findVueVersion ( rootDir : string ) {
222- const resolvedPath = resolvePath ( 'vue/package.json' , rootDir ) ;
223- if ( resolvedPath ) {
224- const vuePackageJson = require ( resolvedPath ) ;
225- const versionNumbers = vuePackageJson . version . split ( '.' ) ;
226- return Number ( versionNumbers [ 0 ] + '.' + versionNumbers [ 1 ] ) ;
227- }
228- else {
229- // console.warn('Load vue/package.json failed from', folder);
230- }
231- }
232221
233- function resolvePath ( scriptPath : string , root : string ) {
234- try {
235- if ( ( require as NodeJS . Require | undefined ) ?. resolve ) {
236- return require . resolve ( scriptPath , { paths : [ root ] } ) ;
222+ resolveVueVersion ( folder : string ) : number | undefined {
223+ const packageJsonPath = this . ts . findConfigFile (
224+ folder ,
225+ fileName => this . readFile ( fileName ) !== undefined ,
226+ 'node_modules/vue/package.json' ,
227+ ) ;
228+ if ( ! packageJsonPath ) {
229+ return ;
237230 }
238- else {
239- // console.warn('failed to resolve path:', scriptPath, 'require.resolve is not supported in web');
231+ const packageJsonContent = this . readFile ( packageJsonPath ) ;
232+ if ( ! packageJsonContent ) {
233+ return ;
240234 }
241- }
242- catch {
243- // console.warn(error);
235+ const packageJson = JSON . parse ( packageJsonContent ) ;
236+ const version : string = packageJson . version ;
237+ const [ majorVersion , minorVersion ] = version . split ( '.' ) ;
238+ return Number ( majorVersion + '.' + minorVersion ) ;
244239 }
245240}
246241
0 commit comments