|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Rolldown sometimes copies `__exportAll` into an app chunk and then a sibling |
| 4 | + * chunk imports that copy. Those two files import each other, so when Nitro |
| 5 | + * loads the parent first the helper is still `undefined` — Vercel 500s with |
| 6 | + * `__exportAll is not a function`. |
| 7 | + * |
| 8 | + * Rewrite those imports to the real helper in `_runtime.mjs`, which has no |
| 9 | + * cycle. |
| 10 | + */ |
| 11 | +import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 12 | +import { dirname, join, relative } from "node:path"; |
| 13 | +import { fileURLToPath } from "node:url"; |
| 14 | + |
| 15 | +const ROOT = join(dirname(fileURLToPath(import.meta.url)), ".."); |
| 16 | + |
| 17 | +export function vercelFuncDir(root = ROOT) { |
| 18 | + return join(root, ".vercel/output/functions/__server.func"); |
| 19 | +} |
| 20 | + |
| 21 | +function walkMjs(dir, out = []) { |
| 22 | + if (!existsSync(dir)) return out; |
| 23 | + for (const ent of readdirSync(dir, { withFileTypes: true })) { |
| 24 | + const p = join(dir, ent.name); |
| 25 | + if (ent.isDirectory()) walkMjs(p, out); |
| 26 | + else if (ent.name.endsWith(".mjs")) out.push(p); |
| 27 | + } |
| 28 | + return out; |
| 29 | +} |
| 30 | + |
| 31 | +function findRuntime(funcDir) { |
| 32 | + const p = join(funcDir, "_runtime.mjs"); |
| 33 | + return existsSync(p) ? p : null; |
| 34 | +} |
| 35 | + |
| 36 | +/** Strip `Foo as __exportAll` / `__exportAll` from an `import { … }` spec list. */ |
| 37 | +function stripExportAllSpec(spec) { |
| 38 | + const kept = spec |
| 39 | + .split(",") |
| 40 | + .map((s) => s.trim()) |
| 41 | + .filter(Boolean) |
| 42 | + .filter((s) => s !== "__exportAll" && !/\bas\s+__exportAll$/.test(s)); |
| 43 | + return kept; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Merge `n as __exportAll` into an existing `_runtime.mjs` import, or prepend |
| 48 | + * a new one. Returns the patched source, or null if nothing changed. |
| 49 | + */ |
| 50 | +export function rewriteExportAllImports(code, runtimeSpecifier) { |
| 51 | + if (!code.includes("__exportAll(")) return null; |
| 52 | + if (/var __exportAll\s*=/.test(code) || /function __exportAll\s*\(/.test(code)) return null; |
| 53 | + |
| 54 | + let stripped = false; |
| 55 | + let next = code.replace( |
| 56 | + /import\s*\{([^}]*)\}\s*from\s*(["'])([^"']+)\2;?/g, |
| 57 | + (full, spec, quote, from) => { |
| 58 | + if (!spec.includes("__exportAll")) return full; |
| 59 | + if (from.includes("_runtime")) return full; |
| 60 | + const kept = stripExportAllSpec(spec); |
| 61 | + stripped = true; |
| 62 | + if (kept.length === 0) return ""; |
| 63 | + return `import { ${kept.join(", ")} } from ${quote}${from}${quote};`; |
| 64 | + }, |
| 65 | + ); |
| 66 | + if (!stripped) return null; |
| 67 | + |
| 68 | + if ( |
| 69 | + /from\s*(["'])[^"']*_runtime[^"']*\1/.test(next) && |
| 70 | + /as\s+__exportAll/.test( |
| 71 | + next.match(/import\s*\{([^}]*)\}\s*from\s*["'][^"']*_runtime[^"']*["']/)?.[1] ?? "", |
| 72 | + ) |
| 73 | + ) { |
| 74 | + return next; |
| 75 | + } |
| 76 | + |
| 77 | + const runtimeImport = `import { n as __exportAll } from ${JSON.stringify(runtimeSpecifier)};`; |
| 78 | + const runtimeRe = /import\s*\{([^}]*)\}\s*from\s*(["'])([^"']*_runtime[^"']*)\2;?/; |
| 79 | + if (runtimeRe.test(next)) { |
| 80 | + next = next.replace(runtimeRe, (full, spec, quote, from) => { |
| 81 | + if (spec.includes("__exportAll")) return full; |
| 82 | + const specs = spec |
| 83 | + .split(",") |
| 84 | + .map((s) => s.trim()) |
| 85 | + .filter(Boolean); |
| 86 | + specs.push("n as __exportAll"); |
| 87 | + return `import { ${specs.join(", ")} } from ${quote}${from}${quote};`; |
| 88 | + }); |
| 89 | + return next; |
| 90 | + } |
| 91 | + |
| 92 | + const firstImport = next.search(/^import\s/m); |
| 93 | + if (firstImport === -1) return `${runtimeImport}\n${next}`; |
| 94 | + return `${next.slice(0, firstImport)}${runtimeImport}\n${next.slice(firstImport)}`; |
| 95 | +} |
| 96 | + |
| 97 | +export function patchFuncDir(funcDir) { |
| 98 | + const runtime = findRuntime(funcDir); |
| 99 | + if (!runtime) return []; |
| 100 | + const patched = []; |
| 101 | + for (const file of walkMjs(funcDir)) { |
| 102 | + const before = readFileSync(file, "utf8"); |
| 103 | + const rel = relative(dirname(file), runtime).replaceAll("\\", "/"); |
| 104 | + const specifier = rel.startsWith(".") ? rel : `./${rel}`; |
| 105 | + const after = rewriteExportAllImports(before, specifier); |
| 106 | + if (after && after !== before) { |
| 107 | + writeFileSync(file, after); |
| 108 | + patched.push(relative(funcDir, file)); |
| 109 | + } |
| 110 | + } |
| 111 | + return patched; |
| 112 | +} |
| 113 | + |
| 114 | +export function patchWorkspace(root = ROOT) { |
| 115 | + return patchFuncDir(vercelFuncDir(root)); |
| 116 | +} |
| 117 | + |
| 118 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 119 | + const patched = patchWorkspace(); |
| 120 | + if (patched.length === 0) { |
| 121 | + console.log("[fix-ssr-exportall] nothing to patch"); |
| 122 | + } else { |
| 123 | + for (const f of patched) console.log(`[fix-ssr-exportall] ${f}`); |
| 124 | + } |
| 125 | +} |
0 commit comments