Skip to content

Comments

fix(sdk): add isBrowserEnvValue for tree-shaking#4454

Open
ScriptedAlchemy wants to merge 9 commits intomainfrom
cursor/tree-shaking-coverage-5fd5
Open

fix(sdk): add isBrowserEnvValue for tree-shaking#4454
ScriptedAlchemy wants to merge 9 commits intomainfrom
cursor/tree-shaking-coverage-5fd5

Conversation

@ScriptedAlchemy
Copy link
Member

@ScriptedAlchemy ScriptedAlchemy commented Feb 17, 2026

Description

Refactors isBrowserEnv from a function to a top-level const that respects ENV_TARGET and includes a runtime fallback. This change enables bundlers to effectively tree-shake browser-specific code when targeting non-browser environments. All call sites across runtime-core, sdk, and bridge packages have been updated to use the boolean isBrowserEnv directly.

Related Issue

No specific issue linked, but addresses the general goal of improving tree-shaking for isBrowserEnv usage.

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

Open in Cursor Open in Web

fixes: #4452

Co-authored-by: Zack Jackson <ScriptedAlchemy@users.noreply.github.com>
@cursor
Copy link

cursor bot commented Feb 17, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@changeset-bot
Copy link

changeset-bot bot commented Feb 17, 2026

🦋 Changeset detected

Latest commit: 2dc915f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 43 packages
Name Type
@module-federation/sdk Major
@module-federation/runtime-core Major
@module-federation/devtools Major
@module-federation/cli Major
@module-federation/data-prefetch Major
@module-federation/dts-plugin Major
@module-federation/enhanced Major
@module-federation/esbuild Patch
@module-federation/managers Major
@module-federation/manifest Major
@module-federation/metro Major
@module-federation/modern-js-v3 Major
@module-federation/modern-js Patch
@module-federation/nextjs-mf Patch
@module-federation/node Patch
@module-federation/retry-plugin Major
@module-federation/rsbuild-plugin Major
@module-federation/rspack Major
@module-federation/rspress-plugin Major
@module-federation/runtime Major
@module-federation/storybook-addon Major
@module-federation/utilities Patch
@module-federation/webpack-bundler-runtime Major
@module-federation/bridge-react-webpack-plugin Major
@module-federation/bridge-react Major
@module-federation/bridge-vue3 Major
shared-tree-shaking-no-server-host Patch
shared-tree-shaking-no-server-provider Patch
@module-federation/metro-plugin-rnc-cli Major
@module-federation/metro-plugin-rnef Major
shared-tree-shaking-with-server-host Patch
shared-tree-shaking-with-server-provider Patch
remote5 Patch
remote6 Patch
website-new Patch
@module-federation/runtime-tools Major
@module-federation/inject-external-runtime-core-plugin Major
@module-federation/third-party-dts-extractor Major
@module-federation/bridge-shared Major
@module-federation/error-codes Major
create-module-federation Major
@module-federation/treeshake-server Major
@module-federation/treeshake-frontend Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link

netlify bot commented Feb 17, 2026

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit 2dc915f
🔍 Latest deploy log https://app.netlify.com/projects/module-federation-docs/deploys/69969039fcec110008a748e8
😎 Deploy Preview https://deploy-preview-4454--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 17, 2026

Open in StackBlitz

@module-federation/devtools

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/devtools@2dc915f

@module-federation/cli

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/cli@2dc915f

create-module-federation

pnpm add https://pkg.pr.new/module-federation/core/create-module-federation@2dc915f

@module-federation/data-prefetch

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/data-prefetch@2dc915f

@module-federation/dts-plugin

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/dts-plugin@2dc915f

@module-federation/enhanced

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/enhanced@2dc915f

@module-federation/error-codes

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/error-codes@2dc915f

@module-federation/managers

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/managers@2dc915f

@module-federation/manifest

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/manifest@2dc915f

@module-federation/metro

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/metro@2dc915f

@module-federation/metro-plugin-rnc-cli

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/metro-plugin-rnc-cli@2dc915f

@module-federation/metro-plugin-rnef

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/metro-plugin-rnef@2dc915f

@module-federation/modern-js-v3

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/modern-js-v3@2dc915f

@module-federation/retry-plugin

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/retry-plugin@2dc915f

@module-federation/rsbuild-plugin

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/rsbuild-plugin@2dc915f

@module-federation/rspack

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/rspack@2dc915f

@module-federation/rspress-plugin

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/rspress-plugin@2dc915f

@module-federation/runtime

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/runtime@2dc915f

@module-federation/runtime-core

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/runtime-core@2dc915f

@module-federation/runtime-tools

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/runtime-tools@2dc915f

@module-federation/sdk

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/sdk@2dc915f

@module-federation/third-party-dts-extractor

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/third-party-dts-extractor@2dc915f

@module-federation/treeshake-frontend

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/treeshake-frontend@2dc915f

@module-federation/treeshake-server

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/treeshake-server@2dc915f

@module-federation/webpack-bundler-runtime

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/webpack-bundler-runtime@2dc915f

@module-federation/bridge-react

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/bridge-react@2dc915f

@module-federation/bridge-react-webpack-plugin

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/bridge-react-webpack-plugin@2dc915f

