Skip to content

Commit 45229b1

Browse files
elecmonkeyautofix-ci[bot]sxzz
authored
feat(create-tsdown): add react-compiler template (#604)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Kevin Deng <sxzz@sxzz.moe>
1 parent 40ae07e commit 45229b1

3 files changed

Lines changed: 82 additions & 11 deletions

File tree

docs/recipes/react-support.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ For the fastest way to get started, use the React component starter template. Th
1010
npx create-tsdown@latest -t react
1111
```
1212

13+
To use React Compiler, you can quickly set up a project with the dedicated template:
14+
15+
```bash
16+
npx create-tsdown@latest -t react-compiler
17+
```
18+
1319
## Minimal Example
1420

1521
To configure `tsdown` for a React library, you can just use a standard `tsdown.config.ts`:
@@ -64,3 +70,32 @@ export default defineConfig({
6470
```
6571

6672
:::
73+
74+
## Enabling React Compiler
75+
76+
React Compiler is an innovative build-time tool that automatically optimizes your React applications. React recommends that library authors use React Compiler to precompile their code for improved performance.
77+
78+
Currently, React Compiler is available only as a Babel plugin. To get started, you can either scaffold the `react-compiler` template as shown above, or integrate it manually:
79+
80+
```bash
81+
pnpm add -D @rollup/plugin-babel babel-plugin-react-compiler
82+
```
83+
84+
```ts [tsdown.config.ts]
85+
import pluginBabel from '@rollup/plugin-babel'
86+
import { defineConfig } from 'tsdown'
87+
88+
export default defineConfig({
89+
plugins: [
90+
pluginBabel({
91+
babelHelpers: 'bundled',
92+
parserOpts: {
93+
sourceType: 'module',
94+
plugins: ['jsx', 'typescript'],
95+
},
96+
plugins: ['babel-plugin-react-compiler'],
97+
extensions: ['.js', '.jsx', '.ts', '.tsx'],
98+
}),
99+
],
100+
})
101+
```

docs/zh-CN/recipes/react-support.md

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
# React 支持
22

3-
`tsdown` 为构建 React 组件库提供了一流的支持。由于 [Rolldown](https://rolldown.rs/) 原生支持打包 JSX/TSX 文件,你无需任何额外的插件即可开始使用
3+
`tsdown` 为构建 React 组件库提供一流支持。由于 [Rolldown](https://rolldown.rs/) 原生支持打包 JSX/TSX 文件,开始使用时无需任何额外插件
44

5-
## 快速开始
5+
## 快速上手
66

7-
最快的入门方式是使用 React 组件启动模板。这个启动项目已经为 React 库开发进行了预配置,因此你可以直接专注于构建组件
7+
最快的入门方式是使用 React 组件起步模板。该项目已为 React 库开发预先配置好,让您可以立即专注于组件开发
88

99
```bash
1010
npx create-tsdown@latest -t react
1111
```
1212

13-
## 最小示例
13+
如果需要使用 React Compiler,可以使用专用模板快速搭建项目:
1414

15-
要为 React 库配置 `tsdown`,你只需使用标准的 `tsdown.config.ts`
15+
```bash
16+
npx create-tsdown@latest -t react-compiler
17+
```
18+
19+
## 最简示例
20+
21+
为 React 组件库配置 `tsdown` 时,直接使用标准的 `tsdown.config.ts` 即可:
1622

1723
```ts [tsdown.config.ts]
1824
import { defineConfig } from 'tsdown'
@@ -24,7 +30,7 @@ export default defineConfig({
2430
})
2531
```
2632

27-
创建你的典型 React 组件:
33+
创建一个典型的 React 组件:
2834

2935
```tsx [MyButton.tsx]
3036
import React from 'react'
@@ -38,20 +44,20 @@ export const MyButton: React.FC<MyButtonProps> = ({ type }) => {
3844
}
3945
```
4046

41-
然后在入口文件中导入它
47+
并在入口文件中导出它
4248

4349
```ts [index.ts]
4450
export { MyButton } from './MyButton'
4551
```
4652

4753
::: warning
4854

49-
`tsdown` 中有两种转换 JSX/TSX 文件的方式
55+
`tsdown` 中有两种 JSX/TSX 转换方式
5056

51-
- **经典模式(classic)**
52-
- **自动模式(automatic**默认)
57+
- **classic(经典**
58+
- **automatic(自动,默认)**
5359

54-
如果你需要使用经典 JSX 转换,可以在配置文件中配置 Rolldown 的 [`inputOptions.jsx`](https://rolldown.rs/reference/config-options#jsx) 选项:
60+
如果需要使用经典 JSX 转换方式,可在配置文件中设置 Rolldown 的 [`inputOptions.jsx`](https://rolldown.rs/reference/config-options#jsx) 选项:
5561

5662
```ts [tsdown.config.ts]
5763
import { defineConfig } from 'tsdown'
@@ -64,3 +70,32 @@ export default defineConfig({
6470
```
6571

6672
:::
73+
74+
## 启用 React Compiler
75+
76+
React Compiler 是一种创新的构建期优化工具,可自动优化 React 应用。React 推荐库作者使用 React Compiler 预编译代码以获得更佳性能。
77+
78+
目前,React Compiler 仅作为 Babel 插件提供。您可以像上文所示脚手架 `react-compiler` 模板,或手动集成:
79+
80+
```bash
81+
pnpm add -D @rollup/plugin-babel babel-plugin-react-compiler
82+
```
83+
84+
```ts [tsdown.config.ts]
85+
import pluginBabel from '@rollup/plugin-babel'
86+
import { defineConfig } from 'tsdown'
87+
88+
export default defineConfig({
89+
plugins: [
90+
pluginBabel({
91+
babelHelpers: 'bundled',
92+
parserOpts: {
93+
sourceType: 'module',
94+
plugins: ['jsx', 'typescript'],
95+
},
96+
plugins: ['babel-plugin-react-compiler'],
97+
extensions: ['.js', '.jsx', '.ts', '.tsx'],
98+
}),
99+
],
100+
})
101+
```

packages/create-tsdown/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export const templateOptions = [
1717
{ value: 'minimal', label: 'Minimal' },
1818
{ value: 'vue', label: 'Vue' },
1919
{ value: 'react', label: 'React' },
20+
{ value: 'react-compiler', label: 'React with React Compiler' },
2021
{ value: 'solid', label: 'Solid' },
2122
{ value: 'svelte', label: 'Svelte' },
2223
] as const

0 commit comments

Comments
 (0)