Skip to content

feat(core): add Module Federation compatibility mode - #1407

Merged
9aoy merged 50 commits into
mainfrom
feat/federation-v2
Jul 24, 2026
Merged

feat(core): add Module Federation compatibility mode#1407
9aoy merged 50 commits into
mainfrom
feat/federation-v2

Conversation

@ScriptedAlchemy

Copy link
Copy Markdown
Contributor

Summary

Rstest's Node test runtime overrides import() and webpack chunk handlers for mocking, which breaks tests that load Module Federation remotes. This PR adds an opt-in federation compatibility mode so federated apps can be tested in Node:

  • New federation: boolean config option and --federation CLI flag.
  • Runtime shims in mockRuntimeCode.js, gated on globalThis.__rstest_federation__: a placeholder chunk-handler bootstrap, an __rstest_dynamic_import__ fallback for non-mocked specifiers, and a __webpack_require__.f Proxy guard so federation's chunk loaders coexist with the mock runtime.
  • Federation example apps (examples/federation), 8 unit tests, an e2e suite with 3 fixtures, and bilingual docs.

This mode pairs with the @module-federation/rstest Rsbuild plugin (module-federation/core#4320); the examples pin its latest pkg.pr.new canary (1b77b1a). Browser mode is unaffected. Supersedes #842.

Validation: lint, typecheck, build, unit tests, e2e, and federation examples all pass locally (webkit e2e skipped: missing system deps).

Related Links

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

ScriptedAlchemy added 5 commits June 12, 2026 00:55
Comment thread packages/core/src/core/plugins/mockRuntimeCode.js Fixed
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 12, 2026

Copy link
Copy Markdown

Deploying rstest with  Cloudflare Pages  Cloudflare Pages

Latest commit: 35b9ec2
Status: ✅  Deploy successful!
Preview URL: https://995489b4.rstest.pages.dev
Branch Preview URL: https://feat-federation-v2.rstest.pages.dev

View logs

@github-actions

Copy link
Copy Markdown

pkg.pr.new preview 40086e4 · workflow run

  • pnpm add https://pkg.pr.new/@rstest/adapter-rsbuild@40086e4
  • pnpm add https://pkg.pr.new/@rstest/adapter-rslib@40086e4
  • pnpm add https://pkg.pr.new/@rstest/adapter-rspack@40086e4
  • pnpm add https://pkg.pr.new/@rstest/browser@40086e4
  • pnpm add https://pkg.pr.new/@rstest/browser-react@40086e4
  • pnpm add https://pkg.pr.new/@rstest/core@40086e4
  • pnpm add https://pkg.pr.new/@rstest/coverage-istanbul@40086e4
  • pnpm add https://pkg.pr.new/@rstest/coverage-v8@40086e4

@github-actions

github-actions Bot commented Jun 12, 2026

Copy link
Copy Markdown

Rsdoctor Bundle Diff Analysis

Found 13 projects in monorepo, 4 projects with changes.

📊 Quick Summary
Project Total Size Change
adapter-rslib 26.3 KB 0
adapter-rspack 9.2 KB 0
adapter-rsbuild 6.1 KB 0
browser 2.1 MB +159.0 B (0.0%)
browser-react 6.4 KB 0
browser-ui 810.2 KB -49.0 B (-0.0%)
coverage-istanbul 18.8 KB 0
coverage-v8 80.0 KB 0
playwright 50.0 KB 0
core/browser 939.1 KB +331.0 B (0.0%)
core/main 1.9 MB +8.7 KB (0.4%)
vscode/extension 47.8 MB 0
vscode/worker 14.8 KB 0
📋 Detailed Reports (Click to expand)

📁 browser

Path: packages/browser/.rsdoctor/rsdoctor-data.json

📌 Baseline Commit: c4b67c7274 | PR: #1615

Metric Current Baseline Change
📊 Total Size 2.1 MB 2.1 MB +159.0 B (0.0%)
📄 JavaScript 2.0 MB 2.0 MB +159.0 B (0.0%)
🎨 CSS 11.6 KB 11.6 KB 0
🌐 HTML 765.0 B 765.0 B 0
📁 Other Assets 43.2 KB 43.2 KB 0

📦 Download Diff Report: browser Bundle Diff

📁 browser-ui

Path: packages/browser-ui/.rsdoctor/rsdoctor-data.json

📌 Baseline Commit: c4b67c7274 | PR: #1615

Metric Current Baseline Change
📊 Total Size 810.2 KB 810.3 KB -49.0 B (-0.0%)
📄 JavaScript 781.9 KB 781.9 KB -49.0 B (-0.0%)
🎨 CSS 11.6 KB 11.6 KB 0
🌐 HTML 765.0 B 765.0 B 0
📁 Other Assets 16.0 KB 16.0 KB 0

📦 Download Diff Report: browser-ui Bundle Diff

📁 core/browser

Path: packages/core/.rsdoctor/browser/rsdoctor-data.json

📌 Baseline Commit: c4b67c7274 | PR: #1615

Metric Current Baseline Change
📊 Total Size 939.1 KB 938.8 KB +331.0 B (0.0%)
📄 JavaScript 798.6 KB 798.6 KB 0
🎨 CSS 0 B 0 B 0
🌐 HTML 0 B 0 B 0
📁 Other Assets 140.5 KB 140.2 KB +331.0 B (0.2%)

📦 Download Diff Report: core/browser Bundle Diff

📁 core/main

Path: packages/core/.rsdoctor/main/rsdoctor-data.json

📌 Baseline Commit: c4b67c7274 | PR: #1615

Metric Current Baseline Change
📊 Total Size 1.9 MB 1.9 MB +8.7 KB (0.4%)
📄 JavaScript 1.6 MB 1.6 MB +7.9 KB (0.5%)
🎨 CSS 0 B 0 B 0
🌐 HTML 0 B 0 B 0
📁 Other Assets 372.4 KB 371.6 KB +810.0 B (0.2%)

📦 Download Diff Report: core/main Bundle Diff

Generated by Rsdoctor GitHub Action

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

Copy link
Copy Markdown

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: 40086e4ff7

ℹ️ 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 thread packages/core/src/runtime/worker/runInPool.ts Outdated
Comment thread packages/core/src/core/plugins/mockRuntimeCode.js
- drop the stale `__rstest_dynamic_import__` global when a reused worker
  (`isolate: false`) runs a non-federation file after a federation one
- wrap a pre-existing `__webpack_require__.f` with the same chunk-handler
  guard proxy so the Module Federation node runtime cannot overwrite
  `readFileVm` / `require` in that runtime-module ordering
- remove the dead local `__rstest_dynamic_import__` binding flagged by code
  scanning: the runtime module is IIFE-isolated, so only the global is
  observable

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

Copy link
Copy Markdown

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: 510f431fba

ℹ️ 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 thread packages/core/src/core/plugins/mockRuntimeCode.js Outdated
ScriptedAlchemy pushed a commit to module-federation/core that referenced this pull request Jun 12, 2026
State that federation: true requires the unreleased @rstest/core federation
build (web-infra-dev/rstest#1407, pkg.pr.new canary), declare the
rstest:federation plugin name a stable public contract, and reword the
provenance blurb: the package is rstest federation mode's companion plugin,
not an extraction from @rstest/core.
ScriptedAlchemy pushed a commit to module-federation/core that referenced this pull request Jun 12, 2026
Install the pkg.pr.new canaries from web-infra-dev/rstest#1407 (commit
40086e4) for @rstest/core, browser, browser-react, and the rsbuild/rslib
adapters so the demos' federation: true flag exercises real rstest
federation support instead of being a silent no-op on 0.8.x. Bump
@rsbuild/core and @rsbuild/plugin-react to the 2.x line the canary uses
and document the required pairing in the demo configs.
ScriptedAlchemy added 2 commits June 12, 2026 03:24
…back

Raw absolute specifiers (`C:\...`) reach native `import()` when federated
async-node chunks call the global fallback; convert them through
`pathToFileURL` like `resolveDynamicImport.ts` does so Windows drive
letters are not parsed as URL schemes.

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

Copy link
Copy Markdown

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: bedbf1a056

ℹ️ 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 thread packages/core/src/core/plugins/mockRuntimeCode.js Outdated
The hook closure binds `import()` to the vm dynamic-import context of the
bundle that installed it, so a worker reused with `isolate: false` must not
carry the previous file's hook into the next federation-enabled file. Drop
it unconditionally in `preparePool`; `mockRuntimeCode.js` reinstalls a
fresh one bound to the current bundle.

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

Copy link
Copy Markdown

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: 773bf1f044

ℹ️ 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 thread packages/core/src/core/plugins/mockRuntimeCode.js
@github-actions

Copy link
Copy Markdown

pkg.pr.new preview 773bf1f · workflow run

  • pnpm add https://pkg.pr.new/@rstest/adapter-rsbuild@773bf1f
  • pnpm add https://pkg.pr.new/@rstest/adapter-rslib@773bf1f
  • pnpm add https://pkg.pr.new/@rstest/adapter-rspack@773bf1f
  • pnpm add https://pkg.pr.new/@rstest/browser@773bf1f
  • pnpm add https://pkg.pr.new/@rstest/browser-react@773bf1f
  • pnpm add https://pkg.pr.new/@rstest/core@773bf1f
  • pnpm add https://pkg.pr.new/@rstest/coverage-istanbul@773bf1f
  • pnpm add https://pkg.pr.new/@rstest/coverage-v8@773bf1f

Comment thread website/docs/en/config/test/federation.mdx
Comment thread examples/federation/.gitignore Outdated
Comment thread examples/federation/component-app/package.json

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

Copy link
Copy Markdown

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: 309d3b9b59

ℹ️ 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 thread packages/core/src/runtime/worker/globalSetupWorker.ts Outdated
Comment thread .github/workflows/test.yml Outdated
Comment thread examples/federation/README_zh-cn.md Outdated
Comment thread examples/federation/component-app/test/Button.ssr.test.ts
@9aoy

9aoy commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

@ScriptedAlchemy hey, the main branch now supports modifying Rstest config from an Rsbuild plugin. It hasn’t been released to npm yet, but if you’re interested, you can merge the main branch, give it a try, and help us test this feature 👀 .

https://rstest.rs/config/build/plugins#modify-rstest-config

# Conflicts:
#	packages/browser/src/hostController.ts
#	packages/core/src/core/runTests.ts
#	packages/core/src/pool/index.ts
#	packages/core/src/runtime/worker/globalSetupWorker.ts
#	pnpm-lock.yaml

@9aoy 9aoy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I found five issues in the current federation implementation. Details are included inline.

Comment thread packages/core/src/runtime/worker/runInPool.ts
Comment thread packages/core/src/core/plugins/mockRuntimeCode.js Outdated
Comment thread packages/core/src/pool/index.ts
Comment thread packages/core/src/runtime/worker/loadModule.ts Outdated
Comment thread packages/core/src/core/executorCapabilities.ts Outdated
Comment thread website/docs/zh/config/test/federation.mdx Outdated
@9aoy

9aoy commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

@ScriptedAlchemy The feature looks good to me within its current scope. One follow-up question: are there plans to support federation compatibility with ESM output (output.module: true) and in Rstest Browser Mode? Would supporting these scenarios require any additional functionality from Rstest?

@fi3ework , do you have any additional suggestions or concerns about this PR?

Also, it looks like CI is currently failing. Could you please take a look?

@fi3ework

Copy link
Copy Markdown
Member

@ScriptedAlchemy hi Zack, i recently did some refactoring on browser mode to make it more isomorphic with node mode. the last expected refactor has just been merged, so could you please resolve the conflicts again, sorry for another around of rebase.

i'm thinking that in order to better release this feature (which is a huge one) and minimize the need for frequent rebasing on your end, should we mark federation in the config as experimental jsdoc and comment out the page routes in the docs? this way, we can verify it in more repositories before making the API and documentation public to users.

overall, this would allow us to strike a better balance between development speed and quality. WDYT.

@9aoy
9aoy merged commit b33d44e into main Jul 24, 2026
12 checks passed
@9aoy
9aoy deleted the feat/federation-v2 branch July 24, 2026 03:01
@9aoy

9aoy commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator

🎉

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