Skip to content

Commit dfbf05f

Browse files
fix(rsbuild-plugin): support app SSR node target with custom environment (#4427)
Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: Zack Jackson <ScriptedAlchemy@users.noreply.github.com>
1 parent 66491bc commit dfbf05f

File tree

13 files changed

+623
-67
lines changed

13 files changed

+623
-67
lines changed

.changeset/quick-forks-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/rsbuild-plugin': patch
3+
---
4+
5+
Fix app-mode `target: 'node'` handling to respect custom `environment` names, improve missing-environment errors, auto-detect default environment names by caller/tooling when `environment` is omitted, and ensure selected node-target environments still receive federation plugin injection for commonjs-like SSR outputs.

apps/website-new/docs/en/guide/build-plugins/plugins-rsbuild.mdx

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ If you need to use the Module Federation runtime capabilities, please install [@
9292
export declare const pluginModuleFederation: (moduleFederationOptions: ModuleFederationOptions, rsbuildOptions?: RSBUILD_PLUGIN_OPTIONS) => RsbuildPlugin;
9393

9494
type RSBUILD_PLUGIN_OPTIONS = {
95+
target?: 'web' | 'node' | 'dual';
9596
ssr?: boolean;
97+
ssrDir?: string;
98+
environment?: string;
9699
}
97100
```
98101
@@ -109,7 +112,7 @@ Additional configuration for the Rsbuild plugin.
109112
110113
:::tip
111114
112-
Only supported when used as a global plugin in Rslib.
115+
`target: 'dual'` is only supported when used as a global plugin in Rslib/Rspress.
113116
114117
:::
115118
@@ -120,6 +123,46 @@ Used to specify the target runtime environment for the output. When set to `dual
120123
121124
After generating SSR output with `target: 'dual'`, you can refer to [Create a Modern.js Consumer](../../../practice/frameworks/modern/index), create a consumer, and integrate the corresponding Rslib SSR producer for development.
122125
126+
For Rsbuild app SSR, use `target: 'node'` with `environment` to apply Module Federation to a specific app environment.
127+
128+
```ts title='rsbuild.config.ts'
129+
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
130+
import { defineConfig } from '@rsbuild/core';
131+
132+
export default defineConfig({
133+
environments: {
134+
client: {},
135+
ssr: {},
136+
},
137+
plugins: [
138+
pluginModuleFederation(
139+
{
140+
name: 'host',
141+
remotes: {
142+
remote: 'remote@http://localhost:3001/mf-manifest.json',
143+
},
144+
},
145+
{
146+
target: 'node',
147+
environment: 'ssr',
148+
},
149+
),
150+
],
151+
});
152+
```
153+
154+
#### environment
155+
156+
* Type: `string`
157+
* Default: auto-detected by caller/tooling:
158+
* Rslib: `'mf'`
159+
* Rsbuild app + `target: 'web'`: `'web'`
160+
* Rsbuild app + `target: 'node'`: `'node'`
161+
* Rspress + `target: 'web'`: `'web'`
162+
* Rspress + `target: 'node'`: `'node'`
163+
164+
Environment name used by `target: 'node'` to select which environment config receives Node-target federation behavior.
165+
123166

124167
#### ssr
125168

apps/website-new/docs/zh/guide/build-plugins/plugins-rsbuild.mdx

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ export default defineConfig({
9292
export declare const pluginModuleFederation: (moduleFederationOptions: ModuleFederationOptions, rsbuildOptions?: RSBUILD_PLUGIN_OPTIONS) => RsbuildPlugin;
9393

9494
type RSBUILD_PLUGIN_OPTIONS = {
95+
target?: 'web' | 'node' | 'dual';
9596
ssr?: boolean;
97+
ssrDir?: string;
98+
environment?: string;
9699
}
97100
```
98101
@@ -108,7 +111,7 @@ Rsbuild 插件额外配置。
108111
109112
:::tip
110113
111-
仅支持 Rslib 全局插件。
114+
`target: 'dual'` 仅支持 Rslib/Rspress 全局插件。
112115
113116
:::
114117
@@ -119,6 +122,46 @@ Rsbuild 插件额外配置。
119122
120123
使用 `target: 'dual'` 生成 SSR 产物后,可参考 [创建 Modern.js 消费者](../../../practice/frameworks/modern/index) 创建消费者,并接入对应的 Rslib SSR 生产者进行开发。
121124
125+
对于 Rsbuild App 的 SSR,可使用 `target: 'node'` + `environment`,将 Module Federation 应用到指定环境。
126+
127+
```ts title='rsbuild.config.ts'
128+
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
129+
import { defineConfig } from '@rsbuild/core';
130+
131+
export default defineConfig({
132+
environments: {
133+
client: {},
134+
ssr: {},
135+
},
136+
plugins: [
137+
pluginModuleFederation(
138+
{
139+
name: 'host',
140+
remotes: {
141+
remote: 'remote@http://localhost:3001/mf-manifest.json',
142+
},
143+
},
144+
{
145+
target: 'node',
146+
environment: 'ssr',
147+
},
148+
),
149+
],
150+
});
151+
```
152+
153+
#### environment
154+
155+
* 类型:`string`
156+
* 默认值:按调用方/工具自动推断:
157+
* Rslib:`'mf'`
158+
* Rsbuild App + `target: 'web'``'web'`
159+
* Rsbuild App + `target: 'node'``'node'`
160+
* Rspress + `target: 'web'``'web'`
161+
* Rspress + `target: 'node'``'node'`
162+
163+
`target: 'node'` 下用于指定要应用 Node Federation 行为的环境名称。
164+
122165

123166
#### ssr
124167

packages/rsbuild-plugin/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,53 @@ export default defineConfig({
3434
});
3535
```
3636

37+
### Rsbuild App SSR (Node target with custom environment)
38+
39+
Use `target: 'node'` with an explicit `environment` to apply federation to a
40+
specific Rsbuild app environment (for example `ssr`).
41+
42+
```ts
43+
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
44+
import { defineConfig } from '@rsbuild/core';
45+
46+
export default defineConfig({
47+
environments: {
48+
client: {},
49+
ssr: {},
50+
},
51+
plugins: [
52+
pluginModuleFederation(
53+
{
54+
name: 'host',
55+
remotes: {
56+
remote: 'remote@http://localhost:3001/mf-manifest.json',
57+
},
58+
},
59+
{
60+
target: 'node',
61+
environment: 'ssr',
62+
},
63+
),
64+
],
65+
});
66+
```
67+
68+
`target: 'dual'` support remains scoped to Rslib/Rspress workflows.
69+
70+
### Default environment detection
71+
72+
If `environment` is omitted, the plugin will choose a default per tool:
73+
74+
- **Rslib**: `mf`
75+
- **Rsbuild app**:
76+
- `target: 'web'``web`
77+
- `target: 'node'``node`
78+
- **Rspress**:
79+
- `target: 'web'``web`
80+
- `target: 'node'``node`
81+
82+
You can still override with `environment` when your project uses custom names.
83+
3784
### Rslib Module
3885

3986
```js

0 commit comments

Comments
 (0)