fix: build Bun-native ESM bundle for OpenCode plugin#415
Merged
Conversation
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.
53182bc to
39d7c37
Compare
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #401 — the
@loreai/opencodeplugin fails when OpenCode is started directly (not vialore run) because thebunexport condition pointed to source files not shipped in the npm package, and the CJS bundle hard-requiresnode:sqlitewhich Bun doesn't implement.Changes
packages/gateway/package.json: Pointbunexport atdist/index.bun.js(a real shipped bundle instead of missing source), addpreparescript for monorepo dev shim, add new files tofilesarraypackages/gateway/script/bundle.ts: Build a Bun-native ESM bundle (dist/index.bun.js) withconditions: ["bun"]so#db/driverresolves tobun:sqlite. Reuses the existing@sentry/bun→@sentry/noderemap (works under Bun). Also builds an ESM embedding worker (dist/embedding-worker.js) since the Bun bundle resolves worker paths viaimport.meta.urlpackages/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 BunResolution matrix
dist/index.bun.js(dev shim → source)bun:sqlite@sentry/bundist/index.cjsnode:sqlite@sentry/node(remapped)dist/index.bun.jsbun:sqlite@sentry/node(remapped)