Skip to content

Process env build paths#4482

Merged
zhoushaw merged 90 commits intomainfrom
cursor/process-env-build-paths-15c0
Mar 8, 2026
Merged

Process env build paths#4482
zhoushaw merged 90 commits intomainfrom
cursor/process-env-build-paths-15c0

Conversation

@ScriptedAlchemy
Copy link
Copy Markdown
Member

Description

This PR refactors internal path resolution in several packages to leverage process.env.IS_ESM_BUILD for compile-time format detection, eliminating runtime heuristics and string replacements. This ensures that tsdown can perform dead code elimination, resulting in CJS and ESM build outputs that contain only the paths and logic relevant to their respective module formats.

Specifically:

  • nextjs-mf: Replaced string replacement and runtime path candidates with compile-time if (process.env.IS_ESM_BUILD === 'true') blocks to hardcode .mjs or .js paths for next-fragments and runtimePlugin.
  • node: Updated UniversalFederationPlugin to use process.env.IS_ESM_BUILD for selecting runtimePlugin.mjs or runtimePlugin.js.
  • sdk: Modified node.ts to use process.env.IS_ESM_BUILD for correct require vs createRequire handling.

This change guarantees that the generated dist files for CJS and ESM builds are correctly tree-shaken and contain only the necessary code.

Related Issue

Addresses feedback and comments from PR #4460.

Types of changes

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

Checklist

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

Open in Web Open in Cursor 

cursoragent and others added 30 commits February 17, 2026 20:44
Co-authored-by: zackary.l.jackson <zackary.l.jackson@gmail.com>
Use tsdown with nx run-commands for migrated packages and align export and type outputs for publint.
Remove legacy rollup configs and add shared migration scaffolding.

Co-authored-by: Cursor <cursoragent@cursor.com>
Align package build metadata with tsdown output and type extension updates.
Remove CJS fallback from ESM runtime alias resolution for consistent module format selection.

Co-authored-by: Cursor <cursoragent@cursor.com>
Use .d.mts for import types so exports match emitted ESM declaration files.

Co-authored-by: Cursor <cursoragent@cursor.com>
@cursor
Copy link
Copy Markdown

cursor bot commented Feb 28, 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
Copy Markdown

changeset-bot bot commented Feb 28, 2026

⚠️ No Changeset found

Latest commit: 10aa45b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Feb 28, 2026

Open in StackBlitz

@module-federation/devtools

pnpm add https://pkg.pr.new/@module-federation/devtools@47512f5

@module-federation/cli

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

create-module-federation

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

@module-federation/data-prefetch

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

@module-federation/dts-plugin

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

@module-federation/enhanced

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

@module-federation/error-codes

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

@module-federation/esbuild

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

@module-federation/managers

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

@module-federation/manifest

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

@module-federation/metro

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

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

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

@module-federation/metro-plugin-rnef

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

@module-federation/modern-js

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

@module-federation/modern-js-v3

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

@module-federation/native-federation-tests

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

@module-federation/native-federation-typescript

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

@module-federation/nextjs-mf

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

@module-federation/node

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

@module-federation/retry-plugin

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

@module-federation/rsbuild-plugin

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

@module-federation/rspack

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

@module-federation/rspress-plugin

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

@module-federation/runtime

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

@module-federation/runtime-core

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

@module-federation/runtime-tools

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

@module-federation/sdk

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

@module-federation/storybook-addon

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

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

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

@module-federation/treeshake-frontend

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

@module-federation/treeshake-server

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

@module-federation/typescript

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

@module-federation/utilities

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

@module-federation/webpack-bundler-runtime

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

@module-federation/bridge-react

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

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

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

@module-federation/bridge-shared

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

@module-federation/bridge-vue3

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

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

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

commit: 47512f5

@ScriptedAlchemy ScriptedAlchemy marked this pull request as ready for review February 28, 2026 02:05
@ScriptedAlchemy
Copy link
Copy Markdown
Member Author

@codex review this pr

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

ℹ️ 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".

Remove runtime candidate arrays and fallback probing. Select exactly one
plugin file per build: plugin.js for ESM, plugin.cjs for CJS.
- rslib.config: define process.env.IS_ESM_BUILD per format (true for esm, false for cjs)
- ModuleFederationPlugin: remove runtime candidate-array resolution
- Use deterministic branch on IS_ESM_BUILD for runtime-tools and runtime paths
- .js for ESM build, .cjs for CJS build
- Keep implementationPath and require.resolve paths behavior
- Preserve useful error message with specifier, paths, and original error
… tests

- Remove instantiate() call after link() in ConfigTestCases.rstest.ts
  Node's link() already performs instantiation; calling instantiate() on
  a linked module throws in Node 20+
