Skip to content

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

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

fix(sdk): add isBrowserEnvValue for tree-shaking#4454
ScriptedAlchemy wants to merge 23 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: 66a47c6

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 failed. Why did it fail? →

Name Link
🔨 Latest commit 66a47c6
🔍 Latest deploy log https://app.netlify.com/projects/module-federation-docs/deploys/69a734117465a10008f081ac

@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/devtools@d0cc48c

@module-federation/cli

pnpm add https://pkg.pr.new/@module-federation/cli@d0cc48c

create-module-federation

pnpm add https://pkg.pr.new/create-module-federation@d0cc48c

@module-federation/data-prefetch

pnpm add https://pkg.pr.new/@module-federation/data-prefetch@d0cc48c

@module-federation/dts-plugin

pnpm add https://pkg.pr.new/@module-federation/dts-plugin@d0cc48c

@module-federation/enhanced

pnpm add https://pkg.pr.new/@module-federation/enhanced@d0cc48c

@module-federation/error-codes

pnpm add https://pkg.pr.new/@module-federation/error-codes@d0cc48c

@module-federation/esbuild

pnpm add https://pkg.pr.new/@module-federation/esbuild@d0cc48c

@module-federation/managers

pnpm add https://pkg.pr.new/@module-federation/managers@d0cc48c

@module-federation/manifest

pnpm add https://pkg.pr.new/@module-federation/manifest@d0cc48c

@module-federation/metro

pnpm add https://pkg.pr.new/@module-federation/metro@d0cc48c

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

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

@module-federation/metro-plugin-rnef

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

@module-federation/modern-js

pnpm add https://pkg.pr.new/@module-federation/modern-js@d0cc48c

@module-federation/modern-js-v3

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

@module-federation/native-federation-tests

pnpm add https://pkg.pr.new/@module-federation/native-federation-tests@d0cc48c

@module-federation/native-federation-typescript

pnpm add https://pkg.pr.new/@module-federation/native-federation-typescript@d0cc48c

@module-federation/nextjs-mf

pnpm add https://pkg.pr.new/@module-federation/nextjs-mf@d0cc48c

@module-federation/node

pnpm add https://pkg.pr.new/@module-federation/node@d0cc48c

@module-federation/retry-plugin

pnpm add https://pkg.pr.new/@module-federation/retry-plugin@d0cc48c

@module-federation/rsbuild-plugin

pnpm add https://pkg.pr.new/@module-federation/rsbuild-plugin@d0cc48c

@module-federation/rspack

pnpm add https://pkg.pr.new/@module-federation/rspack@d0cc48c

@module-federation/rspress-plugin

pnpm add https://pkg.pr.new/@module-federation/rspress-plugin@d0cc48c

@module-federation/runtime

pnpm add https://pkg.pr.new/@module-federation/runtime@d0cc48c

@module-federation/runtime-core

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

@module-federation/runtime-tools

pnpm add https://pkg.pr.new/@module-federation/runtime-tools@d0cc48c

@module-federation/sdk

pnpm add https://pkg.pr.new/@module-federation/sdk@d0cc48c

@module-federation/storybook-addon

pnpm add https://pkg.pr.new/@module-federation/storybook-addon@d0cc48c

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

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

@module-federation/treeshake-frontend

pnpm add https://pkg.pr.new/@module-federation/treeshake-frontend@d0cc48c

@module-federation/treeshake-server

pnpm add https://pkg.pr.new/@module-federation/treeshake-server@d0cc48c

@module-federation/typescript

pnpm add https://pkg.pr.new/@module-federation/typescript@d0cc48c

@module-federation/utilities

pnpm add https://pkg.pr.new/@module-federation/utilities@d0cc48c

@module-federation/webpack-bundler-runtime

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

@module-federation/bridge-react

pnpm add https://pkg.pr.new/@module-federation/bridge-react@d0cc48c

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

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

@module-federation/bridge-shared

pnpm add https://pkg.pr.new/@module-federation/bridge-shared@d0cc48c

@module-federation/bridge-vue3

pnpm add https://pkg.pr.new/@module-federation/bridge-vue3@d0cc48c

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

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

commit: d0cc48c

@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 356.9 kB +349 B (+0.1%) 1.3 kB +1 B (+0.1%)
chrome-devtools 473.2 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
enhanced 1.29 MB no change 744 B no change
managers 70.0 kB no change 334 B no change
manifest 138.4 kB no change 182 B no change
metro-plugin-rnc-cli 0 B no change 314 B no change
node 188.1 kB no change 217 B no change
runtime 17.1 kB -13 B (-0.1%) 634 B no change
runtime-core 245.1 kB +42 B (+0.0%) 474 B no change
sdk 108.7 kB +574 B (+0.5%) 760 B +5 B (+0.7%)
utils 110.6 kB no change 328 B no change
vue3-bridge 138.0 kB +126 B (+0.1%) 21.1 kB +17 B (+0.1%)
webpack-bundler-runtime 89.5 kB -80 B (-0.1%) 405 B no change

Bundle targets

Package Web bundle (gzip) Delta Node bundle (gzip) Delta
bridge-react 17.0 kB -34 B (-0.2%) 17.4 kB -159 B (-0.9%)
chrome-devtools 20.8 kB -2 B (-0.0%) 20.8 kB -2 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%)
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.2 kB -7 B (-0.1%) 6.2 kB -7 B (-0.1%)
metro-plugin-rnc-cli 411 B -3 B (-0.7%) 411 B -3 B (-0.7%)
node 9.2 kB -4 B (-0.0%) 9.2 kB -4 B (-0.0%)
runtime 641 B no change 641 B no change
runtime-core 13.1 kB +3 B (+0.0%) 12.9 kB +3 B (+0.0%)
sdk 4.4 kB +1 B (+0.0%) 5.2 kB -2 B (-0.0%)
utils 2.6 kB -4 B (-0.2%) 2.6 kB -4 B (-0.2%)
vue3-bridge 16.1 kB -152 B (-0.9%) 15.9 kB -912 B (-5.3%)
webpack-bundler-runtime 3.8 kB no change 3.8 kB no change

Total dist (raw): 6.71 MB (+998 B (+0.0%))
Total ESM gzip: 79.0 kB (+23 B (+0.0%))
Total web bundle (gzip): 180.7 kB (-227 B (-0.1%))
Total node bundle (gzip): 181.5 kB (-1116 B (-0.6%))

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 🙏

ScriptedAlchemy and others added 6 commits February 24, 2026 11:13
Restore dynamic browser detection in isBrowserEnv.
Use it in browser-debug checks to avoid stale startup caching.

Co-authored-by: Cursor <cursoragent@cursor.com>
Resolve the dts-plugin TYPE-001 failure by correcting package entry paths for
workspace dependencies and updating RawSource usage for webpack typings.

Co-authored-by: Cursor <cursoragent@cursor.com>
Restore sdk and error-codes export paths to the filenames emitted by the
current build so CI package resolution no longer fails on these branches.

Co-authored-by: Cursor <cursoragent@cursor.com>
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