Skip to content

Commit ee60f64

Browse files
committed
fix: use strict resolving for vue resolution
1 parent 14fc0ab commit ee60f64

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

src/node/resolver.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'fs'
1+
import fs from 'fs-extra'
22
import path from 'path'
33
import slash from 'slash'
44
import { cleanUrl, resolveFrom, queryRE } from './utils'
@@ -313,13 +313,15 @@ export function resolveNodeModule(
313313
try {
314314
// see if the id is a valid package name
315315
pkgPath = resolveFrom(root, `${id}/package.json`)
316-
} catch (e) {}
316+
} catch (e) {
317+
debug(`failed to resolve package.json for ${id}`)
318+
}
317319

318320
if (pkgPath) {
319321
// if yes, this is a entry import. resolve entry file
320322
let pkg
321323
try {
322-
pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
324+
pkg = fs.readJSONSync(pkgPath)
323325
} catch (e) {
324326
return
325327
}

src/node/utils/resolveVue.ts

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import path from 'path'
2+
import fs from 'fs-extra'
23
import { resolveFrom } from './pathUtils'
34
import sfcCompiler from '@vue/compiler-sfc'
45
import chalk from 'chalk'
56
import { lookupFile } from './fsUtils'
67

78
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
1314
compiler: string
1415
version: string
1516
isLocal: boolean
@@ -24,15 +25,18 @@ export function resolveVue(root: string): ResolvedVuePaths {
2425
if (resolved) {
2526
return resolved
2627
}
27-
let vueVersion: string | undefined
28-
let vuePath: string | undefined
28+
let vueVersion: string
29+
let vueBasePath: string
2930
let compilerPath: string
3031

3132
const projectPkg = JSON.parse(lookupFile(root, ['package.json']) || `{}`)
3233
let isLocal = !!(projectPkg.dependencies && projectPkg.dependencies.vue)
3334
if (isLocal) {
3435
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
3640
} catch (e) {
3741
// user has vue listed but not actually installed.
3842
isLocal = false
@@ -43,18 +47,12 @@ export function resolveVue(root: string): ResolvedVuePaths {
4347
// user has local vue, verify that the same version of @vue/compiler-sfc
4448
// is also installed.
4549
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-
)
5250
const compilerPkgPath = resolveFrom(
5351
root,
5452
'@vue/compiler-sfc/package.json'
5553
)
5654
const compilerPkg = require(compilerPkgPath)
57-
if (compilerPkg.version !== vueVersion) {
55+
if (compilerPkg.version !== vueVersion!) {
5856
throw new Error()
5957
}
6058
compilerPath = path.join(path.dirname(compilerPkgPath), compilerPkg.main)
@@ -72,22 +70,20 @@ export function resolveVue(root: string): ResolvedVuePaths {
7270
} else {
7371
// user has no local vue, use vite's dependency version
7472
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'))
7874
compilerPath = require.resolve('@vue/compiler-sfc')
7975
}
8076

81-
const inferPath = (name: string) =>
82-
vuePath && vuePath.replace(/runtime-dom/g, name)
77+
const resolvePath = (name: string) =>
78+
resolveFrom(vueBasePath, `@vue/${name}/dist/${name}.esm-bundler.js`)
8379

8480
resolved = {
8581
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'),
9187
compiler: compilerPath,
9288
isLocal
9389
}

0 commit comments

Comments
 (0)