Skip to content

fix: bundler compatibility for Cloudflare Workers (esbuild/wrangler)#3

Open
ronaldburns wants to merge 2 commits intostandardagents:mainfrom
HBGames:main
Open

fix: bundler compatibility for Cloudflare Workers (esbuild/wrangler)#3
ronaldburns wants to merge 2 commits intostandardagents:mainfrom
HBGames:main

Conversation

@ronaldburns
Copy link
Copy Markdown

@ronaldburns ronaldburns commented Apr 14, 2026

Summary

  • Add ./workerd subpath export so consumers can explicitly import the workerd entry when dynamic imports don't resolve the workerd export condition
  • Use variable-based dynamic imports for fs/promises and module to prevent esbuild/wrangler from failing the build on Node.js-only modules

Problem

When using SIP in a Cloudflare Workers project with wrangler, the build fails:

✘ [ERROR] Could not resolve "fs/promises"
✘ [ERROR] Could not resolve "module"

This happens because:

  1. index.js contains await import('fs/promises') - Even though this code is behind a if (isNode) runtime check and never executes in Workers, esbuild statically analyzes string literal imports and tries to resolve them at bundle time.

  2. Dynamic imports don't use export conditions - await import("@standardagents/sip") resolves to index.js (the default entry) rather than workerd.js, because esbuild doesn't apply the workerd export condition to dynamic imports. Consumers have no way to explicitly request the workerd entry since ./dist/workerd.js isn't an exported subpath.

Fix

Node.js imports (src/wasm/loader.ts, src/decoders/simple.ts): Move the module specifier into a variable so esbuild can't statically resolve it:

// Before: esbuild resolves and fails
const { readFile } = await import('fs/promises');

// After: esbuild skips variable-based dynamic imports
const fsModule = 'fs/promises';
const { readFile } = await import(fsModule);

Subpath export (package.json): Add ./workerd as an explicit subpath so consumers can bypass condition resolution:

import { transform } from "@standardagents/sip/workerd";

Test plan

  • pnpm build:code succeeds
  • Existing Node.js tests still pass (runtime behavior unchanged)
  • Verified in a real wrangler project: wrangler deploy --dry-run succeeds with both static and dynamic imports
  • The fs/promises and module imports still work at runtime in Node.js (variable indirection doesn't affect runtime resolution)

Wrangler's esbuild bundler resolves the "workerd" export condition for
static imports but not for dynamic imports (await import()). This means
consumers using dynamic imports get the Node.js entry (index.js) which
imports fs/promises and fails in Workers.

Adding ./workerd as an explicit subpath export lets consumers write:
  import { transform } from "@standardagents/sip/workerd"
to get the workerd entry directly, bypassing condition resolution.
Use variable-based dynamic imports for fs/promises and module so
esbuild/wrangler cannot statically resolve them at bundle time.
These imports are behind runtime Node.js detection and never execute
in Workers, but esbuild fails the build because it tries to resolve
the string literal import paths.

Before: await import('fs/promises')  -- esbuild resolves and fails
After:  const m = 'fs/promises'; await import(m)  -- esbuild skips
Copilot AI review requested due to automatic review settings April 14, 2026 22:00
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves compatibility when bundling @standardagents/sip for Cloudflare Workers (esbuild/wrangler) by avoiding bundler resolution of Node-only modules and by adding an explicit subpath export for the workerd entrypoint.

Changes:

  • Reworked Node-only dynamic imports (fs/promises, module) to use variable-based specifiers so esbuild/wrangler won’t attempt to resolve them during Workers bundling.
  • Added an explicit ./workerd subpath export so consumers can import the workerd entry directly when dynamic imports don’t honor export conditions.
  • Updated the built artifact (dist/index.js) to match the source changes.

Reviewed changes

Copilot reviewed 3 out of 5 changed files in this pull request and generated no comments.

File Description
src/wasm/loader.ts Avoids bundler-time resolution of fs/promises by moving the specifier into a variable for the Node-only branch.
src/decoders/simple.ts Applies the same variable-based import approach for fs/promises and module in the Node-only codec init path.
package.json Adds ./workerd to exports to enable explicit workerd entry imports.
dist/index.js Updates the published build output to reflect the new dynamic import patterns.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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