|
1 | 1 | # React |
| 2 | + |
| 3 | +In this document, you will learn how to build a React component library with Rslib. |
| 4 | + |
| 5 | +## Create React Project |
| 6 | + |
| 7 | +You can use `create-rslib` to create a project with Rslib + React. Just execute the following command: |
| 8 | + |
| 9 | +import { PackageManagerTabs } from '@theme'; |
| 10 | + |
| 11 | +<PackageManagerTabs |
| 12 | + command={{ |
| 13 | + npm: 'npm create rslib@latest', |
| 14 | + yarn: 'yarn create rslib', |
| 15 | + pnpm: 'pnpm create rslib@latest', |
| 16 | + bun: 'bun create rslib@latest', |
| 17 | + }} |
| 18 | +/> |
| 19 | + |
| 20 | +Then select `React` when prompted to "Select template". |
| 21 | + |
| 22 | +## Use Rslib in an Existing Project |
| 23 | + |
| 24 | +To compile React (JSX and TSX), you need to register the Rsbuild [React Plugin](https://rsbuild.dev/plugins/list/plugin-react). The plugin will automatically add the necessary configuration for React builds. |
| 25 | + |
| 26 | +For example, register in `rslib.config.ts`: |
| 27 | + |
| 28 | +```ts title="rslib.config.ts" {2,8} |
| 29 | +import { defineConfig } from '@rslib/core'; |
| 30 | +import { pluginReact } from '@rsbuild/plugin-react'; |
| 31 | + |
| 32 | +export default defineConfig({ |
| 33 | + lib: [ |
| 34 | + // ... |
| 35 | + ], |
| 36 | + plugins: [pluginReact(/** options here */)], |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | +## JSX Transform |
| 41 | + |
| 42 | +- **Type**: `'automatic' | 'classic'` |
| 43 | +- **Default**: `'automatic'` |
| 44 | + |
| 45 | +React introduced a [new JSX transform](https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html) in version 17. This new transform removes the need to import `React` when using JSX. |
| 46 | + |
| 47 | +By default, Rsbuild uses the new JSX transform, which is `runtime: 'automatic'`. It requires at least React `16.14.0` or higher. The peer dependency for React should be declared as above `16.14.0`. |
| 48 | + |
| 49 | +To change the JSX transform, you can pass the [swcReactOptions](https://rsbuild.dev/plugins/list/plugin-react#swcreactoptionsruntime) option to the React plugin. For example, to use the classic runtime: |
| 50 | + |
| 51 | +```ts title="rslib.config.ts" {10-12} |
| 52 | +import { pluginReact } from '@rsbuild/plugin-react'; |
| 53 | +import { defineConfig } from '@rslib/core'; |
| 54 | + |
| 55 | +export default defineConfig({ |
| 56 | + lib: [ |
| 57 | + // ... |
| 58 | + ], |
| 59 | + plugins: [ |
| 60 | + pluginReact({ |
| 61 | + swcReactOptions: { |
| 62 | + runtime: 'classic', |
| 63 | + }, |
| 64 | + }), |
| 65 | + ], |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +## JSX Import Source |
| 70 | + |
| 71 | +- **Type**: `string` |
| 72 | +- **Default**: `'react'` |
| 73 | + |
| 74 | +When `runtime` is `'automatic'`, you can specify the import path of the JSX transform through `importSource`. |
| 75 | + |
| 76 | +For example, when using [Emotion](https://emotion.sh/), you can set `importSource` to `'@emotion/react'`: |
| 77 | + |
| 78 | +```ts title="rslib.config.ts" {10-12} |
| 79 | +import { pluginReact } from '@rsbuild/plugin-react'; |
| 80 | +import { defineConfig } from '@rslib/core'; |
| 81 | + |
| 82 | +export default defineConfig({ |
| 83 | + lib: [ |
| 84 | + // ... |
| 85 | + ], |
| 86 | + plugins: [ |
| 87 | + pluginReact({ |
| 88 | + swcReactOptions: { |
| 89 | + importSource: '@emotion/react', |
| 90 | + }, |
| 91 | + }), |
| 92 | + ], |
| 93 | +}); |
| 94 | +``` |
| 95 | + |
| 96 | +{/* TODO */} |
| 97 | +{/* ## SVGR */} |
| 98 | + |
| 99 | +## Further Reading |
| 100 | + |
| 101 | +- [Rsbuild React Plugin](https://rsbuild.dev/plugins/list/plugin-react#swcreactoptionsruntime) |
| 102 | +- [SWC Compilation - jsc.transform.react](https://swc.rs/docs/configuration/compilation#jsctransformreact) |
0 commit comments