|
| 1 | +import { readFileSync } from "node:fs"; |
| 2 | +import { createRequire } from "node:module"; |
| 3 | +import { dirname, join } from "node:path"; |
| 4 | + |
| 5 | +const require = createRequire(import.meta.url); |
| 6 | + |
| 7 | +/** |
| 8 | + * Resolve a dependency's installed version. |
| 9 | + * |
| 10 | + * vite-plugin-federation resolves this itself, but only via |
| 11 | + * `resolve("<pkg>/package.json")` — which fails for any package that doesn't |
| 12 | + * list "./package.json" in its exports map (@vueuse/core, clsx, tailwind-merge |
| 13 | + * and class-variance-authority all don't), erroring the dev server with "No |
| 14 | + * description file or no version in description file". Supplying `version` up |
| 15 | + * front makes it skip that lookup entirely. So resolve the entry point and walk |
| 16 | + * up to the manifest instead, which no exports map can block. |
| 17 | + */ |
| 18 | +function versionOf(pkg: string): string { |
| 19 | + let dir = dirname(require.resolve(pkg)); |
| 20 | + for (;;) { |
| 21 | + try { |
| 22 | + const json = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")); |
| 23 | + if (json.name === pkg && json.version) { |
| 24 | + return json.version; |
| 25 | + } |
| 26 | + } catch { |
| 27 | + // keep walking — not every ancestor directory has a manifest |
| 28 | + } |
| 29 | + const parent = dirname(dir); |
| 30 | + if (parent === dir) { |
| 31 | + throw new Error(`federation: could not resolve a version for "${pkg}"`); |
| 32 | + } |
| 33 | + dir = parent; |
| 34 | + } |
| 35 | +} |
| 36 | + |
1 | 37 | /** |
2 | 38 | * Module Federation shared singletons. The host (this web app) and every |
3 | 39 | * plugin remote MUST declare the same set so exactly one instance of each is |
|
10 | 46 | * (e.g. `@5stack/ui`) fails the build. Remotes still share those among |
11 | 47 | * themselves; the first remote to load one provides the singleton. |
12 | 48 | */ |
13 | | -export const FEDERATION_SHARED = { |
14 | | - vue: { singleton: true, requiredVersion: false }, |
15 | | - "vue-router": { singleton: true, requiredVersion: false }, |
16 | | - pinia: { singleton: true, requiredVersion: false }, |
17 | | - "reka-ui": { singleton: true, requiredVersion: false }, |
18 | | - "@vueuse/core": { singleton: true, requiredVersion: false }, |
19 | | - "lucide-vue-next": { singleton: true, requiredVersion: false }, |
20 | | - "class-variance-authority": { singleton: true, requiredVersion: false }, |
21 | | - "tailwind-merge": { singleton: true, requiredVersion: false }, |
22 | | - clsx: { singleton: true, requiredVersion: false }, |
23 | | -} as const; |
| 49 | +export const FEDERATION_SHARED = Object.fromEntries( |
| 50 | + [ |
| 51 | + "vue", |
| 52 | + "vue-router", |
| 53 | + "pinia", |
| 54 | + "reka-ui", |
| 55 | + "@vueuse/core", |
| 56 | + "lucide-vue-next", |
| 57 | + "class-variance-authority", |
| 58 | + "tailwind-merge", |
| 59 | + "clsx", |
| 60 | + ].map((pkg) => [ |
| 61 | + pkg, |
| 62 | + { singleton: true, requiredVersion: false, version: versionOf(pkg) }, |
| 63 | + ]), |
| 64 | +); |
0 commit comments