sqlite-bundle.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { describe, expect, test } from "bun:test"
  2. import type { BunPlugin } from "bun"
  3. import { join } from "path"
  4. // Pins the #sqlite subpath-import condition gate: bundling database.ts for a
  5. // non-Bun runtime must never reach the static `import "bun:sqlite"` in
  6. // sqlite.bun.ts, which crashes workerd (and any other non-Bun bundle) at
  7. // module load. Bare imports are externalized so only core-relative modules —
  8. // including the #sqlite resolution under test — end up in the bundle.
  9. const externalizeBare: BunPlugin = {
  10. name: "externalize-bare",
  11. setup(build) {
  12. build.onResolve({ filter: /^[^.#/]/ }, (args) => ({ path: args.path, external: true }))
  13. },
  14. }
  15. const bundle = async (conditions: Array<string>) => {
  16. const result = await Bun.build({
  17. // join over URL.pathname: on Windows the latter yields "/C:/..." which
  18. // module resolution rejects.
  19. entrypoints: [join(import.meta.dir, "../src/database/database.ts")],
  20. target: "browser",
  21. conditions,
  22. plugins: [externalizeBare],
  23. throw: false,
  24. })
  25. expect(result.logs).toEqual([])
  26. expect(result.success).toBe(true)
  27. return result.outputs[0].text()
  28. }
  29. // Skipped on Windows: Bun.build inside `bun test` reliably panics Bun 1.3.14
  30. // there ("Internal assertion failure", twice in a row on CI, always at this
  31. // file). The assertions pin platform-independent package.json condition
  32. // resolution, so Linux coverage loses nothing.
  33. describe.skipIf(process.platform === "win32")("sqlite bundle conditions", () => {
  34. test("workerd conditions select the Durable Object driver and never bun:sqlite", async () => {
  35. const output = await bundle(["workerd"])
  36. expect(output).not.toContain("bun:sqlite")
  37. expect(output).not.toContain("node:sqlite")
  38. expect(output).toContain("SqliteWorkerd")
  39. })
  40. test("default conditions fall back to node:sqlite, not bun:sqlite", async () => {
  41. const output = await bundle([])
  42. expect(output).not.toContain("bun:sqlite")
  43. expect(output).toContain("SqliteNode")
  44. })
  45. })