Skip to content

Commit

Permalink
docs: add configure SWC guide (#3119)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Aug 3, 2024
1 parent b2d4a23 commit c26114e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 134 deletions.
68 changes: 1 addition & 67 deletions website/docs/en/config/tools/swc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const defaultOptions = {

You can set the options of [builtin:swc-loader](https://rspack.dev/guide/features/builtin-swc-loader) through `tools.swc`.

:::tip
Rsbuild uses `builtin:swc-loader` by default to transform JavaScript code. It is the Rust version of [swc-loader](https://github.com/swc-project/pkgs/tree/main/packages/swc-loader), and its options align with the JS version of swc-loader.
:::
> Refer to [Configure SWC](/guide/basic/configure-swc) for more details.
## Object Type

Expand Down Expand Up @@ -61,67 +59,3 @@ export default {
:::tip
The object returned by the `tools.swc` function will be used directly as the final `builtin:swc-loader` option, and will not be merged with the built-in `builtin:swc-loader` option anymore.
:::

## Example

### Register SWC Plugin

`tools.swc` can be used to register SWC's Wasm plugins, for example, registering `@swc/plugin-styled-jsx`:

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-styled-jsx', {}]],
},
},
},
},
};
```

Please note that SWC's wasm plugins are currently not backward compatible, and there is a certain binding relationship between the SWC plugin and the version of `@swc/core` that Rspack depends on. Therefore, you must select the SWC plugin that matches the current version of `@swc/core` for SWC to work.

For more information, refer to [SWC - Selecting the version](https://swc.rs/docs/plugin/selecting-swc-core).

### Enable Emotion Support

Example of enabling the Emotion support using the `builtin:swc-loader`:

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-emotion', {}]],
},
},
},
},
};
```

For more options, please refer to [@swc/plugin-emotion](https://www.npmjs.com/package/@swc/plugin-emotion).

### Enable Relay Support

Example of enabling the Relay support using the `builtin:swc-loader`:

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-relay', {}]],
},
},
},
},
};
```

For more options, please refer to [@swc/plugin-relay](https://www.npmjs.com/package/@swc/plugin-relay).
1 change: 1 addition & 0 deletions website/docs/en/guide/basic/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"wasm-assets",
"configure-rspack",
"configure-rsbuild",
"configure-swc",
"html-template",
"css-modules",
"typescript",
Expand Down
73 changes: 73 additions & 0 deletions website/docs/en/guide/basic/configure-swc.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Configure SWC

[SWC](https://github.com/swc-project/swc) (Speedy Web Compiler) is a transformer and minimizer for JavaScript and TypeScript based on Rust. SWC provides similar functionality to Babel and Terser, and it is 20x faster than Babel on a single thread and 70x faster on four cores.

Rsbuild uses Rspack's [builtin:swc-loader](https://rspack.dev/guide/features/builtin-swc-loader) by default to transform JavaScript and TypeScript code. It is the Rust version of [swc-loader](https://github.com/swc-project/pkgs/tree/main/packages/swc-loader), and its options align with the JS version of swc-loader.

## Configure swc-loader

Rsbuild provides the [tools.swc](/config/tools/swc) option to configure `builtin:swc-loader`. Here are some examples:

### Register SWC Plugin

`tools.swc` can be used to register SWC's Wasm plugins, for example, registering `@swc/plugin-styled-jsx`:

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-styled-jsx', {}]],
},
},
},
},
};
```

Please note that SWC's wasm plugins are currently not backward compatible, and there is a certain binding relationship between the SWC plugin and the version of `@swc/core` that Rspack depends on. Therefore, you must select the SWC plugin that matches the current version of `@swc/core` for SWC to work.

For more information, refer to [SWC - Selecting the version](https://swc.rs/docs/plugin/selecting-swc-core).

> You can check out the [awesome-swc](https://github.com/swc-contrib/awesome-swc) repository to see the SWC plugins available in the community.
### Enable Emotion Support

Example of enabling the Emotion support using the `builtin:swc-loader`:

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-emotion', {}]],
},
},
},
},
};
```

For more options, please refer to [@swc/plugin-emotion](https://www.npmjs.com/package/@swc/plugin-emotion).

### Enable Relay Support

Example of enabling the Relay support using the `builtin:swc-loader`:

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-relay', {}]],
},
},
},
},
};
```

