MonorePo中的VUE2和VUE3的构建:Vue-Template-Compiler的版本不匹配错误

发布于 2025-01-31 07:48:54 字数 10098 浏览 0 评论 0 原文

假设我使用pnpm在monorepo中有两个软件包。一个用于vue@3( package-a ),另一个用于vue@2( package-b )。对于记录,它们都取决于第三个软件包,称为 core

您可以在下面找到它的样子:

.
├── .npmrc
├── .nvmrc
├── package.json
├── packages
│   ├── package-a
│   │   ├── index.ts
│   │   ├── node_modules
│   │   │   ├── .bin
│   │   │   │   ├── tsc
│   │   │   │   ├── tsserver
│   │   │   │   ├── vite
│   │   │   │   └── vue-tsc
│   │   │   ├── @myscope
│   │   │   │   └── core -> ../../../core
│   │   │   ├── @vitejs
│   │   │   │   └── plugin-vue -> ../../../../node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@vitejs/plugin-vue
│   │   │   ├── typescript -> ../../../node_modules/.pnpm/[email protected]/node_modules/typescript
│   │   │   ├── vite -> ../../../node_modules/.pnpm/[email protected]/node_modules/vite
│   │   │   ├── vue -> ../../../node_modules/.pnpm/[email protected]/node_modules/vue
│   │   │   └── vue-tsc -> ../../../node_modules/.pnpm/[email protected][email protected]/node_modules/vue-tsc
│   │   ├── package.json
│   │   ├── tsconfig.json
│   │   ├── tsconfig.node.json
│   │   └── vite.config.ts
│   ├── package-b
│   │   ├── index.ts
│   │   ├── node_modules
│   │   │   ├── .bin
│   │   │   │   ├── tsc
│   │   │   │   ├── tsserver
│   │   │   │   ├── vite
│   │   │   │   └── vue-tsc
│   │   │   ├── @myscope
│   │   │   │   └── core -> ../../../core
│   │   │   ├── typescript -> ../../../node_modules/.pnpm/[email protected]/node_modules/typescript
│   │   │   ├── vite -> ../../../node_modules/.pnpm/[email protected]/node_modules/vite
│   │   │   ├── vite-plugin-vue2 -> ../../../node_modules/.pnpm/[email protected]_dj3dtukdyynhbiqf2xbv2ocyei/node_modules/vite-plugin-vue2
│   │   │   ├── vue -> ../../../node_modules/.pnpm/[email protected]/node_modules/vue
│   │   │   ├── vue-template-compiler -> ../../../node_modules/.pnpm/[email protected]/node_modules/vue-template-compiler
│   │   │   └── vue-tsc -> ../../../node_modules/.pnpm/[email protected][email protected]/node_modules/vue-tsc
│   │   ├── package.json
│   │   ├── tsconfig.json
│   │   ├── tsconfig.node.json
│   │   └── vite.config.ts
│   └── core
│       ├── index.ts
│       └── package.json
├── pnpm-lock.yaml
└── pnpm-workspace.yaml

packages/package-a/povpage.json 中:

{
  "name": "@myscope/package-a",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "vue-tsc --noEmit && vite build"
  },
  "dependencies": {
    "@myscope/core": "workspace:*",
    "vue": "^3.2.25"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^2.3.3",
    "typescript": "^4.5.4",
    "vite": "^2.9.9",
    "vue-tsc": "^0.34.7"
  }
}

in packages/package-b/poffcate.json

{
  "name": "@myscope/package-b",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "vue-tsc --noEmit && vite build"
  },
  "dependencies": {
    "@myscope/core": "workspace:*",
    "vue": "^2.6.14"
  },
  "devDependencies": {
    "typescript": "^4.5.4",
    "vite": "^2.9.9",
    "vite-plugin-vue2": "^2.0.0",
    "vue-template-compiler": "^2.6.14",
    "vue-tsc": "^0.34.7"
  }
}

当我运行时pnpm在 package-b (vue@2)中,pnpm run build 我得到了版本不匹配错误,而应该不应该:

> vue-tsc --noEmit && vite build

failed to load config from /path/to/my/project/monorepo/packages/package-b/vite.config.ts
error during build:
Error: 

Vue packages version mismatch:

- [email protected] (/path/to/my/project/monorepo/node_modules/.pnpm/[email protected]/node_modules/vue/index.js)
- [email protected] (/path/to/my/project/monorepo/node_modules/.pnpm/[email protected]/node_modules/vue-template-compiler/package.json)

