Skip to content

Commit cf36b3e

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: vuejs/vitepress#1507
1 parent 40cff6b commit cf36b3e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

packages/plugin-vue/src/compiler.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ import type * as _compiler from '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')
15-
13+
const compiler = tryResolveCompiler(root) || tryResolveCompiler()
1614
if (!compiler) {
1715
throw new Error(
1816
`Failed to resolve vue/compiler-sfc.\n` +
@@ -24,6 +22,14 @@ export function resolveCompiler(root: string): typeof _compiler {
2422
return compiler
2523
}
2624

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

0 commit comments

Comments
 (0)