For more options, please refer to [@swc/plugin-relay](https://www.npmjs.com/package/@swc/plugin-relay).
68 changes: 1 addition & 67 deletions website/docs/zh/config/tools/swc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ const defaultOptions = {

通过 `tools.swc` 可以设置 [builtin:swc-loader](https://rspack.dev/guide/features/builtin-swc-loader) 的选项。

:::tip
Rsbuild 默认使用 `builtin:swc-loader` 来转换 JavaScript 代码,它是 [swc-loader](https://github.com/swc-project/pkgs/tree/main/packages/swc-loader) 的 Rust 版本,选项与 JS 版本的 swc-loader 保持对齐。
:::
> 参考 [配置 SWC](/guide/basic/configure-swc) 了解更多。
## Object 类型

Expand Down Expand Up @@ -61,67 +59,3 @@ export default {
:::tip
`tools.swc` 函数返回的对象会直接作为最终使用的 `builtin:swc-loader` 选项,不会再与内置的 `builtin:swc-loader` 选项进行合并。
:::

## 示例

### 注册 SWC 插件

`tools.swc` 可以用于注册 SWC 的 Wasm 插件,比如注册 `@swc/plugin-styled-jsx`

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-styled-jsx', {}]],
},
},
},
},
};
```

注意,目前 SWC 的 Wasm 插件不向后兼容,SWC 插件和 Rspack 依赖的 `@swc/core` 的版本存在一定的绑定关系。因此,你需要选择和当前 `@swc/core` 版本匹配的 SWC 插件,才能使 SWC 正常执行。

更多信息可参考 [SWC - Selecting the version](https://swc.rs/docs/plugin/selecting-swc-core)

### 启用 Emotion 支持

启用 `builtin:swc-loader` 对 Emotion 支持的示例:

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-emotion', {}]],
},
},
},
},
};
```

更多选项可参考 [@swc/plugin-emotion](https://www.npmjs.com/package/@swc/plugin-emotion)

### 启用 Relay 支持

启用 `builtin:swc-loader` 对 Relay 支持的示例:

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-relay', {}]],
},
},
},
},
};
```

更多选项可参考 [@swc/plugin-relay](https://www.npmjs.com/package/@swc/plugin-relay)
1 change: 1 addition & 0 deletions website/docs/zh/guide/basic/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"wasm-assets",
"configure-rspack",
"configure-rsbuild",
"configure-swc",
"html-template",
"css-modules",
"typescript",
Expand Down
73 changes: 73 additions & 0 deletions website/docs/zh/guide/basic/configure-swc.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 配置 SWC

[SWC](https://github.com/swc-project/swc)(Speedy Web Compiler)是基于 Rust 语言编写的高性能 JavaScript 和 TypeScript 转译和压缩工具。SWC 提供与 Babel 和 Terser 相似的能力,在单线程上它比 Babel 快 20 倍,在四核上它比 Babel 快 70 倍。

Rsbuild 默认使用 Rspack 的 [builtin:swc-loader](https://rspack.dev/guide/features/builtin-swc-loader) 来转换 JavaScript 和 TypeScript 代码,它是 [swc-loader](https://github.com/swc-project/pkgs/tree/main/packages/swc-loader) 的 Rust 版本,选项与 JS 版本的 swc-loader 保持对齐。

## 配置 swc-loader

Rsbuild 提供了 [tools.swc](/config/tools/swc) 选项来配置 `builtin:swc-loader`,下面是一些示例:

### 注册 SWC 插件

`tools.swc` 可以用于注册 SWC 的 Wasm 插件,比如注册 `@swc/plugin-styled-jsx`

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-styled-jsx', {}]],
},
},
},
},
};
```

注意,目前 SWC 的 Wasm 插件不向后兼容,SWC 插件和 Rspack 依赖的 `@swc/core` 的版本存在一定的绑定关系。因此,你需要选择和当前 `@swc/core` 版本匹配的 SWC 插件,才能使 SWC 正常执行。

更多信息可参考 [SWC - Selecting the version](https://swc.rs/docs/plugin/selecting-swc-core)

> 你可以通过 [awesome-swc](https://github.com/swc-contrib/awesome-swc) 仓库查看社区中可用的 SWC 插件。
### 启用 Emotion 支持

启用 `builtin:swc-loader` 对 Emotion 支持的示例:

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-emotion', {}]],
},
},
},
},
};
```

更多选项可参考 [@swc/plugin-emotion](https://www.npmjs.com/package/@swc/plugin-emotion)

### 启用 Relay 支持

启用 `builtin:swc-loader` 对 Relay 支持的示例:

```js
export default {
tools: {
swc: {
jsc: {
experimental: {
plugins: [['@swc/plugin-relay', {}]],
},
},
},
},
};
```

更多选项可参考 [@swc/plugin-relay](https://www.npmjs.com/package/@swc/plugin-relay)

0 comments on commit c26114e

Please sign in to comment.