Skip to content

Commit 9e5dd24

Browse files
committed
docs: solution
1 parent 45fb04c commit 9e5dd24

File tree

6 files changed

+210
-6
lines changed

6 files changed

+210
-6
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ In this chapter, we will introduce how to use Rslib to development libraries for
44

55
## Browser Target
66

7-
Rslib outputs code for the browser by default, so no additional configuration is necessary to get started.
8-
97
When developing a library that runs in the browser, you can package it in both [ESM](/guide/basic/output-format#esm--cjs) and [CJS](/guide/basic/output-format#esm--cjs) formats for integration with application bundlers. Configuring the package [conditional exports](https://nodejs.org/api/packages.html#conditional-exports) to ESM output allows for better tree shaking. Additionally, you can create [UMD](/guide/basic/output-format#umd) format output for direct browser use and even generate [Module Federation ](/guide/advanced/module-federation) formats for dynamic loading by other applications. Configure [Browserslist](https://rsbuild.dev/guide/advanced/browserslist) according to the target browser support to determine the downgrade syntax of the output, or adding [polyfill](/guide/advanced/output-compatibility) for API compatibility.
108

119
When publishing to npm, you can choose not to [minify](/config/rsbuild/output#outputminify) your code or to minify it while providing a [sourcemap](/config/rsbuild/output#outputsourcemap) to enhance the debugging experience for users of your library. For styling, you can use [CSS](/guide/advanced/css), or [CSS pre-processors](/guide/advanced/css#preprocessors) like Sass, Less, or Stylus, or apply [PostCSS](/guide/advanced/css#postcss) for CSS post-processing. Tools like [Tailwind CSS](/guide/advanced/css#tailwind-css) can also help in building your styles. Using [CSS modules](/guide/advanced/css#css-modules) to create CSS modules is another option.
@@ -21,7 +19,7 @@ Refer to the solutions in this chapter to learn how to use Rslib to develop brow
2119

2220
By setting set [target](/config/rsbuild/output#outputtarget) to `"node"` to development libraries for Node.js.
2321

24-
You can create a [pure ESM](/guide/basic/output-format#esm--cjs) package or a [dual package](/guide/basic/output-format#esm--cjs) that supports both ESM and CJS as needed. In CJS output, `import.meta.url` will be automatically [shimmed](/config/lib/shims) for compatibility and `__dirname` and `__filename` got an optional ESM shims to ensure proper use across different module system. Node.js's built-in packages will be [externalized by default](/guide/advanced/third-party-deps), simplifying the configuration.
22+
You can create a [pure ESM](/guide/basic/output-format#esm--cjs) package or a [dual package](/guide/basic/output-format#esm--cjs) that supports both ESM and CJS as needed. In CJS output, `import.meta.url` will be automatically [shimmed](/config/lib/shims) for compatibility and `__dirname` and `__filename` got optional ESM shims to ensure proper use across different module system. Node.js's built-in packages will be [externalized by default](/guide/advanced/third-party-deps).
2523

2624
{/* TODO: Clarify default behavior */}
2725
{/* ### Default Behavior */}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default defineConfig({
5454

5555
Rslib set [target](/config/rsbuild/output#outputtarget) to `"node"` by default, which is different from the default target of Rsbuild.
5656

57-
Rslib adjusts many configurations for Node.js. For example, [output.externals](/config/rsbuild/output#outputtarget) 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.
57+
When target is valued as `"node"`, Rslib adjusts many configurations for Node.js. For example, [output.externals](/config/rsbuild/output#outputtarget) 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.
5858

5959
### Externals
6060

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default defineConfig({
4949

5050
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.
5151

52-
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`.
52+
By default, Rsbuild uses the new JSX transform, which is `runtime: 'automatic'`. It requires at least React `16.14.0` or higher. The `peerDependencies` should be declared as `"react": ">=16.14.0"`.
5353

5454
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:
5555

@@ -79,7 +79,7 @@ export default defineConfig({
7979
- **Type**: `string`
8080
- **Default**: `'react'`
8181

82-
When `runtime` is `'automatic'`, you can specify the import path of the JSX transform through `importSource`.
82+
When `runtime` is valued as `'automatic'`, you can specify the import path of the JSX transform through `importSource`.
8383

8484
For example, when using [Emotion](https://emotion.sh/), you can set `importSource` to `'@emotion/react'`:
8585

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
11
# 解决方案
2+
3+
在本章中,我们将介绍如何使用 Rslib 开发浏览器和 Node.js 的库。我们还将介绍如何为不同的 UI 框架创建库。
4+
5+
## Browser Target
6+
7+
开发在浏览器中运行的库时,可以将其打包为 [ESM](/guide/basic/output-format#esm--cjs)[CJS](/guide/basic/output-format#esm--cjs) 格式,用于与 app 的 bundler 集成。将包 [conditional-exports](https://nodejs.org/api/packages.html#conditional-exports) 配置为 ESM 输出可以更好地进行树摇动。此外,您可以创建 [UMD](/guide/basic/output-format#umd) 格式输出以供浏览器直接使用,甚至可以生成 [Module Federation](/guide/advanced/module-federation) 格式以供其他应用程序动态加载。根据目标浏览器支持配置[Browserslist](https://rsbuild.dev/guide/advanced/browserslist)以确定输出的降级语法,或添加[polyfill](/guide/advanced/output-compatibility) API 兼容性。
8+
9+
发布到 npm 时,你可以选择不压缩代码或[压缩代码](/config/rsbuild/output#outputminify),同时提供[sourcemap](/config/rsbuild/output#outputsourcema) 以增强库使用者的调试体验。样式上,可以使用 [CSS](/guide/advanced/css)[CSS 预处理器](/guide/advanced/css#preprocessors),如 Sass、Less 或 Stylus 等,或使用 [PostCSS](/guide/advanced/css#postcss) 用于 CSS 后处理。 [Tailwind CSS](/guide/advanced/css#tailwind-css) 等工具也可以帮助您构建样式。或使用 [CSS 模块](/guide/advanced/css#css-modules)
10+
11+
在资源管理方面,Rslib 处理代码中使用的[静态资源](/guide/advanced/assets),例如 SVG 和 PNG 文件。您还可以构建[React](/guide/solution/react)[Preact](https://github.com/web-infra-dev/rslib/tree/main/examples/preact-component-的组件库Bundle-false) 或其他框架,使用 [Storybook](/guide/advanced/storybook) 进行 UI 组件开发和测试。
12+
13+
参考本章的解决方案,了解如何使用 Rslib 开发一个在浏览器中使用的库,并。
14+
15+
{/* TODO: Clarify default behavior */}
16+
{/* ### Default Behavior */}
17+
18+
## Node.js Target
19+
20+
通过设置 [target](/config/rsbuild/output#outputtarget)`"node"` 来开发 Node.js 中使用的库。
21+
22+
你可以创建一个 [pure ESM](/guide/basic/output-format#esm--cjs) 包或者 [dual package](/guide/basic/output-format#esm--cjs) 同时按需支持 ESM 和 CJS。在 CJS 输出中,由于兼容性 `import.meta.url` 会被自动 [shimmed](/config/lib/shims),同时 `__dirname``__filename` 有可选的 ESM shims,以确保在不同的模块系统中正确使用。Node.js 中的 built-in packages 会被 [默认 externalized](/guide/advanced/third-party-deps)
23+
24+
{/* TODO: Clarify default behavior */}
25+
{/* ### Default Behavior */}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,71 @@
11
# Node.js
2+
3+
在本文档中,您将学习如何使用 Rslib 构建 Node.js 库。
4+
5+
## Create Node.js Project
6+
7+
您可以使用 `create-rslib` 使用 Rslib + Node.js 创建项目。只需执行以下命令:
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+
然后在提示 "Select template" 时选择 `Node.js`
21+
22+
## 在现有项目中使用 Rslib
23+
24+
Rslib 为 Node.js 项目提供无缝支持,允许你以最少的配置轻松构建 Node.js 项目。
25+
26+
例如, 在 `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+
});
51+
```
52+
53+
## Node.js 中的 target
54+
55+
Rslib 默认将 [target](/config/rsbuild/output#outputtarget) 设置为 `"node"`,这与 Rsbuild 的target 默认值不同。
56+
57+
当 target 为 `"node"`,Rslib 会为 Node.js 调整一些配置。例如,[output.externals](/config/rsbuild/output#outputtarget) 将排除内置 Node.js 模块,而 [shims](/config/lib/shims) 将在 CJS 产物中为 `import.meta.url` 添加 shim。
58+
59+
### Externals
60+
61+
所有 Node.js [built-in modules](https://nodejs.org/docs/latest/api/) 会默认被 external。
62+
63+
### Shims
64+
65+
- `global`: 保持原样,建议使用 [globalThis](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis) 替代.
66+
- `__filename`: 当以 ESM 格式输出时,替换 `__filename``fileURLToPath(import.meta.url)`.
67+
- `__dirname`: 当以 ESM 格式输出时,替换 `__dirname``dirname(fileURLToPath(import.meta.url))`.
68+
69+
{/* TODO: Rspack doesn't support createRequire now */}
70+
{/* ### createRequire */}
71+
{/* Requiring module with [createRequire](https://nodejs.org/api/module.html#modulecreaterequirefilename) will also works in ESM format. */}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,113 @@
11
# React
2+
3+
在本文档中,您将学习如何使用 Rslib 构建 React 组件库。
4+
5+
## 创建 React 项目
6+
7+
你可以使用 "create-rslib" 创建 Rslib + React 项目。只需执行以下命令:
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+
然后,当提示 "Select template" 选择 `React`
21+
22+
## 在现有项目中使用 Rslib
23+
24+
开发 React 组件,需要设置 [target](/config/rsbuild/output#outputtarget)`"web"``rslib.config.ts` 中。 这一点至关重要,因为 Rslib 默认将 `target` 设置为 `"node"`,这与 Rsbuild 的 target 默认值不同。
25+
26+
要编译 React(JSX 和 TSX),您需要注册 Rsbuild [React 插件](https://rsbuild.dev/plugins/list/plugin-react)。该插件将自动添加 React 构建所需的配置。
27+
28+
例如,在 `rslib.config.ts` 中注册:
29+
30+
```ts title="rslib.config.ts" {2,8-11}
31+
import { defineConfig } from '@rslib/core';
32+
import { pluginReact } from '@rsbuild/plugin-react';
33+
34+
export default defineConfig({
35+
lib: [
36+
// ...
37+
],
38+
output: {
39+
target: 'web',
40+
},
41+
plugins: [pluginReact(/** options here */)],
42+
});
43+
```
44+
45+
## JSX Transform
46+
47+
- **类型**: `'automatic' | 'classic'`
48+
- **默认值**: `'automatic'`
49+
50+
React 引入了一个 [新的 JSX transform](https://legacy.reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html) 在版本 17 中。这个新的 transform 在使用 JSX 时无需导入 `React`
51+
52+
默认情况下,Rsbuild 使用新的 JSX 转换,即“ `runtime: 'automatic'`。需要 React `16.14.0` 或更高版本。 `peerDependencies` 中应声明 `"react": ">=16.14.0"`
53+
54+
要更改 JSX transform,可以传递 [swcReactOptions](https://rsbuild.dev/plugins/list/plugin-react#swcreactoptionsruntime) 给 React plugin. 比如要使用 classic runtime 时:
55+
56+
```ts title="rslib.config.ts" {13-15}
57+
import { pluginReact } from '@rsbuild/plugin-react';
58+
import { defineConfig } from '@rslib/core';
59+
60+
export default defineConfig({
61+
lib: [
62+
// ...
63+
],
64+
output: {
65+
target: 'web',
66+
},
67+
plugins: [
68+
pluginReact({
69+
swcReactOptions: {
70+
runtime: 'classic',
71+
},
72+
}),
73+
],
74+
});
75+
```
76+
77+
## JSX Import Source
78+
79+
- **类型**: `string`
80+
- **默认值**: `'react'`
81+
82+
`runtime` 的值为 `'automatic'`,可以通过 `importSource` 指定 JSX transform 的 import 路径。
83+
84+
例如,当使用 [Emotion](https://emotion.sh/),你可以设置 `importSource``'@emotion/react'`:
85+
86+
```ts title="rslib.config.ts" {13-15}
87+
import { pluginReact } from '@rsbuild/plugin-react';
88+
import { defineConfig } from '@rslib/core';
89+
90+
export default defineConfig({
91+
lib: [
92+
// ...
93+
],
94+
output: {
95+
target: 'web',
96+
},
97+
plugins: [
98+
pluginReact({
99+
swcReactOptions: {
100+
importSource: '@emotion/react',
101+
},
102+
}),
103+
],
104+
});
105+
```
106+
107+
{/* TODO */}
108+
{/* ## SVGR */}
109+
110+
## 进一步了解
111+
112+
- [Rsbuild React Plugin](https://rsbuild.dev/plugins/list/plugin-react#swcreactoptionsruntime)
113+
- [SWC Compilation - jsc.transform.react](https://swc.rs/docs/configuration/compilation#jsctransformreact)

0 commit comments

Comments
 (0)