mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(site): build the Worker as one file so its chunks can't import each other (#63031)
This commit is contained in:
@@ -1,7 +1,14 @@
|
||||
// Fails the build when three.js (or any WebGL module) reaches the Worker's server bundle. Its module
|
||||
// start-up creates an AbortController, which Cloudflare Workers forbid during global start-up, so
|
||||
// every page would answer 500. Run after `vite build`; browser-only modules must be loaded with
|
||||
// `import.meta.env.SSR ? undefined : () => import(...)` (see test/server-bundle.test.ts).
|
||||
// Fails the build when the Worker's server bundle can't start:
|
||||
//
|
||||
// - three.js (or any WebGL module) in it. Its module start-up creates an AbortController, which
|
||||
// Cloudflare Workers forbid during global start-up, so every page would answer 500. Browser-only
|
||||
// modules must be loaded with `import.meta.env.SSR ? undefined : () => import(...)` (see
|
||||
// test/server-bundle.test.ts).
|
||||
// - Files that import each other. On 5 Nov the server chunk split into _ssr/ssr.mjs and ssr2.mjs, each
|
||||
// importing the other, and workerd refused to load it ("Export 'ssr_exports' is not defined").
|
||||
// vite.config.ts now builds the Worker as one file; this catches a cycle if splitting comes back.
|
||||
//
|
||||
// Run after `vite build`.
|
||||
import path from "node:path"
|
||||
import { Glob } from "bun"
|
||||
|
||||
@@ -15,16 +22,40 @@ if (!files.length) {
|
||||
console.error(`check-server-bundle: no server output in ${root}; run vite build first`)
|
||||
process.exit(1)
|
||||
}
|
||||
const found = (
|
||||
await Promise.all(
|
||||
files.map(async (file) => {
|
||||
const text = await Bun.file(path.join(root, file)).text()
|
||||
return markers.filter((marker) => text.includes(marker)).map((marker) => `${file}: ${marker}`)
|
||||
}),
|
||||
)
|
||||
).flat()
|
||||
const texts = new Map(
|
||||
await Promise.all(files.map(async (file) => [file, await Bun.file(path.join(root, file)).text()] as const)),
|
||||
)
|
||||
const found = files.flatMap((file) =>
|
||||
markers.filter((marker) => texts.get(file)!.includes(marker)).map((marker) => `${file}: ${marker}`),
|
||||
)
|
||||
if (found.length) {
|
||||
console.error("check-server-bundle: three.js/WebGL code is in the server bundle:\n " + found.join("\n "))
|
||||
process.exit(1)
|
||||
}
|
||||
console.log(`check-server-bundle: ${files.length} server files, no three.js/WebGL code`)
|
||||
|
||||
// Each file's static imports of the bundle's other files (relative specifiers; dynamic import() is fine).
|
||||
const imports = new Map(
|
||||
files.map((file) => [
|
||||
file,
|
||||
[...texts.get(file)!.matchAll(/^(?:import|export)\b[^;"']*?(?:from\s*)?["'](\.{1,2}\/[^"']+)["']/gm)].map(
|
||||
(match) => path.posix.join(path.posix.dirname(file), match[1]),
|
||||
),
|
||||
]),
|
||||
)
|
||||
const cycle = files.map((file) => cycleFrom(file, [file])).find(Boolean)
|
||||
if (cycle) {
|
||||
console.error(`check-server-bundle: server files import each other, which workerd can't load:\n ${cycle.join(" → ")}`)
|
||||
process.exit(1)
|
||||
}
|
||||
console.log(`check-server-bundle: ${files.length} server files, no import cycles, no three.js/WebGL code`)
|
||||
|
||||
/** A path of static imports from `trail`'s last file back to its first, if there is one. */
|
||||
function cycleFrom(start: string, trail: string[], seen = new Set<string>()): string[] | undefined {
|
||||
for (const next of imports.get(trail.at(-1)!) ?? []) {
|
||||
if (next === start) return [...trail, next]
|
||||
if (seen.has(next) || !imports.has(next)) continue
|
||||
seen.add(next)
|
||||
const found = cycleFrom(start, [...trail, next], seen)
|
||||
if (found) return found
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +53,12 @@ export default defineConfig({
|
||||
cloudflare: {
|
||||
nodeCompat: true,
|
||||
},
|
||||
// The Worker's code in one file. Nitro bundles the server build's chunks again, and Rolldown
|
||||
// could split code the pages share with the server entry into a chunk that imports the entry
|
||||
// back, which workerd refuses to load (5 Nov: _ssr/ssr.mjs and ssr2.mjs, "Export 'ssr_exports'
|
||||
// is not defined"). Every module ships in the one upload anyway, so splitting saves nothing.
|
||||
// Here, not as Nitro's top-level inlineDynamicImports: the preset's rollupConfig overrides that.
|
||||
rollupConfig: { output: { inlineDynamicImports: true } },
|
||||
}),
|
||||
notFoundInDev(),
|
||||
devOnlyCss(),
|
||||
|
||||
Reference in New Issue
Block a user