Skip to content

fix: build Bun-native ESM bundle for OpenCode plugin#415

Merged
BYK merged 1 commit into
mainfrom
fix/bun-bundle-for-opencode-plugin
May 20, 2026
Merged

fix: build Bun-native ESM bundle for OpenCode plugin#415
BYK merged 1 commit into
mainfrom
fix/bun-bundle-for-opencode-plugin

Conversation

@BYK
Copy link
Copy Markdown
Owner

@BYK BYK commented May 20, 2026

Summary

Fixes #401 — the @loreai/opencode plugin fails when OpenCode is started directly (not via lore run) because the bun export condition pointed to source files not shipped in the npm package, and the CJS bundle hard-requires node:sqlite which Bun doesn't implement.

Changes

  • packages/gateway/package.json: Point bun export at dist/index.bun.js (a real shipped bundle instead of missing source), add prepare script for monorepo dev shim, add new files to files array
  • packages/gateway/script/bundle.ts: Build a Bun-native ESM bundle (dist/index.bun.js) with conditions: ["bun"] so #db/driver resolves to bun:sqlite. Reuses the existing @sentry/bun@sentry/node remap (works under Bun). Also builds an ESM embedding worker (dist/embedding-worker.js) since the Bun bundle resolves worker paths via import.meta.url
  • packages/gateway/test/bundle-exports.test.ts (new): Smoke test with two layers — static content checks (correct sqlite driver per bundle, export-vs-files consistency) and runtime import verification under Bun

Resolution matrix

Scenario Export condition SQLite driver Sentry
Monorepo dev (Bun) dist/index.bun.js (dev shim → source) bun:sqlite @sentry/bun
npm + Node.js (CLI) dist/index.cjs node:sqlite @sentry/node (remapped)
npm + Bun (OpenCode plugin) dist/index.bun.js bun:sqlite @sentry/node (remapped)

The @loreai/opencode plugin fails when OpenCode is started directly
(not via `lore run`) because:

1. The `bun` export condition pointed to `./src/index.ts` which isn't
   shipped in the npm package (only `dist/` is in `files`).

2. The CJS bundle hard-requires `node:sqlite` which Bun doesn't
   implement.

Fix: Build a second ESM bundle (`dist/index.bun.js`) with
`conditions: ["bun"]` so `#db/driver` resolves to `bun:sqlite`.
Remap `@sentry/bun` to `@sentry/node` (same as the CJS build —
works under Bun, proven by getsentry/cli).

Also adds an ESM embedding worker (`dist/embedding-worker.js`) since
the Bun bundle resolves worker paths via `import.meta.url`.

A `prepare` script creates a dev shim in the monorepo so the `bun`
export condition works before building.

Includes a smoke test that verifies bundle contents (correct sqlite
driver per bundle, export-vs-files consistency) and runtime
importability under Bun.
@BYK BYK force-pushed the fix/bun-bundle-for-opencode-plugin branch from 53182bc to 39d7c37 Compare May 20, 2026 08:24
@BYK BYK merged commit 465ef2c into main May 20, 2026
12 of 13 checks passed
@BYK BYK deleted the fix/bun-bundle-for-opencode-plugin branch May 20, 2026 08:28
BYK added a commit that referenced this pull request May 20, 2026
… bun exports (#425)

## Summary

Fixes the lore startup failure in OpenCode caused by PR #415 — `Cannot
find module '@loreai/gateway'`.

PR #415 changed the gateway's `"bun"` export condition from
`./src/index.ts` (always present) to `./dist/index.bun.js` (requires
`bun run bundle`). A `prepare` script was added to create a dev shim,
but Bun does not execute lifecycle scripts for workspace packages, so
the file was never created.

## Changes

- **`packages/gateway/script/build.ts`**: Replace the no-op
`buildLibrary()` with a function that creates lightweight dev shims
(`export * from "../src/index.ts"`) for `dist/index.bun.js` and
`dist/embedding-worker.js`. Skips overwriting real bundle output by
content-checking existing files.
- **`packages/gateway/package.json`**: Remove the broken `prepare`
script (never ran for workspace packages).
- **`package.json` (root)**: Add `postinstall` script that runs `bun
--filter '@loreai/gateway' build` so `bun install` alone creates the dev
shims.
- **`packages/gateway/test/bundle-exports.test.ts`**: Update skip
condition to detect dev shims vs real bundles so content-checking tests
correctly skip in dev.

## How it works

- `bun run build` (workspace-wide) now creates dev shims for gateway's
`"bun"` exports
- `bun run bundle` (production) still wipes `dist/` first and creates
real artifacts — shims never interfere
- Root `postinstall` ensures fresh clones + `bun install` have working
exports immediately
- `.gitignore` already excludes `dist/`, so shims aren't committed
This was referenced May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Plugin fails when starting OpenCode directly (works only via lore run)

1 participant