1
1
import path from 'path'
2
+ import fs from 'fs-extra'
2
3
import { resolveFrom } from './pathUtils'
3
4
import sfcCompiler from '@vue/compiler-sfc'
4
5
import chalk from 'chalk'
5
6
import { lookupFile } from './fsUtils'
6
7
7
8
interface ResolvedVuePaths {
8
- vue : string | undefined
9
- '@vue/runtime-dom' : string | undefined
10
- '@vue/runtime-core' : string | undefined
11
- '@vue/reactivity' : string | undefined
12
- '@vue/shared' : string | undefined
9
+ vue : string
10
+ '@vue/runtime-dom' : string
11
+ '@vue/runtime-core' : string
12
+ '@vue/reactivity' : string
13
+ '@vue/shared' : string
13
14
compiler : string
14
15
version : string
15
16
isLocal : boolean
@@ -24,15 +25,18 @@ export function resolveVue(root: string): ResolvedVuePaths {
24
25
if ( resolved ) {
25
26
return resolved
26
27
}
27
- let vueVersion : string | undefined
28
- let vuePath : string | undefined
28
+ let vueVersion : string
29
+ let vueBasePath : string
29
30
let compilerPath : string
30
31
31
32
const projectPkg = JSON . parse ( lookupFile ( root , [ 'package.json' ] ) || `{}` )
32
33
let isLocal = ! ! ( projectPkg . dependencies && projectPkg . dependencies . vue )
33
34
if ( isLocal ) {
34
35
try {
35
- resolveFrom ( root , 'vue' )
36
+ const userVuePkg = resolveFrom ( root , 'vue/package.json' )
37
+ vueBasePath = path . dirname ( userVuePkg )
38
+ vueVersion = fs . readJSONSync ( userVuePkg ) . version
39
+ isLocal = true
36
40
} catch ( e ) {
37
41
// user has vue listed but not actually installed.
38
42
isLocal = false
@@ -43,18 +47,12 @@ export function resolveVue(root: string): ResolvedVuePaths {
43
47
// user has local vue, verify that the same version of @vue/compiler-sfc
44
48
// is also installed.
45
49
try {
46
- const userVuePkg = resolveFrom ( root , 'vue/package.json' )
47
- vueVersion = require ( userVuePkg ) . version
48
- vuePath = resolveFrom (
49
- root ,
50
- '@vue/runtime-dom/dist/runtime-dom.esm-bundler.js'
51
- )
52
50
const compilerPkgPath = resolveFrom (
53
51
root ,
54
52
'@vue/compiler-sfc/package.json'
55
53
)
56
54
const compilerPkg = require ( compilerPkgPath )
57
- if ( compilerPkg . version !== vueVersion ) {
55
+ if ( compilerPkg . version !== vueVersion ! ) {
58
56
throw new Error ( )
59
57
}
60
58
compilerPath = path . join ( path . dirname ( compilerPkgPath ) , compilerPkg . main )
@@ -72,22 +70,20 @@ export function resolveVue(root: string): ResolvedVuePaths {
72
70
} else {
73
71
// user has no local vue, use vite's dependency version
74
72
vueVersion = require ( 'vue/package.json' ) . version
75
- vuePath = require . resolve (
76
- '@vue/runtime-dom/dist/runtime-dom.esm-bundler.js'
77
- )
73
+ vueBasePath = path . dirname ( require . resolve ( 'vue/package.json' ) )
78
74
compilerPath = require . resolve ( '@vue/compiler-sfc' )
79
75
}
80
76
81
- const inferPath = ( name : string ) =>
82
- vuePath && vuePath . replace ( / r u n t i m e - d o m / g , name )
77
+ const resolvePath = ( name : string ) =>
78
+ resolveFrom ( vueBasePath , `@vue/ ${ name } /dist/ ${ name } .esm-bundler.js` )
83
79
84
80
resolved = {
85
81
version : vueVersion ! ,
86
- vue : vuePath ,
87
- '@vue/runtime-dom' : vuePath ,
88
- '@vue/runtime-core' : inferPath ( 'runtime-core' ) ,
89
- '@vue/reactivity' : inferPath ( 'reactivity' ) ,
90
- '@vue/shared' : inferPath ( 'shared' ) ,
82
+ vue : resolvePath ( 'runtime-dom' ) ,
83
+ '@vue/runtime-dom' : resolvePath ( 'runtime-dom' ) ,
84
+ '@vue/runtime-core' : resolvePath ( 'runtime-core' ) ,
85
+ '@vue/reactivity' : resolvePath ( 'reactivity' ) ,
86
+ '@vue/shared' : resolvePath ( 'shared' ) ,
91
87
compiler : compilerPath ,
92
88
isLocal
93
89
}
0 commit comments