Skip to content

Process env build paths#4482

Open
ScriptedAlchemy wants to merge 89 commits intomainfrom
cursor/process-env-build-paths-15c0
Open

Process env build paths#4482
ScriptedAlchemy wants to merge 89 commits intomainfrom
cursor/process-env-build-paths-15c0

Conversation

@ScriptedAlchemy
Copy link
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

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

changeset-bot bot commented Feb 28, 2026

⚠️ No Changeset found

Latest commit: 7f35e91

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

pkg-pr-new bot commented Feb 28, 2026

Open in StackBlitz

@module-federation/devtools

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

@module-federation/cli

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

create-module-federation

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

@module-federation/data-prefetch

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

@module-federation/dts-plugin

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

@module-federation/enhanced

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

@module-federation/error-codes

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

@module-federation/esbuild

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

@module-federation/managers

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

@module-federation/manifest

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

@module-federation/metro

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

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

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

@module-federation/metro-plugin-rnef

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

@module-federation/modern-js

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

@module-federation/modern-js-v3

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

@module-federation/native-federation-tests

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

@module-federation/native-federation-typescript

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

@module-federation/nextjs-mf

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

@module-federation/node

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

@module-federation/retry-plugin

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

@module-federation/rsbuild-plugin

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

@module-federation/rspack

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

@module-federation/rspress-plugin

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

@module-federation/runtime

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

@module-federation/runtime-core

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

@module-federation/runtime-tools

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

@module-federation/sdk

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

@module-federation/storybook-addon

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

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

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

@module-federation/treeshake-frontend

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

@module-federation/treeshake-server

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

@module-federation/typescript

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

@module-federation/utilities

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

@module-federation/webpack-bundler-runtime

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

@module-federation/bridge-react

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

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

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

@module-federation/bridge-shared

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

@module-federation/bridge-vue3

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

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

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

commit: 2c86000

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

@codex review this pr

@chatgpt-codex-connector
Copy link

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
Member Author

@codex deep research for bugs

@chatgpt-codex-connector
Copy link

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

netlify bot commented Feb 28, 2026

Deploy Preview for module-federation-docs failed. Why did it fail? →

Name Link
🔨 Latest commit 7f35e91
🔍 Latest deploy log https://app.netlify.com/projects/module-federation-docs/deploys/69a7340c06f8040008b4745f

@github-actions
Copy link
Contributor

github-actions bot commented Feb 28, 2026

Bundle Size Report

11 package(s) changed, 30 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
webpack-bundler-runtime 89.5 kB +213 B (+0.2%) 405 B no change
runtime-core 244.9 kB +150 B (+0.1%) 474 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
runtime 17.1 kB -13 B (-0.1%) 634 B no change
rspack 58.6 kB +8 B (+0.0%) 211 B no change

Total dist: 6.71 MB (+6.5 kB (+0.1%))
Total ESM gzip: 79.0 kB (no change)

@ScriptedAlchemy
Copy link
Member Author

@codex review pr

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: 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') {

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}`;

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 👍 / 👎.

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.

3 participants