Skip to content

fix(cli): use build plugin to redirect morphsdk ESM barrel to CJS - #10956

Merged
catrielmuller merged 1 commit into
mainfrom
fix/morphsdk-cjs-build-plugin
Jun 5, 2026
Merged

fix(cli): use build plugin to redirect morphsdk ESM barrel to CJS#10956
catrielmuller merged 1 commit into
mainfrom
fix/morphsdk-cjs-build-plugin

Conversation

@catrielmuller

Copy link
Copy Markdown
Contributor

Issue

Follow-up to #10955. The createRequire fix did not prevent the SyntaxError: Exported binding 'G9' needs to refer to a top-level declared variable from occurring. The build-cli job is still failing consistently on cli-linux-x64 in https://github.com/Kilo-Org/kilocode/actions/runs/27033311756/job/79791018541.

Root cause

Bun 1.3.14 with conditions: ["browser"] in Bun.build() resolves @morphllm/morphsdk/tools/warp-grep/client via the "import" condition (the pre-split ESM barrel client.js) even inside createRequire() calls. The surrounding module's ESM context overrides the implicit require condition, so all 52 chunk-*.js side-imports still flow through Bun's ESM splitter, which generates the invalid minified output.

Fix

Replace the createRequire runtime hack with a Bun build plugin (morphsdkCjsPlugin) added in script/build.ts. The plugin's onResolve callback intercepts @morphllm/morphsdk/tools/warp-grep/client before the ESM splitter is invoked and redirects it to the absolute path of client.cjs — the self-contained ~2300-line CJS bundle that has no chunk-*.js imports.

morphsdk.ts is simplified to a plain re-export; no source-level workaround is needed because the CJS redirection happens entirely at build time.

How to verify

Trigger the publish workflow after merging — build-cli (all targets, including cli-linux-x64) should pass without the SyntaxError.

Checklist

  • Issue linked above
  • bun run typecheck from packages/opencode/ passes
  • bun turbo typecheck (all 17 packages) passes
  • bun run script/check-opencode-annotations.ts passes
  • No changeset needed — CI-only fix
  • I personally reviewed the diff

The createRequire approach from #10955 did not prevent the Bun ESM
splitter from generating invalid output. Bun 1.3.14 with
`conditions: ["browser"]` resolves @morphllm/morphsdk via the "import"
condition (pre-split ESM barrel) even inside createRequire() calls,
pulling in 52 chunk-*.js files that cause:

  SyntaxError: Exported binding 'G9' needs to refer to a top-level declared variable.

Fix: add a morphsdkCjsPlugin in script/build.ts using Bun's onResolve
API to redirect the module specifier to the absolute path of client.cjs
before the ESM splitter is invoked. client.cjs is a self-contained
~2300-line CJS bundle with no chunk-*.js imports.

morphsdk.ts is simplified to a plain re-export — the CJS redirection
happens at build time, no source-level workaround needed.
@catrielmuller
catrielmuller enabled auto-merge June 5, 2026 19:04
@catrielmuller
catrielmuller merged commit 7d241ee into main Jun 5, 2026
20 checks passed
@catrielmuller
catrielmuller deleted the fix/morphsdk-cjs-build-plugin branch June 5, 2026 19:09
t7tran pushed a commit to t7tran/kilocode that referenced this pull request Aug 14, 2026
…d-plugin

fix(cli): use build plugin to redirect morphsdk ESM barrel to CJS
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.

2 participants