Skip to content

Commit 06ea8a7

Browse files
committed
fix: avoid resolving to 2.7 compiler-sfc
This can happen in monorepos where Vue 2.7 is in a package's deps while Vue 3 is only a transitive dep, e.g. from VitePress. Even with pnpm this can happen because `pnpm run` emulates npm behavior by injecting NODE_PATH. ref: vitejs/vite-plugin-vue@cf36b3e
1 parent 2792fca commit 06ea8a7

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/core/compiler.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ declare module 'vue/compiler-sfc' {
1010

1111
export function resolveCompiler(root: string): typeof _compiler {
1212
// resolve from project root first, then fallback to peer dep (if any)
13-
const compiler =
14-
tryRequire('vue/compiler-sfc', root) || tryRequire('vue/compiler-sfc')
13+
const compiler = tryResolveCompiler(root) || tryResolveCompiler()
1514

1615
if (!compiler) {
1716
throw new Error(
@@ -24,6 +23,14 @@ export function resolveCompiler(root: string): typeof _compiler {
2423
return compiler
2524
}
2625

26+
function tryResolveCompiler(root?: string) {
27+
const vueMeta = tryRequire('vue/package.json', root)
28+
// make sure to check the version is 3+ since 2.7 now also has vue/compiler-sfc
29+
if (vueMeta && vueMeta.version.split('.')[0] >= 3) {
30+
return tryRequire('vue/compiler-sfc', root)
31+
}
32+
}
33+
2734
const _require = createRequire(import.meta.url || __filename)
2835
function tryRequire(id: string, from?: string) {
2936
try {

0 commit comments

Comments
 (0)