File tree Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Expand file tree Collapse file tree 1 file changed +68
-0
lines changed Original file line number Diff line number Diff line change
1
+ # React 支持
2
+
3
+ ` tsdown ` 为构建 React 组件库提供了一流的支持。由于 [ Rolldown] ( https://rolldown.rs/ ) 原生支持打包 JSX/TSX 文件,你无需任何额外的插件即可开始使用。
4
+
5
+ ## 快速开始
6
+
7
+ 最快的入门方式是使用 React 组件启动模板。这个启动项目已经为 React 库开发进行了预配置,因此你可以直接专注于构建组件。
8
+
9
+ ``` bash
10
+ npx create-tsdown@latest -t react
11
+ ```
12
+
13
+ ## 最小示例
14
+
15
+ 要为 React 库配置 ` tsdown ` ,你只需使用标准的 ` tsdown.config.ts ` :
16
+
17
+ ``` ts [tsdown.config.ts]
18
+ import { defineConfig } from ' tsdown'
19
+
20
+ export default defineConfig ([
21
+ {
22
+ entry: [' ./src/index.ts' ],
23
+ platform: ' neutral' ,
24
+ dts: true ,
25
+ },
26
+ ])
27
+ ```
28
+
29
+ 创建你的典型 React 组件:
30
+
31
+ ``` tsx [MyButton.tsx]
32
+ import React from ' react'
33
+
34
+ interface MyButtonProps {
35
+ type? : ' primary'
36
+ }
37
+
38
+ export const MyButton: React .FC <MyButtonProps > = ({ type }) => {
39
+ return <button className = " my-button" >my button: type { type } </button >
40
+ }
41
+ ```
42
+
43
+ 然后在入口文件中导入它:
44
+
45
+ ``` ts [index.ts]
46
+ export { MyButton } from ' ./MyButton'
47
+ ```
48
+
49
+ ::: warning
50
+
51
+ 在 ` tsdown ` 中有两种转换 JSX/TSX 文件的方式:
52
+
53
+ - ** 经典模式(classic)**
54
+ - ** 自动模式(automatic)** (默认)
55
+
56
+ 如果你需要使用经典 JSX 转换,可以在配置文件中配置 Rolldown 的 [ ` inputOptions.jsx ` ] ( https://rolldown.rs/reference/config-options#jsx ) 选项:
57
+
58
+ ``` ts [tsdown.config.ts]
59
+ import { defineConfig } from ' tsdown'
60
+
61
+ export default defineConfig ({
62
+ inputOptions: {
63
+ jsx: ' react' , // 使用经典 JSX 转换
64
+ },
65
+ })
66
+ ```
67
+
68
+ :::
You can’t perform that action at this time.
0 commit comments