Skip to content

Commit b9bdc6e

Browse files
committed
docs(solution): add react and node.js
1 parent 8f65812 commit b9bdc6e

File tree

7 files changed

+177
-4
lines changed

7 files changed

+177
-4
lines changed

packages/create-rslib/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async function getTemplateName({ template }: Argv) {
2727

2828
const templateName = checkCancel<TemplateName>(
2929
await select({
30-
message: 'Select templates',
30+
message: 'Select template',
3131
options: [
3232
{ value: 'node-dual', label: 'Node.js dual ESM/CJS package' },
3333
{ value: 'node-esm', label: 'Node.js pure ESM package' },

website/docs/en/guide/basic/upgrade-rslib.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ This section explains how to upgrade the project's Rslib dependencies to the lat
99
Rslib is still in the early stages of development, and the API may change frequently. We recommend upgrading to the latest version to access new features and bug fixes. We plan to release version 0.1.0 in the fourth quarter of 2024.
1010
:::
1111

12+
When `bundle` is set to `false`, the entry should be set a glob pattern to include all the files. You can check out [glob patterns](https://github.com/micromatch/picomatch#globbing-features) here.
13+
1214
## Using Taze
1315

1416
We recommend using [Taze](https://github.com/antfu-collective/taze) to upgrade the Rslib version. Taze is a CLI tool for updating npm dependencies.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
["react"]
1+
["nodejs", "react"]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Node.js
2+
3+
In this document, you will learn how to build a Node.js library using Rslib.
4+
5+
## Create Node.js Project
6+
7+
You can use `create-rslib` to create a project with Rslib + Node.js. 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 `Node.js` when prompted to "Select template".
21+
22+
## Use Rslib in an existing project
23+
24+
To development an Node.js library, you need to set [target](/config/rsbuild/output#target) to `"node"` in `rslib.config.ts`. This is crucial because when the `target` is set to `"node"`, Rslib automatically adjusts many configurations for Node.js. For example, [output.externals](/config/rsbuild/output#target) will exclude built-in Node.js modules, and [shims](/config/lib/shims) will add a shim for `import.meta.url` in CJS output by default.
25+
26+
For example, in `rsbuild.config.ts`:
27+
28+
```ts title="rslib.config.ts"
29+
import { defineConfig } from '@rslib/core';
30+
31+
export default defineConfig({
32+
lib: [
33+
{
34+
format: 'esm',
35+
output: {
36+
distPath: {
37+
root: './dist/esm',
38+
},
39+
},
40+
},
41+
{
42+
format: 'cjs',
43+
output: {
44+
distPath: {
45+
root: './dist/cjs',
46+
},
47+
},
48+
},
49+
],
50+
output: {
51+
target: 'node',
52+
},
53+
});
54+
```
55+
56+
## Target for Node.js
57+
58+
When [target](/config/rsbuild/output#target) is set to `"node"`, Rslib will automatically adjust the following configurations:
59+
60+
### Externals
61+
62+
All Node.js [built-in modules](https://nodejs.org/docs/latest/api/) are externalized by default.
63+
64+
### Shims
65+
66+
- `global`: leave it as it is, while it's recommended to use [globalThis](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis) instead.
67+
- `__filename`: When outputting in ESM format, replace `__filename` with the result of `fileURLToPath(import.meta.url)`.
68+
- `__dirname`: When outputting in ESM format, replace `__dirname` with the result of `dirname(fileURLToPath(import.meta.url))`.
69+
70+
{/* TODO: Rspack doesn't support createRequire now */}
71+
{/* ### createRequire */}
72+
{/* Requiring module with [createRequire](https://nodejs.org/api/module.html#modulecreaterequirefilename) will also works in ESM format. */}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,102 @@
11
# 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)

website/docs/en/guide/solution/typescript.mdx

Lines changed: 0 additions & 1 deletion
This file was deleted.

website/docs/en/guide/solution/vue.mdx

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)