Skip to content

Commit b54120c

Browse files
committed
fix: use createRequire for CJS interop with @slack packages
Bun's bundler cannot resolve named exports from CJS modules that use Object.defineProperty. Namespace imports also lose getter-defined properties during bundling. Using createRequire forces runtime CJS loading which correctly resolves all constructors. Signed-off-by: xuezhaojun <xuezhaokeepgoing@gmail.com>
1 parent 6f34a2e commit b54120c

3 files changed

Lines changed: 13 additions & 12 deletions

File tree

opencode-slack-plugin/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

opencode-slack-plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kubeopencode/opencode-slack-plugin",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"type": "module",
55
"license": "MIT",
66
"description": "OpenCode plugin that connects to Slack via Socket Mode — zero port exposure, runs inside your OpenCode process",

opencode-slack-plugin/src/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// Copyright Contributors to the KubeOpenCode project
22
import type { PluginModule, PluginInput, Hooks } from "@opencode-ai/plugin"
3-
// CJS/ESM interop: @slack/web-api and @slack/socket-mode are CommonJS modules.
4-
// Named imports (e.g. import { WebClient }) fail in Bun's ESM loader because
5-
// the exports are defined via Object.defineProperty — not resolvable by static
6-
// analysis. Namespace imports work because they import the entire module.exports
7-
// object and destructure at runtime.
8-
import * as SlackWebApi from "@slack/web-api"
9-
import * as SlackSocketMode from "@slack/socket-mode"
3+
import { createRequire } from "node:module"
104
import * as os from "os"
11-
const { WebClient } = SlackWebApi
12-
const { SocketModeClient } = SlackSocketMode
5+
// CJS/ESM interop: @slack/web-api and @slack/socket-mode are CommonJS modules
6+
// that use Object.defineProperty for their exports. Bun's bundler cannot
7+
// resolve named imports from these (it produces "X is not a constructor" at
8+
// runtime). Namespace imports (import * as) also fail because Bun's bundled
9+
// namespace object loses the getter-defined properties. Using createRequire to
10+
// load them as CJS guarantees the real module.exports with all constructors.
11+
const require = createRequire(import.meta.url)
12+
const { WebClient } = require("@slack/web-api") as typeof import("@slack/web-api")
13+
const { SocketModeClient } = require("@slack/socket-mode") as typeof import("@slack/socket-mode")
1314

1415
// ---------------------------------------------------------------------------
1516
// Types

0 commit comments

Comments
 (0)