-
-
Notifications
You must be signed in to change notification settings - Fork 57
docs(solution): add react and node.js #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| ["react"] | ||
| ["nodejs", "react"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Node.js | ||
|
|
||
| In this document, you will learn how to build a Node.js library using Rslib. | ||
|
|
||
| ## Create Node.js Project | ||
|
|
||
| You can use `create-rslib` to create a project with Rslib + Node.js. Just execute the following command: | ||
|
|
||
| import { PackageManagerTabs } from '@theme'; | ||
|
|
||
| <PackageManagerTabs | ||
| command={{ | ||
| npm: 'npm create rslib@latest', | ||
| yarn: 'yarn create rslib', | ||
| pnpm: 'pnpm create rslib@latest', | ||
| bun: 'bun create rslib@latest', | ||
| }} | ||
| /> | ||
|
|
||
| Then select `Node.js` when prompted to "Select template". | ||
|
|
||
| ## Use Rslib in an existing project | ||
|
|
||
| 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. | ||
|
|
||
| For example, in `rsbuild.config.ts`: | ||
|
|
||
| ```ts title="rslib.config.ts" | ||
| import { defineConfig } from '@rslib/core'; | ||
|
|
||
| export default defineConfig({ | ||
| lib: [ | ||
| { | ||
| format: 'esm', | ||
| output: { | ||
| distPath: { | ||
| root: './dist/esm', | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| format: 'cjs', | ||
| output: { | ||
| distPath: { | ||
| root: './dist/cjs', | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| output: { | ||
| target: 'node', | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## Target for Node.js | ||
|
|
||
| When [target](/config/rsbuild/output#target) is set to `"node"`, Rslib will automatically adjust the following configurations: | ||
|
|
||
| ### Externals | ||
|
|
||
| All Node.js [built-in modules](https://nodejs.org/docs/latest/api/) are externalized by default. | ||
|
|
||
| ### Shims | ||
|
|
||
| - `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. | ||
| - `__filename`: When outputting in ESM format, replace `__filename` with the result of `fileURLToPath(import.meta.url)`. | ||
| - `__dirname`: When outputting in ESM format, replace `__dirname` with the result of `dirname(fileURLToPath(import.meta.url))`. | ||
|
|
||
| {/* TODO: Rspack doesn't support createRequire now */} | ||
| {/* ### createRequire */} | ||
| {/* Requiring module with [createRequire](https://nodejs.org/api/module.html#modulecreaterequirefilename) will also works in ESM format. */} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,102 @@ | ||
| # React | ||
|
|
||
| In this document, you will learn how to build a React component library with Rslib. | ||
|
|
||
| ## Create React Project | ||
|
|
||
| You can use `create-rslib` to create a project with Rslib + React. Just execute the following command: | ||
|
|
||
| import { PackageManagerTabs } from '@theme'; | ||
|
|
||
| <PackageManagerTabs | ||
| command={{ | ||
| npm: 'npm create rslib@latest', | ||
| yarn: 'yarn create rslib', | ||
| pnpm: 'pnpm create rslib@latest', | ||
| bun: 'bun create rslib@latest', | ||
| }} | ||
| /> | ||
|
|
||
| Then select `React` when prompted to "Select template". | ||
|
|
||
| ## Use Rslib in an Existing Project | ||
|
|
||
| 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. | ||
|
|
||
| For example, register in `rslib.config.ts`: | ||
|
|
||
| ```ts title="rslib.config.ts" {2,8} | ||
| import { defineConfig } from '@rslib/core'; | ||
| import { pluginReact } from '@rsbuild/plugin-react'; | ||
|
|
||
| export default defineConfig({ | ||
| lib: [ | ||
| // ... | ||
| ], | ||
| plugins: [pluginReact(/** options here */)], | ||
| }); | ||
| ``` | ||
|
|
||
| ## JSX Transform | ||
|
|
||
| - **Type**: `'automatic' | 'classic'` | ||
| - **Default**: `'automatic'` | ||
|
|
||
| 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. | ||
|
|
||
| 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`. | ||
|
|
||
| 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: | ||
|
|
||
| ```ts title="rslib.config.ts" {10-12} | ||
| import { pluginReact } from '@rsbuild/plugin-react'; | ||
| import { defineConfig } from '@rslib/core'; | ||
|
|
||
| export default defineConfig({ | ||
| lib: [ | ||
| // ... | ||
| ], | ||
| plugins: [ | ||
| pluginReact({ | ||
| swcReactOptions: { | ||
| runtime: 'classic', | ||
| }, | ||
| }), | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| ## JSX Import Source | ||
|
|
||
| - **Type**: `string` | ||
| - **Default**: `'react'` | ||
|
|
||
| When `runtime` is `'automatic'`, you can specify the import path of the JSX transform through `importSource`. | ||
|
|
||
| For example, when using [Emotion](https://emotion.sh/), you can set `importSource` to `'@emotion/react'`: | ||
|
|
||
| ```ts title="rslib.config.ts" {10-12} | ||
| import { pluginReact } from '@rsbuild/plugin-react'; | ||
| import { defineConfig } from '@rslib/core'; | ||
|
|
||
| export default defineConfig({ | ||
| lib: [ | ||
| // ... | ||
| ], | ||
| plugins: [ | ||
| pluginReact({ | ||
| swcReactOptions: { | ||
| importSource: '@emotion/react', | ||
| }, | ||
| }), | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| {/* TODO */} | ||
| {/* ## SVGR */} | ||
|
|
||
| ## Further Reading | ||
|
|
||
| - [Rsbuild React Plugin](https://rsbuild.dev/plugins/list/plugin-react#swcreactoptionsruntime) | ||
| - [SWC Compilation - jsc.transform.react](https://swc.rs/docs/configuration/compilation#jsctransformreact) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.