Skip to content

fix: make Node-only imports bundler-discoverable#885

Open
barjin wants to merge 3 commits intomasterfrom
claude/fix-bundle-dependencies-GzCLE
Open

fix: make Node-only imports bundler-discoverable#885
barjin wants to merge 3 commits intomasterfrom
claude/fix-bundle-dependencies-GzCLE

Conversation

@barjin
Copy link
Copy Markdown
Member

@barjin barjin commented Apr 18, 2026

Closes #861, #852

The dynamicNodeImport(specifier) helper hid proxy-agent from every bundler, so anything targeting Node (esbuild, Turbo, bun build --compile) crashed at runtime with Cannot find module 'proxy-agent'. Replacing it with plain await import('proxy-agent') / await import('node:*') makes those imports statically discoverable, so bundlers include them naturally.

The browser build strips the same imports back out via an IgnorePlugin for proxy-agent and resolve.alias: false entries for the os / zlib / util built-ins that are only used in isNode()-guarded code paths; output.asyncChunks: false keeps the UMD bundle a single file. The resulting dist/bundle.js stays at 907 KB with the same module list as before, and all four bundlers in test/bundling still build cleanly.

The isNode() now also checks the BROWSER_BUILD global variable - if it's set, we're running the browser bundle (so we do not even try loading the Node-specific parts, as they might be missing from the current environment).

https://claude.ai/code/session_01LnDB4Jd3qCoNoJBKgvWvFZ

Replaces the `dynamicNodeImport(specifier)` trick with plain static
`await import('proxy-agent')` / `await import('node:*')` calls so that
Node-targeting bundlers (esbuild, webpack, Turbo, `bun build --compile`)
can statically discover and include `proxy-agent` in their output.

The browser `rsbuild` build now strips these imports out via an
`IgnorePlugin` for `proxy-agent` and `resolve.alias: false` aliases for
the `node:*` / bare-name Node built-ins that are only referenced from
code paths guarded by `isNode()`. `asyncChunks: false` inlines the
remaining (empty) dynamic imports into the single UMD bundle.

Resulting browser bundle is the same size as before (907 KB) while
the Node dist now exposes `proxy-agent` as a static `require`, fixing
`Cannot find module 'proxy-agent'` when bundling the client.
@github-actions github-actions bot added this to the 138th sprint - Tooling team milestone Apr 18, 2026
@github-actions github-actions bot added the t-tooling Issues with this label are in the ownership of the tooling team. label Apr 18, 2026
@barjin barjin linked an issue Apr 20, 2026 that may be closed by this pull request
@barjin barjin requested a review from Copilot April 20, 2026 05:47
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 adjusts Node-only dynamic imports so bundlers targeting Node can statically discover and include required dependencies (notably proxy-agent), while ensuring the browser bundle still excludes those Node-only modules and built-ins.

Changes:

  • Replaced the dynamicNodeImport() wrapper with direct import('proxy-agent') / import('node:*') to make imports bundler-discoverable.
  • Added a BROWSER_BUILD-based short-circuit to isNode() to prevent Node-only code paths from running in the browser bundle.
  • Updated the browser bundling config (Rspack via Rsbuild) to ignore proxy-agent, alias selected Node built-ins to false, and keep the UMD bundle single-file (asyncChunks: false).

Reviewed changes

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

File Description
src/utils.ts Switches Node-only imports to direct import('node:*'), removes dynamicNodeImport(), and updates Node detection for browser bundles.
src/http_client.ts Updates Node-only initialization to use direct import('proxy-agent') / import('node:os') so bundlers can include them.
rsbuild.config.ts Configures the browser build to strip Node-only modules and disable selected built-in resolutions; keeps output as a single UMD file.

💡 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

t-tooling Issues with this label are in the ownership of the tooling team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dynamic import of proxy-agent breaks bun single executable bundling Cannot find module 'proxy-agent' when using esbuild

4 participants