- Add status checks for cached ESM modules: return early when evaluated,
  skip link when already linked
- Apply same instantiate() removal in asModule.js helper for consistency
@ScriptedAlchemy
Copy link
Copy Markdown
Member Author

@codex deep research for bugs

@chatgpt-codex-connector
Copy link
Copy Markdown

Summary

  • Added a missing changeset for the PR’s publishable package behavior changes, marking @module-federation/data-prefetch, @module-federation/enhanced, @module-federation/nextjs-mf, @module-federation/node, @module-federation/rspack, and @module-federation/sdk as patch. .changeset/deep-bugs-research.mdL1-L7
  • Documented the release note context for the ESM/CJS build-path refactor and Node 20 ESM evaluation alignment so the original PR changes are versioned correctly on release. .changeset/deep-bugs-research.mdL10-L10
  • Committed on branch work with commit 7f71455 and created a follow-up PR via the make_pr tool.

Testing

  • curl -L -s 'https://duckduckgo.com/html/?q=node+require.resolve+package+exports+subpath' | head -n 20
  • npx nx format:check (fails in current branch baseline; Nx falls back to all files because main ref is unavailable and reports multiple repo files needing formatting)
  • git status --short
  • git log --oneline -3

Skipped checks (and why)

  • Skipped package build/test matrices from AGENTS because this follow-up only adds a changeset file (docs/release metadata), with no additional source-code behavior changes beyond the existing PR.

View task →

Base automatically changed from feat/tsup to main February 28, 2026 06:47
@netlify
Copy link
Copy Markdown

netlify bot commented Feb 28, 2026

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit 10aa45b
🔍 Latest deploy log https://app.netlify.com/projects/module-federation-docs/deploys/69a93886c1d0f3000859e5ee
😎 Deploy Preview https://deploy-preview-4482--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.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 28, 2026

Bundle Size Report

9 package(s) changed, 32 unchanged.

Package Total dist Delta ESM gzip Delta
modern-js-plugin 184.8 kB +4.0 kB (+2.2%) 1.2 kB no change
modern-js-plugin-v3 176.4 kB +4.0 kB (+2.3%) 872 B no change
enhanced 1.29 MB -1621 B (-0.1%) 744 B no change
data-prefetch 118.2 kB -683 B (-0.6%) 105 B no change
node 188.4 kB +348 B (+0.2%) 217 B no change
runtime-tools 4.9 kB +39 B (+0.8%) 95 B no change
nextjs-mf 127.4 kB -28 B (-0.0%) 160 B no change
inject-external-runtime-core-plugin 5.4 kB -26 B (-0.5%) 548 B no change
rspack 58.6 kB +8 B (+0.0%) 211 B no change

Total dist: 6.72 MB (+6.2 kB (+0.1%))
Total ESM gzip: 79.1 kB (no change)

@ScriptedAlchemy
Copy link
Copy Markdown
Member Author

@codex review pr

Copy link
Copy Markdown

@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: 8ecaacd542

ℹ️ 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".


let requireFn: NodeRequire;
if (process.env['IS_ESM_BUILD'] === 'true') {
if (process.env.IS_ESM_BUILD === 'true') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Use bracket access for undeclared IS_ESM_BUILD env key

packages/sdk/tsconfig.json enables noPropertyAccessFromIndexSignature, and NodeJS.ProcessEnv in this package does not declare IS_ESM_BUILD; using process.env.IS_ESM_BUILD here raises TS4111 during type/declaration builds. Since sdk builds with tsdown + dts.resolver: 'tsc', this can break the package build unless the env key is declared in ProcessEnv or accessed via process.env['IS_ESM_BUILD'].

Useful? React with 👍 / 👎.

Comment on lines +39 to +41
const runtimeToolsSpec = `@module-federation/runtime-tools/dist/index${ext}`;
const bundlerRuntimeSpec = `@module-federation/webpack-bundler-runtime/dist/index${ext}`;
const runtimeSpec = `@module-federation/runtime/dist/index${ext}`;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restore legacy runtime entry fallbacks for implementation path

This change hardcodes runtime resolution to dist/index.{js,cjs} only, removing the previous fallback set for legacy entry names and package-root resolution. In mixed-version setups (especially when implementation points to older runtime packages), require.resolve can now fail during plugin initialization, which prevents runtime alias wiring and breaks federation startup.

Useful? React with 👍 / 👎.

@zhoushaw zhoushaw merged commit c2a1d99 into main Mar 8, 2026
24 checks passed
@zhoushaw zhoushaw deleted the cursor/process-env-build-paths-15c0 branch March 8, 2026 23:52
@2heal1 2heal1 mentioned this pull request Mar 14, 2026
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.

4 participants