refactor(webpack-bundler-runtime): compose the bundler runtime from adapters - #5137
ScriptedAlchemy wants to merge 14 commits into
Conversation
🦋 Changeset detectedLatest commit: ab1bcc0 The changes in this PR will be included in the next version bump. This PR includes changesets to release 48 packages
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 |
…into rfc5128/02-bundler-runtime-compose
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1b3250f26b
ℹ️ 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".
| const platform = federation.instance?.platform as | ||
| | Partial<NodePlatform> | ||
| | undefined; | ||
| if (!platform?.loadScriptNode) { |
There was a problem hiding this comment.
Store the initialized instance for the loader shim
When the compose entry is embedded by FederationRuntimePlugin.getTemplate, the imported federation properties are copied into a fresh __webpack_require__.federation, and the result of bundlerRuntime.init is assigned only to that fresh object. This closure still reads the original federation.instance, which therefore remains undefined; in Node composed builds, every runtime.loadScriptNode call throws the missing-platform error even when a node or universal platform capability was supplied. Assign the result of composeInit to the captured federation instance inside init, or resolve the instance from the webpack-require-backed object.
Useful? React with 👍 / 👎.
@module-federation/devtools
@module-federation/cli
create-module-federation
@module-federation/dts-plugin
@module-federation/enhanced
@module-federation/error-codes
@module-federation/esbuild
@module-federation/managers
@module-federation/manifest
@module-federation/metro
@module-federation/metro-plugin-rnc-cli
@module-federation/metro-plugin-rnef
@module-federation/metro-plugin-rock
@module-federation/modern-js
@module-federation/modern-js-v3
@module-federation/native-federation-tests
@module-federation/native-federation-typescript
@module-federation/nextjs-mf
@module-federation/node
@module-federation/observability-plugin
@module-federation/playground
@module-federation/retry-plugin
@module-federation/rsbuild-plugin
@module-federation/rspack
@module-federation/rspress-plugin
@module-federation/rstest
@module-federation/runtime
@module-federation/runtime-core
@module-federation/runtime-tools
@module-federation/sdk
@module-federation/storybook-addon
@module-federation/third-party-dts-extractor
@module-federation/treeshake-frontend
@module-federation/treeshake-server
@module-federation/typescript
@module-federation/utilities
@module-federation/webpack-bundler-runtime
@module-federation/bridge-react
@module-federation/bridge-react-webpack-plugin
@module-federation/bridge-shared
@module-federation/bridge-vue3
@module-federation/inject-external-runtime-core-plugin
commit: |
…into rfc5128/02-bundler-runtime-compose
…into rfc5128/02-bundler-runtime-compose
…declarations stay external
…ports in the boundary check
…into rfc5128/02-bundler-runtime-compose
…into rfc5128/02-bundler-runtime-compose
…into rfc5128/02-bundler-runtime-compose
…sed runtime shim Its only reader is this repo's node runtime plugin, which loads through instance.platform.loadScriptNode since PR 3. The shim keeps init because rspack native runtimes before 2.0.0-beta.1 call federation.runtime.init.
…into rfc5128/02-bundler-runtime-compose
…into rfc5128/02-bundler-runtime-compose
Part of RFC #5128 (alternate proposal to #5036).
Stack: 2 of 7. Base: #5135 (
rfc5128/01-runtime-core-kernel). Next: #5139.Landing order: #5135 → #5137 → #5139 → #5134 → #5140 → #5141 → #5142.
Companions on main: #5143 (disabled remote loadEntry), #5144 (enhanced duplicate serializer keys). #5126 (@rspack/core 2) is on main. PR 6 and 7 include it.
What changes
The webpack bundler runtime can now be composed from parts. Importers of the
@module-federation/webpack-bundler-runtimeroot see the same default-export keys, named exports, andfederation.runtimeas on main. A generated bootstrap can instead import only the adapters and runtime-core capabilities a build uses, so the parts it leaves out are not in the module graph. Enhanced and rspack generate that bootstrap in #5140 and #5141.@module-federation/webpack-bundler-runtime/composeexportscreateFederation({ buildId, capabilities, adapters })../adapters/remotes,./adapters/consumes,./adapters/share-scope, and./adapters/container. The tree-shaking share plugin moves into its own module, and the consumes adapter adds it before init.srcexcept the root imports the@module-federation/runtimeroot, theruntime-coreroot, orruntime/helpers.getRemoteEntryandgetGlobalSnapshotInfoByModuleInfonow come fromruntime-core/kernel.In a composed build,
federation.runtimeis a shim with onlyinit.initbuilds the composed instance with the build id as the default id. Rspack native runtimes before 2.0.0-beta.1 callfederation.runtime.init, so the shim needs it. The shim has noloadScriptNode. Node loads through the instance platform instead (#5139), so web builds do not import the Node loader.Verification
.d.tsbundled runtime-core types, so a consumertscfailed withTS2322. The consumertscnow reports 0 errors.compose.spec.tschecks that each adapter exposes exactly its keys, and that the composer's bundler runtime exposes onlySandinit. Init runsbeforeInitfirst and passesbuildIdas the default id. Each adapter runs against an enabled and a disabled handler. For example, consumes without the shared capability fails with the disabled-shared error.federation.runtimehas exactlyinit. An rspack 1.x style key copy followed byfederation.runtime.init(initOptions)keeps the build id. The spec copies the keys the waymoduleFederationDefaultRuntime.jsin@rspack/core1.7.9 does.legacy-root.spec.tschecks that the root's default-export keys, named exports, andfederation.runtimenamespace match origin/main. The builtdist/index.cjswas also compared by hand against the parent's.import-boundary.spec.tschecks that no source module exceptindex.tsimports the runtime or runtime-core root orruntime/helpers.tscand lint clean. Enhanced's configCases pass.9afd2096e. The gates review ran at785a04d99, and its two audit findings were fixed and reproduced as fixed at the merged head. Theruntime.initshim and the removal ofloadScriptNodefrom the shim each passed a delta review. The live headab1bcc040has the same patch over its parent.ab1bcc040with 19 checks.@rspack/core1.5.8, 1.7.9, and 2.1.10 is tested in feat(rspack): redirect the native bundler runtime to a composed entry #5141.Deviations from the RFC
federation.runtimeshim carriesinit. Rspack native runtimes before 2.0.0-beta.1 copy the federation keys and then callfederation.runtime.init, and composed builds crashed on rspack 1.5 to 1.7 without it. The RFC now listsinitin the shim.Overlaps with open PRs
This PR carries none of the spun-out main fixes and no code from other open PRs. The PRs below change files this PR also changes. This PR does not include their changes.
webpack-bundler-runtime/package.json,tsdown.config.ts,src/index.ts,src/types.ts, andsrc/init.ts. Only one of the two RFCs lands.webpack-bundler-runtime/package.json.webpack-bundler-runtime/src/types.ts.