Skip to content

Commit 13db254

Browse files
authored
docs: babel migration guide (#4358)
1 parent e35e9fc commit 13db254

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed

website/docs/en/guide/migration/webpack.mdx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,57 @@ Most of the common webpack loaders and plugins can still be used in Rsbuild, but
154154

155155
Rsbuild does not support the use of Rspack's [devServer](https://rspack.dev/config/dev-server) config. Please refer to [Rspack Dev Server](/guide/basic/server#rspack-dev-server) for replacement.
156156

157+
## Babel migration
158+
159+
Rsbuild uses SWC by default, so most commonly used Babel plugins are no longer required. Here are some common Babel plugins migration examples.
160+
161+
### @babel/preset-env
162+
163+
`@babel/preset-env` is no longer required, Rsbuild will automatically downgrade code based on the [browserslist](/guide/advanced/browserslist) configuration.
164+
165+
Note that Rsbuild does not inject polyfill by default. You can refer to [Polyfill mode](/guide/advanced/browser-compatibility#polyfill-mode) for how to inject.
166+
167+
### @babel/preset-typescript
168+
169+
`@babel/preset-typescript` is no longer required, as Rsbuild enables SWC's TypeScript transformation by default.
170+
171+
### @babel/preset-react
172+
173+
`@babel/preset-react` is no longer required, replace it with [@rsbuild/plugin-react](/plugins/list/plugin-react).
174+
175+
### @babel/plugin-transform-runtime
176+
177+
`@babel/plugin-transform-runtime` is no longer required, Rsbuild has built-in equivalent `@swc/helpers` as runtime helpers.
178+
179+
### babel-plugin-import
180+
181+
`babel-plugin-import` can be replaced with the [source.transformImport](/config/source/transform-import) configuration in Rsbuild.
182+
183+
- Babel configuration:
184+
185+
```js title="babel.config.js"
186+
module.exports = {
187+
plugins: [
188+
[
189+
'import',
190+
{ libraryName: 'some-library', libraryDirectory: 'es', style: true },
191+
],
192+
],
193+
};
194+
```
195+
196+
- Rsbuild configuration:
197+
198+
```ts title="rsbuild.config.ts"
199+
export default {
200+
source: {
201+
transformImport: [
202+
{ libraryName: 'some-library', libraryDirectory: 'es', style: true },
203+
],
204+
},
205+
};
206+
```
207+
157208
## Validating results
158209

159210
After completing the above steps, you have completed the basic migration from webpack to Rsbuild. You can now run the `npm run dev` command to try starting the dev server.

website/docs/zh/guide/migration/webpack.mdx

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export default {
109109

110110
Rsbuild 提供了丰富的插件,对常见的使用场景提供开箱即用的支持,你可以参考[插件列表](/plugins/list/index)文档来了解这些插件。
111111

112-
我们以 React 项目为例,来介绍如何接入 Rsbuild 插件。首先,你可以移除一些 React 相关的构建依赖,它们已经被 Rsbuild React 插件内置,比如:
112+
我们以 React 项目为例,来介绍如何接入 Rsbuild 插件。首先,你可以移除一些 React 相关的构建依赖,它们已经被 `@rsbuild/plugin-react` 插件内置,比如:
113113

114114
- `react-refresh`
115115
- `@babel/preset-react`
@@ -154,6 +154,57 @@ export default {
154154

155155
Rsbuild 不支持使用 Rspack 的 [devServer](https://rspack.dev/config/dev-server) 配置项,请参考 [Rspack Dev Server](/guide/basic/server#rspack-dev-server) 进行替换。
156156

157+
## Babel 迁移
158+
159+
Rsbuild 默认使用 SWC 编译代码,所以大部分常用的 Babel 插件不再需要,下面是一些常见的 Babel 插件的迁移示例。
160+
161+
### @babel/preset-env
162+
163+
`@babel/preset-env` 不再需要,Rsbuild 会自动根据 [browserslist](/guide/advanced/browserslist) 配置进行代码降级。
164+
165+
注意 Rsbuild 默认不会注入 polyfill,你可以参考 [Polyfill 方案](/guide/advanced/browser-compatibility#polyfill-方案) 了解如何注入。
166+
167+
### @babel/preset-typescript
168+
169+
`@babel/preset-typescript` 不再需要,因为 Rsbuild 开启 SWC 的 TypeScript 转换。
170+
171+
### @babel/preset-react
172+
173+
`@babel/preset-react` 不再需要,替换为 [@rsbuild/plugin-react](/plugins/list/plugin-react) 即可。
174+
175+
### @babel/plugin-transform-runtime
176+
177+
`@babel/plugin-transform-runtime` 不再需要,Rsbuild 中内置了等价的 `@swc/helpers` 作为 runtime helpers。
178+
179+
### babel-plugin-import
180+
181+
`babel-plugin-import` 插件可以替换为 Rsbuild 的 [source.transformImport](/config/source/transform-import) 配置项。
182+
183+
- Babel 配置:
184+
185+
```js title="babel.config.js"
186+
module.exports = {
187+
plugins: [
188+
[
189+
'import',
190+
{ libraryName: 'some-library', libraryDirectory: 'es', style: true },
191+
],
192+
],
193+
};
194+
```
195+
196+
- Rsbuild 配置:
197+
198+
```ts title="rsbuild.config.ts"
199+
export default {
200+
source: {
201+
transformImport: [
202+
{ libraryName: 'some-library', libraryDirectory: 'es', style: true },
203+
],
204+
},
205+
};
206+
```
207+
157208
## 验证结果
158209

159210
完成以上步骤后,你已经完成了从 webpack 到 Rsbuild 的基本迁移,此时可以执行 `npm run dev` 命令来尝试启动开发服务器。

0 commit comments

Comments
 (0)