fix: bundler compatibility for Cloudflare Workers (esbuild/wrangler)#3
Open
ronaldburns wants to merge 2 commits intostandardagents:mainfrom
Open
fix: bundler compatibility for Cloudflare Workers (esbuild/wrangler)#3ronaldburns wants to merge 2 commits intostandardagents:mainfrom
ronaldburns wants to merge 2 commits intostandardagents:mainfrom
Conversation
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
There was a problem hiding this comment.
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
./workerdsubpath 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.
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
./workerdsubpath export so consumers can explicitly import the workerd entry when dynamic imports don't resolve theworkerdexport conditionfs/promisesandmoduleto prevent esbuild/wrangler from failing the build on Node.js-only modulesProblem
When using SIP in a Cloudflare Workers project with wrangler, the build fails:
This happens because:
index.jscontainsawait import('fs/promises')- Even though this code is behind aif (isNode)runtime check and never executes in Workers, esbuild statically analyzes string literal imports and tries to resolve them at bundle time.Dynamic imports don't use export conditions -
await import("@standardagents/sip")resolves toindex.js(the default entry) rather thanworkerd.js, because esbuild doesn't apply theworkerdexport condition to dynamic imports. Consumers have no way to explicitly request the workerd entry since./dist/workerd.jsisn'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:Subpath export (
package.json): Add./workerdas an explicit subpath so consumers can bypass condition resolution:Test plan
pnpm build:codesucceedswrangler deploy --dry-runsucceeds with both static and dynamic importsfs/promisesandmoduleimports still work at runtime in Node.js (variable indirection doesn't affect runtime resolution)