This may cause things to work incorrectly. Make sure to use the same version for both.
If you are using vue-loader@>=10.0, simply update vue-template-compiler.
If you are using vue-loader@<10.0 or vueify, re-installing vue-loader/vueify should bump vue-template-compiler to the latest.

    at Object.<anonymous> (/path/to/my/project/monorepo/node_modules/.pnpm/[email protected]/node_modules/vue-template-compiler/index.js:10:9)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/path/to/my/project/monorepo/node_modules/.pnpm/[email protected]_dj3dtukdyynhbiqf2xbv2ocyei/node_modules/vite-plugin-vue2/dist/utils/descriptorCache.js:34:42)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/path/to/my/project/monorepo/node_modules/.pnpm/[email protected]_dj3dtukdyynhbiqf2xbv2ocyei/node_modules/vite-plugin-vue2/dist/main.js:35:27)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
 ELIFECYCLE  Command failed with exit code 1.
 ELIFECYCLE  Command failed with exit code 1.

似乎已解决的版本未遵循 package.json < /code>并使用 vue@3 而不是 vue@2 。我不明白为什么。这可能是关于 vue-template-compiler 解决vue之类的错误吗?

我发现可以使它工作的“肮脏”解决方法是添加 .npmrc 在项目的根部,但我不知道通过这样做可能会得到什么副作用。无论如何,是否有一种更干净的方法可以解决此问题?

PS。这可以与 https://github.com/vuejs/vuejs/vuejs/vue/vue/issues/11828 但是我在这里问,请在封闭的问题中回复。

Let's say I have two packages in a monorepo using pnpm. One for Vue@3 (package-a), one other for Vue@2 (package-b). For the record, they both depend on a third package called core.

You can find what it looks like below:

.
├── .npmrc
├── .nvmrc
├── package.json
├── packages
│   ├── package-a
│   │   ├── index.ts
│   │   ├── node_modules
│   │   │   ├── .bin
│   │   │   │   ├── tsc
│   │   │   │   ├── tsserver
│   │   │   │   ├── vite
│   │   │   │   └── vue-tsc
│   │   │   ├── @myscope
│   │   │   │   └── core -> ../../../core
│   │   │   ├── @vitejs
│   │   │   │   └── plugin-vue -> ../../../../node_modules/.pnpm/@[email protected][email protected][email protected]/node_modules/@vitejs/plugin-vue
│   │   │   ├── typescript -> ../../../node_modules/.pnpm/[email protected]/node_modules/typescript
│   │   │   ├── vite -> ../../../node_modules/.pnpm/[email protected]/node_modules/vite
│   │   │   ├── vue -> ../../../node_modules/.pnpm/[email protected]/node_modules/vue
│   │   │   └── vue-tsc -> ../../../node_modules/.pnpm/[email protected][email protected]/node_modules/vue-tsc
│   │   ├── package.json
│   │   ├── tsconfig.json
│   │   ├── tsconfig.node.json
│   │   └── vite.config.ts
│   ├── package-b
│   │   ├── index.ts
│   │   ├── node_modules
│   │   │   ├── .bin
│   │   │   │   ├── tsc
│   │   │   │   ├── tsserver
│   │   │   │   ├── vite
│   │   │   │   └── vue-tsc
│   │   │   ├── @myscope
│   │   │   │   └── core -> ../../../core
│   │   │   ├── typescript -> ../../../node_modules/.pnpm/[email protected]/node_modules/typescript
│   │   │   ├── vite -> ../../../node_modules/.pnpm/[email protected]/node_modules/vite
│   │   │   ├── vite-plugin-vue2 -> ../../../node_modules/.pnpm/[email protected]_dj3dtukdyynhbiqf2xbv2ocyei/node_modules/vite-plugin-vue2
│   │   │   ├── vue -> ../../../node_modules/.pnpm/[email protected]/node_modules/vue
│   │   │   ├── vue-template-compiler -> ../../../node_modules/.pnpm/[email protected]/node_modules/vue-template-compiler
│   │   │   └── vue-tsc -> ../../../node_modules/.pnpm/[email protected][email protected]/node_modules/vue-tsc
│   │   ├── package.json
│   │   ├── tsconfig.json
│   │   ├── tsconfig.node.json
│   │   └── vite.config.ts
│   └── core
│       ├── index.ts
│       └── package.json
├── pnpm-lock.yaml
└── pnpm-workspace.yaml

In packages/package-a/package.json:

{
  "name": "@myscope/package-a",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "vue-tsc --noEmit && vite build"
  },
  "dependencies": {
    "@myscope/core": "workspace:*",
    "vue": "^3.2.25"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^2.3.3",
    "typescript": "^4.5.4",
    "vite": "^2.9.9",
    "vue-tsc": "^0.34.7"
  }
}

