| 1 | # drizzle-schema-chunks |
| 2 | |
| 3 | Fixture for [#749](https://github.com/alchemy-run/alchemy/issues/749). |
| 4 | |
| 5 | Cloudflare Worker `ScriptStartupError` after Alchemy/Rolldown bundling when |
| 6 | top-level Drizzle schema modules are code-split into a chunk separate from |
| 7 | `drizzle-orm`. Cross-chunk evaluation in workerd leaves class bindings |
| 8 | incomplete (`PgSerialBuilder is not a constructor`, or the classic |
| 9 | `Cannot access '<minified>' before initialization` TDZ). |
| 10 | |
| 11 | ## Layout |
| 12 | |
| 13 | Mirrors the reporter's monorepo shape: |
| 14 | |
| 15 | - `schema/*` — db package tables (`pgTable` at module scope) |
| 16 | - `auth/*` — auth package tables that cross-import the db schema |
| 17 | - `worker.ts` — Worker entry that imports the full graph |
| 18 | |
| 19 | ## How the test pins the fix |
| 20 | |
| 21 | Small graphs stay single-chunk under Alchemy's default Worker bundler, so |
| 22 | `DrizzleSchemaChunks.test.ts` deploys a stack whose `Cloudflare.Worker` |
| 23 | *forces* the issue's chunk layout via `build` options: the schema group |
| 24 | captures only the schema modules (`includeDependenciesRecursively: false`), |
| 25 | leaving `drizzle-orm` in the entry chunk. The resulting graph is **cyclic** |
| 26 | (`worker.js -> auth-*.js -> worker.js`), so ESM evaluation runs the schema |
| 27 | chunk before drizzle's class bindings initialize — the TDZ. Cloudflare's |
| 28 | script-startup validation runs on exactly those chunks at upload, so the |
| 29 | deploy itself is the regression assertion; the test then verifies the |
| 30 | cyclic layout on disk and fetches the worker. |
| 31 | |
| 32 | The cycle is essential: an *acyclic* split (e.g. drizzle in its own chunk |
| 33 | imported by the schema chunk) evaluates correctly under plain import order |
| 34 | and never triggers the bug, with or without `strictExecutionOrder`. Setting |
| 35 | `strictExecutionOrder: false` on the stack's `build.output` restores the |
| 36 | exact issue error: |
| 37 | |
| 38 | ``` |
| 39 | ScriptStartupError: Uncaught ReferenceError: Cannot access 'a' before initialization |
| 40 | at auth-BFaahPAe.js:1:110 |
| 41 | ``` |
| 42 | |
| 43 | ## The fix |
| 44 | |
| 45 | `WorkerBundle` sets `strictExecutionOrder: true` on its rolldown output |
| 46 | options, which wraps cross-chunk modules so evaluation follows ESM semantics |
| 47 | regardless of how the graph was chunked. Before that default, this exact |
| 48 | split failed Cloudflare startup validation. The user-side `advancedChunks` |
| 49 | grouping workaround from the issue is no longer necessary. |