@module-federation/bridge-shared

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/bridge-shared@2dc915f

@module-federation/bridge-vue3

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/bridge-vue3@2dc915f

@module-federation/inject-external-runtime-core-plugin

pnpm add https://pkg.pr.new/module-federation/core/@module-federation/inject-external-runtime-core-plugin@2dc915f

commit: 2dc915f

@github-actions
Copy link
Contributor

github-actions bot commented Feb 17, 2026

Bundle Size Report

15 package(s) changed, 26 unchanged.

Package dist + ESM entry

Package Total dist (raw) Delta ESM gzip Delta
bridge-react 359.7 kB +139 B (+0.0%) 1.3 kB +3 B (+0.2%)
chrome-devtools 472.7 kB no change 3.9 kB no change
cli 27.4 kB no change 786 B no change
core 17.6 kB no change 436 B no change
dts-plugin 626.7 kB no change 23.4 kB no change
enhanced 1.29 MB no change 744 B no change
managers 64.7 kB no change 334 B no change
manifest 134.7 kB no change 182 B no change
metro-plugin-rnc-cli 0 B no change 314 B no change
nextjs-mf 111.4 kB no change 284 B no change
node 123.1 kB no change 489 B no change
runtime-core 305.1 kB +77 B (+0.0%) 25.5 kB -6 B (-0.0%)
sdk 105.0 kB +408 B (+0.4%) 8.2 kB +43 B (+0.5%)
utils 110.6 kB no change 328 B no change
vue3-bridge 159.0 kB +114 B (+0.1%) 23.3 kB +21 B (+0.1%)

Bundle targets

Package Web bundle (gzip) Delta Node bundle (gzip) Delta
bridge-react 17.1 kB -22 B (-0.1%) 20.5 kB -13 B (-0.1%)
chrome-devtools 20.7 kB -5 B (-0.0%) 20.7 kB -5 B (-0.0%)
cli 2.4 kB -6 B (-0.2%) 2.4 kB -6 B (-0.2%)
core 1.5 kB -4 B (-0.3%) 1.4 kB -5 B (-0.3%)
dts-plugin 16.4 kB -5 B (-0.0%) 16.4 kB -5 B (-0.0%)
enhanced 5.4 kB -11 B (-0.2%) 5.4 kB -11 B (-0.2%)
managers 2.4 kB -4 B (-0.2%) 2.4 kB -4 B (-0.2%)
manifest 6.1 kB -6 B (-0.1%) 6.1 kB -6 B (-0.1%)
metro-plugin-rnc-cli 411 B -3 B (-0.7%) 411 B -3 B (-0.7%)
nextjs-mf 6.7 kB -7 B (-0.1%) 6.7 kB -7 B (-0.1%)
node 9.7 kB -5 B (-0.1%) 9.7 kB -5 B (-0.1%)
runtime-core 13.0 kB +2 B (+0.0%) 12.8 kB +3 B (+0.0%)
sdk 4.2 kB -2 B (-0.0%) 5.7 kB +6 B (+0.1%)
utils 2.6 kB -4 B (-0.2%) 2.6 kB -4 B (-0.2%)
vue3-bridge 16.0 kB -163 B (-1.0%) 18.4 kB +2 B (+0.0%)

Total dist (raw): 6.89 MB (+738 B (+0.0%))
Total ESM gzip: 138.7 kB (+61 B (+0.0%))
Total web bundle (gzip): 184.8 kB (-245 B (-0.1%))
Total node bundle (gzip): 191.8 kB (-63 B (-0.0%))

Bundle sizes are generated with rslib (Rspack). Web/node bundles set ENV_TARGET and enable tree-shaking. Bare imports are externalized to keep sizes consistent with prior reporting, and assets are emitted as resources.

@ScriptedAlchemy ScriptedAlchemy changed the title Tree shaking coverage fix(sdk): add isBrowserEnvValue for tree-shaking Feb 17, 2026
@ScriptedAlchemy ScriptedAlchemy marked this pull request as ready for review February 17, 2026 01:24
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 82b5e2e668

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +11 to +14
const isBrowserEnvValue =
typeof ENV_TARGET !== 'undefined'
? ENV_TARGET === 'web'
: typeof window !== 'undefined' && typeof window.document !== 'undefined';

Choose a reason for hiding this comment

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

P2 Badge Recompute browser detection at call time

This change memoizes browser detection at module evaluation (isBrowserEnvValue) and then has isBrowserEnv() return that cached value, so imports that happen before DOM globals are attached (for example in SSR/test harness bootstraps that set window later) will permanently report non-browser and route runtime code down node-only paths. Previously isBrowserEnv() re-checked window/document each call, so this is a behavior regression in dynamic initialization contexts.

Useful? React with 👍 / 👎.

Copy link

@joshuaellis joshuaellis left a comment

Choose a reason for hiding this comment

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

Yep, this has improved things significantly, the mf-vite package still needs some other changes, but this is a very good first step as any node branches collapse to no-ops when we apply the ENV_TARGET. Thanks!

@joshuaellis
Copy link

Be great to get this merged and published so we can update the vite plugin upstream with the new release 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isBrowserEnv() stops tree shaking in bundlers and leaks node specific code to browser bundles

3 participants