In packages/package-b/package.json

{
  "name": "@myscope/package-b",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "vue-tsc --noEmit && vite build"
  },
  "dependencies": {
    "@myscope/core": "workspace:*",
    "vue": "^2.6.14"
  },
  "devDependencies": {
    "typescript": "^4.5.4",
    "vite": "^2.9.9",
    "vite-plugin-vue2": "^2.0.0",
    "vue-template-compiler": "^2.6.14",
    "vue-tsc": "^0.34.7"
  }
}

When I run pnpm run build in package-b (Vue@2) I get the version mismatch error whereas it shouldnt:

> vue-tsc --noEmit && vite build

failed to load config from /path/to/my/project/monorepo/packages/package-b/vite.config.ts
error during build:
Error: 

Vue packages version mismatch:

- [email protected] (/path/to/my/project/monorepo/node_modules/.pnpm/[email protected]/node_modules/vue/index.js)
- [email protected] (/path/to/my/project/monorepo/node_modules/.pnpm/[email protected]/node_modules/vue-template-compiler/package.json)

This may cause things to work incorrectly. Make sure to use the same version for both.
If you are using vue-loader@>=10.0, simply update vue-template-compiler.
If you are using vue-loader@<10.0 or vueify, re-installing vue-loader/vueify should bump vue-template-compiler to the latest.

    at Object.<anonymous> (/path/to/my/project/monorepo/node_modules/.pnpm/[email protected]/node_modules/vue-template-compiler/index.js:10:9)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/path/to/my/project/monorepo/node_modules/.pnpm/[email protected]_dj3dtukdyynhbiqf2xbv2ocyei/node_modules/vite-plugin-vue2/dist/utils/descriptorCache.js:34:42)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/path/to/my/project/monorepo/node_modules/.pnpm/[email protected]_dj3dtukdyynhbiqf2xbv2ocyei/node_modules/vite-plugin-vue2/dist/main.js:35:27)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
 ELIFECYCLE  Command failed with exit code 1.
 ELIFECYCLE  Command failed with exit code 1.

It seems that the resolved version doesn't follows package.json and uses Vue@3 instead of Vue@2. I don't understand why. Could this be a bug about how vue-template-compiler resolves Vue or something?

A "dirty" workaround I've found to get it work is to add hoist=false to .npmrc at the project's root but I don't know what are the possible side-effects I can get by doing this. Anyway, is there a cleaner way to fix this?

PS. This can be related to https://github.com/vuejs/vue/issues/11828 but I'm asking here do do not reply in a closed issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2025-02-07 07:48:55

i got eve)

有两种可能的解决方案对我独立工作:

a)将vue-tsc移至root package.json。这是我尝试将所有项目工具放在各个软件包中所做的。

b)在package.json中使用“ pnpm.packageextentions”。

。 ://github.com/pnpm/pnpm/blob/main/packages/pnpm/changelog.md#700“ rel =“ nofollow noreferrer”>打破变化:

根工作空间项目的依赖项不用于解决其他工作区项目的同伴依赖性#44669>#444699

with pnpm.packageextensions nohoist = false 到 .npmrc

diff --git i/package.json w/package.json
index 95cda08..182e89d 100644
--- i/package.json
+++ w/package.json
@@ -41,5 +41,14 @@
   "packageManager": "[email protected]",
   "engines": {
     "node": ">=16.15.0"
+  },
+  "pnpm": {
+    "packageExtensions": {
+      "vue-template-compiler": {
+        "peerDependencies": {
+          "vue": "^2.6.14"
+        }
+      }
+    }
   }
 }

Answering my own question since I got help on the VueJS discord server.

There are two possible solutions that both worked for me independently:

a) move vue-tsc to the root package.json. This is what I did as I try and keep all my project tooling out of the individual packages.

b) use "pnpm.packageExtentions" in package.json to add vue@2 as a peer dependency of vue-template-compiler

Option a) doesn't seem to work as of pnpm@7 because of a breaking change:

Dependencies of the root workspace project are not used to resolve peer dependencies of other workspace projects #4469

Option B with pnpm.packageExtensions is working fine. No need to add nohoist=false anymore to .npmrc.

diff --git i/package.json w/package.json
index 95cda08..182e89d 100644
--- i/package.json
+++ w/package.json
@@ -41,5 +41,14 @@
   "packageManager": "[email protected]",
   "engines": {
     "node": ">=16.15.0"
+  },
+  "pnpm": {
+    "packageExtensions": {
+      "vue-template-compiler": {
+        "peerDependencies": {
+          "vue": "^2.6.14"
+        }
+      }
+    }
   }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文