@@ -5,19 +5,19 @@ title: "Basic CRA with Rsbuild"
5
5
# Basic CRA with Rsbuild
6
6
7
7
:::tip Demo Reference
8
- 在此处查看示例项目列表 : [ Rsbuild CRA] ( https://github.com/module-federation/module-federation-examples/tree/master/cra )
8
+ 点击此处查看示例项目 : [ Rsbuild CRA] ( https://github.com/module-federation/module-federation-examples/tree/master/cra )
9
9
:::
10
10
11
11
## 设置环境
12
- 在开始之前,你需要安装 [ Node.js] ( https://nodejs.org/ ) ,并确保你的 Node.js 版本 >= 16。 ** 我们建议使用 Node.js 20 的 LTS 版本. **
12
+ 在开始之前,你需要安装 [ Node.js] ( https://nodejs.org/ ) ,并确保你的 Node.js 版本 >= 16。 ** 我们建议使用 Node.js 20 的 LTS 版本。 **
13
13
14
14
你可以使用以下命令检查当前使用的 Node.js 版本:
15
15
16
16
``` bash
17
17
node -v
18
18
```
19
19
20
- 如果你当前环境没有安装Node .js,或者安装的版本太低,可以使用 [ nvm] ( https://github.com/nvm-sh/nvm ) 或 [ fnm] (https: //github.com/Schniz/fnm) 安装所需的版本。
20
+ 如果你当前环境没有安装 Node .js,或者安装的版本太低,可以使用 [ nvm] ( https://github.com/nvm-sh/nvm ) 或 [ fnm] ( https://github.com/Schniz/fnm ) 安装所需的版本。
21
21
22
22
以下是如何通过 nvm 安装 Node.js 20 LTS 版本的示例:
23
23
@@ -35,7 +35,7 @@ nvm use 20
35
35
36
36
### 创建 React 项目
37
37
38
- 你可以使用“ create-rsbuild”使用 Rsbuild + React 创建项目。 只需执行以下命令:
38
+ 你可以使用 ` create-rsbuild ` 来创建一个 Rsbuild + React 的项目, 只需执行以下命令:
39
39
40
40
import { PackageManagerTabs } from ' @theme' ;
41
41
97
97
98
98
### 在现有项目中使用 React
99
99
100
- 要编译React,你需要注册Rsbuild [ React Plugin] ( https://rsbuild.dev/plugins/list/plugin-react ) 。该插件将自动添加 React 构建所需的配置。
100
+ 要编译 React,你需要注册 Rsbuild [ React Plugin] ( https://rsbuild.dev/plugins/list/plugin-react ) 。该插件将自动添加 React 构建所需的配置。
101
101
102
- 例如,在` rsbuild.config.ts ` 中注册:
102
+ 例如,在 ` rsbuild.config.ts ` 中注册:
103
103
104
104
105
105
``` ts title="rsbuild.config.ts"
@@ -112,14 +112,14 @@ export default defineConfig({
112
112
```
113
113
114
114
:::tip
115
- 对于使用Create React App的项目 ,可以参考【CRA迁移指南】 (https://rsbuild.dev/guide/migration/cra ) 。
115
+ 对于使用 Create React App 的项目 ,可以参考 [ CRA 迁移指南 ] ( https://rsbuild.dev/guide/migration/cra ) 。
116
116
:::
117
117
118
118
通过检查 Yarn 提供的安装报告,确保 Webpack 版本 5 或更高版本已安装。
119
119
120
- ## 步骤2:更新条目文件
120
+ ## 步骤 2:更新入口文件
121
121
122
- 在这两个应用程序中,将“ index.js” 文件重命名为“ bootstrap.js” 。此更改允许异步加载“ bootstrap.js” ,这对于模块联邦在两个应用程序之间正确运行至关重要。
122
+ 在这两个应用程序中,将 ` index.js ` 文件重命名为 ` bootstrap.js ` 。此更改允许异步加载 ` bootstrap.js ` ,这对于模块联邦在两个应用程序之间正确运行至关重要。
123
123
124
124
``` bash
125
125
mv src/index.tsx src/bootstrap.tsx
@@ -140,23 +140,23 @@ root.render(
140
140
);
141
141
```
142
142
143
- 现在,在两个应用程序中创建一个新的“ index.tsx” 文件,其中包含以下内容以导入“ bootstrap.tsx” :
143
+ 现在,在两个应用程序中创建一个新的 ` index.tsx ` 文件,其中包含以下内容以导入 ` bootstrap.tsx ` :
144
144
145
145
``` typescript
146
146
import (' ./bootstrap' );
147
147
```
148
148
149
- ## 步骤 3:创建并公开
149
+ ## 步骤 3:创建并导出
150
150
151
- 现在,创建一个从“ MFE2”公开的组件
151
+ 现在,创建一个从 ` MFE2 ` 导出的组件
152
152
153
153
154
154
<Steps >
155
155
156
156
157
157
### 3.1 创建按钮组件
158
158
159
- 在“ MFE2” 中,在 src 目录中创建一个名为“ Button.tsx” 的新文件,其中包含以下内容:
159
+ 在` MFE2 ` 中,在 src 目录中创建一个名为 ` Button.tsx ` 的新文件,其中包含以下内容:
160
160
161
161
``` typescript
162
162
const Button = () => (
@@ -185,9 +185,9 @@ const App = () => {
185
185
186
186
export default App ;
187
187
```
188
- ## 3.3:在MFE2中配置Rsbuild
188
+ ## 3.3:在 MFE2 中配置 Rsbuild
189
189
190
- 在` MFE2 ` 根目录下创建` rsbuild.config.ts ` 文件,配置如下:
190
+ 在 ` MFE2 ` 根目录下创建 ` rsbuild.config.ts ` 文件,配置如下:
191
191
192
192
``` ts title="rsbuild.config.ts"
193
193
import { defineConfig } from ' @rsbuild/core' ;
@@ -224,12 +224,12 @@ export default defineConfig({
224
224
</Steps >
225
225
## 步骤 4:使用远程模块
226
226
227
- 在“ MFE1” 中使用“ MFE2” 公开的模块
227
+ 在 ` MFE1 ` 中使用 ` MFE2 ` 公开的模块
228
228
229
229
<Steps >
230
230
### 4.1 更新 App.tsx
231
231
232
- 更新“ MFE1”中的“ App.tsx” 以导入并渲染“ MFE2” 按钮组件:
232
+ 更新 ` MFE1 ` 中的 ` App.tsx ` 以导入并渲染 ` MFE2 ` 按钮组件:
233
233
234
234
``` typescript
235
235
import React from ' react' ;
@@ -249,7 +249,7 @@ export default App;
249
249
250
250
### 4.2:在 MFE1 中配置 Rsbuild
251
251
252
- 在“ MFE1” 根目录中创建“ rsbuild.config.ts” 文件,配置如下:
252
+ 在 ` MFE1 ` 根目录中创建 ` rsbuild.config.ts ` 文件,配置如下:
253
253
254
254
``` ts title="rsbuild.config.ts"
255
255
import { defineConfig } from ' @rsbuild/core' ;
@@ -284,6 +284,6 @@ export default defineConfig({
284
284
```
285
285
</Steps >
286
286
287
- 此设置会在“ MFE1” 内启动模块联邦,并且在启动开发服务器后,可以通过“ http://localhost:3001” 进行访问。
287
+ 此设置会在 ` MFE1 ` 内启动模块联邦,并且在启动开发服务器后,可以通过 ` http://localhost:3001 ` 进行访问。
288
288
289
- 类似地,配置激活“ MFE2” 的模块联邦,从而在“ http://localhost:3002/remoteEntry.js”处公开“ Button” 组件。随着开发服务器的运行,可以通过“ http://localhost:3002” 进行访问。
289
+ 类似地,配置激活 ` MFE2 ` 的模块联邦,从而可通过 ` http://localhost:3002/remoteEntry.js ` 加载 ` Button ` 组件。随着开发服务器的运行,可以通过 ` http://localhost:3002 ` 进行访问。
0 commit comments