Skip to content

Commit 9d6172e

Browse files
authored
docs: improve Rspack / Rsbuild plugin guide (#2808)
1 parent 2a1df29 commit 9d6172e

File tree

4 files changed

+117
-51
lines changed

4 files changed

+117
-51
lines changed

apps/website-new/docs/en/guide/basic/rspack.mdx

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Rspack Plugin
22

33
:::info Note
4-
Requires Rspack version 0.5.0 or above
4+
Requires Rspack version 0.5.0 or above.
55
:::
66

77
- Capable of building modules that meet the `Module Federation` loading specifications.
@@ -29,15 +29,17 @@ import { PackageManagerTabs } from '@theme';
2929

3030
### Register the Plugin
3131

32-
In `Rspack`, you can add the plugin through the `plugins` configuration item in the `rspack.config.js` file:
32+
#### Rsbuild
3333

34-
```ts title='rspack.config.js'
34+
In [Rsbuild](https://rsbuild.dev/), you can add the plugin in the `rsbuild.config.ts` file:
35+
36+
```ts title="rsbuild.config.ts"
3537
import { defineConfig } from '@rsbuild/core';
3638
import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
3739

3840
export default defineConfig({
39-
// publicPath must be configured if using manifest
4041
dev: {
42+
// assetPrefix must be configured if using manifest
4143
assetPrefix: 'http://localhost:2000/',
4244
},
4345
output: {
@@ -48,23 +50,54 @@ export default defineConfig({
4850
port: 2000,
4951
},
5052
tools: {
51-
rspack: (config, { appendPlugins }) => {
52-
// You need to set a unique value that is not equal to other applications
53-
config.output!.uniqueName = 'federation_provider';
54-
appendPlugins([
53+
rspack: {
54+
output: {
55+
// You need to set a unique value that is not equal to other applications
56+
uniqueName: 'federation_provider',
57+
},
58+
plugins: [
5559
new ModuleFederationPlugin({
5660
name: 'federation_provider',
5761
exposes: {
5862
'./button': './src/button.tsx',
5963
},
6064
shared: ['react', 'react-dom'],
6165
}),
62-
]);
63-
},
66+
],
67+
}
6468
},
6569
});
6670
```
6771

72+
#### Rspack
73+
74+
In [Rspack](https://rspack.dev/), you can add the plugin in the `rspack.config.js` file:
75+
76+
```ts title="rspack.config.js"
77+
const { ModuleFederationPlugin } = require('@module-federation/enhanced/rspack');
78+
79+
module.exports = {
80+
devServer: {
81+
port: 2000,
82+
},
83+
output: {
84+
// You need to set a unique value that is not equal to other applications
85+
uniqueName: 'federation_provider',
86+
// publicPath must be configured if using manifest
87+
publicPath: 'http://localhost:2000/',
88+
},
89+
plugins: [
90+
new ModuleFederationPlugin({
91+
name: 'federation_provider',
92+
exposes: {
93+
'./button': './src/button.tsx',
94+
},
95+
shared: ['react', 'react-dom'],
96+
}),
97+
],
98+
};
99+
```
100+
68101
## Configure the Build Plugin
69102

70103
- Type: `ModuleFederationPlugin(options: ModuleFederationOptions)`

apps/website-new/docs/en/guide/start/quick-start.mdx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,36 +88,37 @@ export default function Button() {
8888

8989
> Export the Button component through Module Federation
9090
91-
```typescript
92-
// rsbuild.config.ts
91+
```typescript title="rsbuild.config.ts"
9392
import { defineConfig } from '@rsbuild/core';
9493
import { pluginReact } from '@rsbuild/plugin-react';
9594
import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
9695

9796
export default defineConfig({
97+
plugins: [pluginReact()],
9898
server: {
9999
port: 3000,
100100
},
101101
dev: {
102102
// It is necessary to configure assetPrefix, and in the production environment, you need to configure output.assetPrefix
103-
assetPrefix: 'http://localhost:3000',
103+
assetPrefix: true,
104104
},
105105
tools: {
106-
rspack: (config, { appendPlugins }) => {
107-
// You need to set a unique value that is not equal to other applications
108-
config.output!.uniqueName = 'federation_provider';
109-
appendPlugins([
106+
rspack: {
107+
output: {
108+
// You need to set a unique value that is not equal to other applications
109+
uniqueName: 'federation_provider'
110+
},
111+
plugins: [
110112
new ModuleFederationPlugin({
111113
name: 'federation_provider',
112114
exposes: {
113115
'./button': './src/button.tsx',
114116
},
115117
shared: ['react', 'react-dom'],
116118
}),
117-
]);
119+
],
118120
},
119121
},
120-
plugins: [pluginReact()],
121122
});
122123
```
123124

@@ -217,19 +218,19 @@ bun add @module-federation/enhanced
217218

218219
> Add Module Federation plugin to consume remote modules
219220

220-
```typescript
221-
// rsbuild.config.ts
221+
```typescript title="rsbuild.config.ts"
222222
import { defineConfig } from '@rsbuild/core';
223223
import { pluginReact } from '@rsbuild/plugin-react';
224224
import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
225225

226226
export default defineConfig({
227+
plugins: [pluginReact()],
227228
server: {
228229
port: 2000,
229230
},
230231
tools: {
231-
rspack: (config, { appendPlugins }) => {
232-
appendPlugins([
232+
rspack: {
233+
plugins: [
233234
new ModuleFederationPlugin({
234235
name: 'federation_consumer',
235236
remotes: {
@@ -238,10 +239,9 @@ export default defineConfig({
238239
},
239240
shared: ['react', 'react-dom'],
240241
}),
241-
]);
242+
],
242243
},
243244
},
244-
plugins: [pluginReact()],
245245
});
246246
```
247247

apps/website-new/docs/zh/guide/basic/rspack.mdx

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Rspack Plugin
22

33
:::info 注意
4-
需要 Rspack 0.5.0 及以上版本
4+
需要 Rspack 0.5.0 及以上版本
55
:::
66

77
- 能够构建出满足 `Module Federation` 加载规范的模块
@@ -29,16 +29,18 @@ import { PackageManagerTabs } from '@theme';
2929

3030
### 注册插件
3131

32-
`Rspack` 中,你可以通过 `rspack.config.js` 配置文件中的 `plugins` 配置项来添加插件:
32+
#### Rsbuild
3333

34-
```ts title='rspack.config.js'
34+
[Rsbuild](https://rsbuild.dev/) 中,你可以通过 `rsbuild.config.ts` 配置文件来添加插件:
35+
36+
```ts title='rsbuild.config.js'
3537
import { defineConfig } from '@rsbuild/core';
3638
import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
3739

3840
export default defineConfig({
39-
// 使用 manifest 必须要配置 publicPath
4041
dev: {
41-
assetPrefix: 'http://localhost:2000/',
42+
// 使用 manifest 必须要配置 assetPrefix
43+
assetPrefix: true,
4244
},
4345
output: {
4446
// 这将影响生产环境中使用的产物路径前缀
@@ -48,23 +50,54 @@ export default defineConfig({
4850
port: 2000,
4951
},
5052
tools: {
51-
rspack: (config, { appendPlugins }) => {
52-
// 需要设置一个唯一值不能和其他应用相等
53-
config.output!.uniqueName = 'federation_provider';
54-
appendPlugins([
53+
rspack: {
54+
output: {
55+
// 需要设置一个唯一值,不能和其他应用相等
56+
uniqueName: 'federation_provider',
57+
},
58+
plugins: [
5559
new ModuleFederationPlugin({
5660
name: 'federation_provider',
5761
exposes: {
5862
'./button': './src/button.tsx',
5963
},
6064
shared: ['react', 'react-dom'],
6165
}),
62-
]);
63-
},
66+
],
67+
}
6468
},
6569
});
6670
```
6771

72+
#### Rspack
73+
74+
[Rspack](https://rspack.dev/) 中,你可以通过 `rspack.config.js` 配置文件来添加插件:
75+
76+
```ts title="rspack.config.js"
77+
const { ModuleFederationPlugin } = require('@module-federation/enhanced/rspack');
78+
79+
module.exports = {
80+
devServer: {
81+
port: 2000,
82+
},
83+
output: {
84+
// 需要设置一个唯一值,不能和其他应用相等
85+
uniqueName: 'federation_provider',
86+
// 使用 manifest 必须要配置 publicPath
87+
publicPath: 'http://localhost:2000/',
88+
},
89+
plugins: [
90+
new ModuleFederationPlugin({
91+
name: 'federation_provider',
92+
exposes: {
93+
'./button': './src/button.tsx',
94+
},
95+
shared: ['react', 'react-dom'],
96+
}),
97+
],
98+
};
99+
```
100+
68101
## 配置构建插件
69102

70103
- Type: `ModuleFederationPlugin(options: ModuleFederationOptions)`

apps/website-new/docs/zh/guide/start/quick-start.mdx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,36 +85,37 @@ export default function Button() {
8585

8686
> 通过 Module Federation 导出 Button 组件
8787
88-
```typescript
89-
//rsbuild.config.ts
88+
```typescript title="rsbuild.config.ts"
9089
import { defineConfig } from '@rsbuild/core';
9190
import { pluginReact } from '@rsbuild/plugin-react';
9291
import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
9392

9493
export default defineConfig({
94+
plugins: [pluginReact()],
9595
server: {
9696
port: 3000,
9797
},
9898
dev: {
9999
// 必须要配置 assetPrefix,在生产环境需要配置 output.assetPrefix
100-
assetPrefix: 'http://localhost:3000',
100+
assetPrefix: true,
101101
},
102102
tools: {
103-
rspack: (config, { appendPlugins }) => {
104-
// 需要设置一个唯一值不能和其他应用相等
105-
config.output!.uniqueName = 'federation_provider';
106-
appendPlugins([
103+
rspack: {
104+
output: {
105+
// 需要设置一个唯一值,不能和其他应用相等
106+
uniqueName: 'federation_provider'
107+
},
108+
plugins: [
107109
new ModuleFederationPlugin({
108110
name: 'federation_provider',
109111
exposes: {
110112
'./button': './src/button.tsx',
111113
},
112114
shared: ['react', 'react-dom'],
113115
}),
114-
]);
116+
],
115117
},
116118
},
117-
plugins: [pluginReact()],
118119
});
119120
```
120121

@@ -199,19 +200,19 @@ bun add @module-federation/enhanced
199200

200201
> 增加 Module Federation 插件消费远程模块
201202
202-
```typescript
203-
// rsbuild.config.ts
203+
```typescript title="rsbuild.config.ts"
204204
import { defineConfig } from '@rsbuild/core';
205205
import { pluginReact } from '@rsbuild/plugin-react';
206206
import { ModuleFederationPlugin } from '@module-federation/enhanced/rspack';
207207

208208
export default defineConfig({
209+
plugins: [pluginReact()],
209210
server: {
210211
port: 2000,
211212
},
212213
tools: {
213-
rspack: (config, { appendPlugins }) => {
214-
appendPlugins([
214+
rspack: {
215+
plugins: [
215216
new ModuleFederationPlugin({
216217
name: 'federation_consumer',
217218
remotes: {
@@ -220,10 +221,9 @@ export default defineConfig({
220221
},
221222
shared: ['react', 'react-dom'],
222223
}),
223-
]);
224+
],
224225
},
225226
},
226-
plugins: [pluginReact()],
227227
});
228228
```
229229

0 commit comments

Comments
 (0)