fix: bundle plugin to JS for server mode compatibility#36
Conversation
Bun's on-the-fly TypeScript transpilation fails to extract named exports from CJS modules (like @opentelemetry/*) when loaded via `await import()` in opencode's compiled binary. This causes plugins to fail in server/CLI mode while working fine in TUI mode. Fix by pre-bundling all dependencies into a single JavaScript file using `bun build`, which resolves imports at build time and avoids CJS/ESM interop issues at runtime. - Add `build` script: `bun build src/index.ts --outdir=./dist --target=node` - Update exports/main/module to point to `dist/index.js` - Add `prepublishOnly` to ensure build runs before npm publish - Add `./server` export for explicit server mode entry point Fixes DEVtheOPS#35
📝 WalkthroughWalkthroughPackage distribution configuration updated to publish built JavaScript artifacts from Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
package.json (1)
61-67: Considerprepackinstead of (or in addition to)prepublishOnly.
prepublishOnlyonly runs onnpm publish.prepackadditionally runs onnpm packand when the package is installed from a git URL/tarball, which is a common way users try plugins before they hit npm. Givendist/is gitignored, installing from git today would produce a broken package.♻️ Suggested change
- "prepublishOnly": "bun run build" + "prepack": "bun run build"🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@package.json` around lines 61 - 67, The package.json currently only defines a "prepublishOnly" script which runs build before npm publish; add a "prepack" script that also runs the build (same command as "build") so packaging via npm pack or installing from a git/tarball will include the built dist; update the "scripts" object to include "prepack": "bun run build" (or the identical command used by "prepublishOnly"/"build") and ensure it runs the same build pipeline as the existing "build" script.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@package.json`:
- Around line 31-34: The package currently publishes only JS (exports: "." and
"./server" -> "./dist/index.js") but omits TypeScript declarations because
tsconfig.json has noEmit: true and bun build doesn't emit .d.ts, so TypeScript
consumers lose types; add a build-time declaration step: create a
tsconfig.build.json that overrides noEmit to false, sets declaration: true and
emitDeclarationOnly: true with outDir: "dist", update the "build" script to run
that tsc declaration step (or a separate "build:types" command) so .d.ts files
are emitted into dist/, and update package.json to expose them via the types
field/condition in the export map (add "types": "./dist/index.d.ts" or a "types"
export condition alongside the existing exports) so consumers get typings.
---
Nitpick comments:
In `@package.json`:
- Around line 61-67: The package.json currently only defines a "prepublishOnly"
script which runs build before npm publish; add a "prepack" script that also
runs the build (same command as "build") so packaging via npm pack or installing
from a git/tarball will include the built dist; update the "scripts" object to
include "prepack": "bun run build" (or the identical command used by
"prepublishOnly"/"build") and ensure it runs the same build pipeline as the
existing "build" script.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: fd3d252c-e3c4-410d-9d15-462d6e75fbb6
📒 Files selected for processing (1)
package.json
Add tsconfig.build.json that emits only declaration files into dist/, so TypeScript consumers retain type information after the switch from source to bundled JS entry points. - Add tsconfig.build.json with emitDeclarationOnly + rootDir: ./src - Update build script to run tsc -p tsconfig.build.json after bundling - Add types field to package.json exports and top-level - Include .d.ts condition in export map for both . and ./server Addresses CodeRabbit review comment on PR DEVtheOPS#36
|
Addressed CodeRabbit's review comment about missing TypeScript declarations:
Now |
prepack runs on both npm publish and npm pack (and when installing from git/tarball), whereas prepublishOnly only runs on npm publish. Since dist/ is gitignored, installing directly from git would produce a broken package without this change. Addresses CodeRabbit nitpick comment on PR DEVtheOPS#36
Problem
Plugin fails to load in server/CLI mode (
opencode run) with errors like:Works fine in TUI mode. Seems like a difference in plugin loading behaviour between TUI and other modes.
Root Cause
Best effort explanation from AI below -
Fix
Pre-bundle all dependencies into a single JavaScript file using
bun build, which resolves imports at build time and avoids CJS/ESM interop issues at runtime.bun buildresolves all imports at build time, inlining the CJS modules into a single JavaScript file. The named exports are extracted during the build, not at runtime. When opencode loads the bundled file, there are no CJS modules left to interop with — everything is already ESM.Changes
buildscript:bun build src/index.ts --outdir=./dist --target=node && tsc -p tsconfig.build.jsonexports/main/moduleto point todist/index.js./serverexport for explicit server mode entry pointtypescondition to exports map and top-leveltypesfield for TypeScript consumerstsconfig.build.jsonto emit declaration files alongside bundled JSprepackhook (instead ofprepublishOnly) to ensure build runs before npm publish, npm pack, and when installing from gitfilesto shipdist/instead ofsrc/Verification
I have verified that it works manually via -
Happy to delete the package after fix is merged.
Fixes #35