|
| 1 | +/** |
| 2 | + * #287 — the browser packages must actually bundle for a browser. |
| 3 | + * |
| 4 | + * `@metaobjectsdev/metadata`'s package root exports `MetaDataLoader`, which imports |
| 5 | + * `library/library-sources.ts`, which does `import { fileURLToPath } from "node:url"`. |
| 6 | + * So a single VALUE import from that root — `runtime-web` imported six `LAYOUT_*` |
| 7 | + * constants for `buildGrid` — dragged the Node-only loader into every browser bundle: |
| 8 | + * |
| 9 | + * error: Browser polyfill for module "node:url" doesn't have a matching export |
| 10 | + * named "fileURLToPath" |
| 11 | + * … metadata/dist/library/library-sources.js |
| 12 | + * |
| 13 | + * Because every generated `<Entity>.hooks.ts` imports `buildFilterQs` from |
| 14 | + * `runtime-web`, **no client consuming the generated hooks could build at all** — |
| 15 | + * unconditionally, on 0.21.3. Reported by an adopting project. |
| 16 | + * |
| 17 | + * Why nothing caught it: this package's tests run under Bun's *test* runner, which |
| 18 | + * resolves the `"bun"` export condition to TypeScript SOURCE and never bundles. The |
| 19 | + * failure only exists on the `dist` path a published consumer resolves, under a |
| 20 | + * browser-targeted bundler. Unit tests here can pass forever while the package is |
| 21 | + * unbuildable for its only audience. So this test does the one thing that reproduces it: |
| 22 | + * runs a real browser-target bundle over the BUILT output. |
| 23 | + * |
| 24 | + * The root barrel already keeps one Node-only module out deliberately |
| 25 | + * (`registry-coverage.ts`, with a comment saying why); `library-sources` reaches it via |
| 26 | + * the loader instead. The fix is `@metaobjectsdev/metadata/constants` — a barrel of pure |
| 27 | + * constant modules with no `node:*` anywhere in its graph — which browser packages import |
| 28 | + * VALUES from. Types may still come from the root: `import type` is erased at build time. |
| 29 | + */ |
| 30 | + |
| 31 | +import { describe, test, expect } from "bun:test"; |
| 32 | +import { existsSync } from "node:fs"; |
| 33 | +import { join } from "node:path"; |
| 34 | + |
| 35 | +const PKG_ROOT = join(import.meta.dir, ".."); |
| 36 | +const DIST_ENTRY = join(PKG_ROOT, "dist", "index.js"); |
| 37 | +const METADATA_CONSTANTS = join( |
| 38 | + PKG_ROOT, "..", "..", "..", "..", "server", "typescript", "packages", "metadata", "dist", "constants.js", |
| 39 | +); |
| 40 | + |
| 41 | +/** Bundle `entry` for the browser via Bun's bundler; resolve the failure text, if any. */ |
| 42 | +async function browserBundle(entry: string): Promise<{ ok: boolean; message: string }> { |
| 43 | + const built = await Bun.build({ entrypoints: [entry], target: "browser", throw: false }); |
| 44 | + return { |
| 45 | + ok: built.success, |
| 46 | + message: built.logs.map((l) => String(l)).join("\n"), |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +describe("#287 — browser bundleability", () => { |
| 51 | + test("the BUILT runtime-web entry bundles for a browser target", async () => { |
| 52 | + // dist/ is what a published consumer resolves. If it is missing the gate is |
| 53 | + // meaningless, so say so rather than skipping quietly. |
| 54 | + expect( |
| 55 | + existsSync(DIST_ENTRY), |
| 56 | + "dist/index.js is missing — run `bun run build` before this gate; " + |
| 57 | + "testing src/ would not reproduce #287 (Bun's test runner resolves the `bun` " + |
| 58 | + "export condition to TypeScript source and never bundles).", |
| 59 | + ).toBe(true); |
| 60 | + |
| 61 | + const { ok, message } = await browserBundle(DIST_ENTRY); |
| 62 | + // Name the original symptom so a future failure is self-diagnosing. |
| 63 | + expect(message).not.toMatch(/node:url|fileURLToPath/); |
| 64 | + expect(message).not.toMatch(/node:fs|node:path/); |
| 65 | + expect(ok).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + test("the metadata constants subpath is free of node:* in its whole graph", async () => { |
| 69 | + // The fix depends entirely on this barrel staying pure. One transitive node:* |
| 70 | + // import added here silently re-breaks every browser build downstream. |
| 71 | + expect(existsSync(METADATA_CONSTANTS)).toBe(true); |
| 72 | + const { ok, message } = await browserBundle(METADATA_CONSTANTS); |
| 73 | + expect(message).not.toMatch(/node:/); |
| 74 | + expect(ok).toBe(true); |
| 75 | + }); |
| 76 | + |
| 77 | + test("importing metamodel VALUES from the metadata ROOT is what breaks — a live demo", async () => { |
| 78 | + // Pins the causal claim rather than asserting it in a comment: bundling the root |
| 79 | + // barrel for a browser must still fail. If this ever starts passing, the root became |
| 80 | + // browser-safe and the constants subpath is no longer load-bearing — worth knowing |
| 81 | + // deliberately rather than discovering when someone "simplifies" the import back. |
| 82 | + const root = join( |
| 83 | + PKG_ROOT, "..", "..", "..", "..", "server", "typescript", "packages", "metadata", "dist", "index.js", |
| 84 | + ); |
| 85 | + if (!existsSync(root)) return; // metadata not built in this run — nothing to assert |
| 86 | + const { ok } = await browserBundle(root); |
| 87 | + expect(ok).toBe(false); |
| 88 | + }); |
| 89 | +}); |
0 commit comments