You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/en/guide/basic/configure-rslib.mdx
+49-72Lines changed: 49 additions & 72 deletions
Original file line number
Diff line number
Diff line change
@@ -1,94 +1,72 @@
1
1
# Configure Rslib
2
2
3
-
Rslib provides a wide range of configuration options and sets a common default value for each option, which can meet the requirements of most use cases. Therefore, in most cases, you can use Rslib out of the box with few configurations.
4
-
5
-
If you need to customize build behaviors, you can read the following sections to learn about the configuration options and its usage.
3
+
Rslib's configuration is based on Rsbuild, which means that you can use all of Rsbuild configurations, as well as the `lib` configuration specific to Rslib.
6
4
7
5
## Configuration Structure
8
6
9
-
The configuration structure of Rslib looks like this:
7
+
Rslib provides the `lib` option to configure the library outputs. It is an array, and each object is used to describe a format of the output.
8
+
9
+
For example, output ESM and CJS formats, and use `es2021` syntax:
10
+
11
+
```js title="rslib.config.mjs"
12
+
exportdefault {
13
+
lib: [
14
+
{ format:'esm', syntax:'es2021' },
15
+
{ format:'cjs', syntax:'es2021' },
16
+
],
17
+
};
18
+
```
19
+
20
+
### Common Rsbuild Configurations
21
+
22
+
You can set common Rsbuild configurations outside the `lib` field, which will be inherited by each configuration object inside the `lib` field.
23
+
24
+
For example, set the [output.target](/config/rsbuild/output#outputtarget) of Rsbuild to `web`, which will affect the output of all `lib` configuration objects:
25
+
26
+
```js title="rslib.config.mjs"
27
+
exportdefault {
28
+
lib: [
29
+
{ format:'esm', syntax:'es2021' },
30
+
{ format:'cjs', syntax:'es2021' },
31
+
],
32
+
output: {
33
+
target:'web',
34
+
},
35
+
};
36
+
```
37
+
38
+
### Separate Rsbuild Configurations
39
+
40
+
In the `lib` field, you can set separate Rsbuild configurations for each output format, which will override the common Rsbuild configurations outside the `lib` field.
41
+
42
+
For example, separately set the [output.target](/config/rsbuild/output#outputtarget) of the ESM output to `web`:
10
43
11
44
```js title="rslib.config.mjs"
12
45
exportdefault {
13
-
// Array of Lib configurations.
14
-
// Each object represents a set of independent configurations for each output format.
15
-
// Distinct configurations can be specified in each object, which is a superset of Rsbuild configurations along with Rslib's specific configurations.
16
46
lib: [
17
-
// The first set of Lib configurations
47
+
// The target of the ESM output is `web`
18
48
{
19
-
// Independent Rslib specific configurations
20
-
format:'esm', // options for the output format of JavaScript files
21
-
bundle:true, // options for the build type
22
-
autoExtension:true, // options for the extensions of JavaScript files
23
-
syntax:'esnext', // options for the target syntax
24
-
externalHelpers:false, // options for externalizing helper functions
25
-
umdName:'RslibUmdExample', // options for the export name of the UMD library
26
-
autoExternal: {
27
-
// options for automatically externalizing third-party dependencies
28
-
},
29
-
redirect: {
30
-
// options for redirecting import paths
31
-
},
32
-
dts: {
33
-
// options for generating TypeScript declaration files
34
-
},
35
-
shims: {
36
-
// options for compatibility shims
37
-
},
38
-
banner: {
39
-
// options for the file banner
40
-
},
41
-
footer: {
42
-
// options for the file footer
43
-
},
44
-
// Independent Rsbuild configurations
49
+
format:'esm',
45
50
output: {
46
-
// options for build outputs
47
-
},
48
-
source: {
49
-
// options for source code parsing and compilation
50
-
},
51
-
tools: {
52
-
// options for the low-level tools
51
+
target:'web',
53
52
},
54
-
plugins: [
55
-
// configure Rsbuild plugins
56
-
],
57
-
// other independent Rsbuild configurations
58
53
},
54
+
// The CJS output inherits the common configuration and target is `node`
59
55
{
60
-
// The second set of Lib configurations
61
56
format:'cjs',
62
-
// ... other independent Rslib specific configurations and Rsbuild configurations
63
57
},
64
-
// ... Additional sets of Lib configurations (if needed)
65
58
],
66
-
// Shared Rsbuild configurations
67
59
output: {
68
-
// options for build outputs
69
-
},
70
-
source: {
71
-
// options for source code parsing and compilation
72
-
},
73
-
tools: {
74
-
// options for the low-level tools
60
+
target:'node',
75
61
},
76
-
plugins: [
77
-
// configure Rsbuild plugins
78
-
],
79
-
// ... other Rsbuild configurations
80
62
};
81
63
```
82
64
83
-
The configuration file exports a default object that includes a `lib` array and shared Rsbuild configuration options. You can find detailed descriptions of all configs on the [Configure Overview](/config/) page.
65
+
Rslib will generate the [environments](https://rsbuild.dev/config/environments) configuration of Rsbuild internally, please refer to [Configuration Debug](#configuration-debug) to view the final generated configuration.
84
66
85
-
-**[Lib Configurations](/config/lib/) (at least one object is required)**: The `lib` array contains multiple objects, each representing a set of independent configurations that will generate different outputs. Each object within the `lib` array can specify unique configurations, which is a superset of Rsbuild
86
-
configurations along with Rslib's specific configurations.
87
-
-**Shared [Rsbuild Configurations](/config/rsbuild/) (optional)**: Outside the `lib` array, there are shared Rsbuild configuration options that will be deep merged with independent Rsbuild configuration of each `lib` configuration object.
67
+
You can also refer to the [Configuration Overview](/config/) page to view the detailed introduction of all configurations.
88
68
89
-
Rslib generates corresponding Rsbuild [environments](https://rsbuild.dev/guide/advanced/environments) configurations based on the multiple build configurations of different output formats. You can view the final generated configurations through the [configuration debug](#configuration-debug) documentation.
90
-
91
-
## Configuration Usage
69
+
## Configuration File
92
70
93
71
When you use the CLI of Rslib, Rslib will automatically read the configuration file in the root directory of the current project and resolve it in the following order:
In Rslib project, you can configure Rsbuild related configurations in both `lib` configuration object and shared configuration section.
177
-
178
-
Common-used Rsbuild configurations in library development are listed at [Rsbuild Configuration](/config/rsbuild/).
154
+
Rslib allows you to use most of the Rsbuild configurations. Currently, the `environments` config is not supported because it is generated internally by Rslib.
179
155
180
-
You can also refer to the [Rsbuild Configuration](https://rsbuild.dev/config/index#config-overview) for checking the complete configuration items.
156
+
- Refer to [Rsbuild Configuration](/config/rsbuild/) for common Rsbuild configurations.
157
+
- Refer to [Rsbuild Documentation](https://rsbuild.dev/config/index#config-overview) for all Rsbuild configurations.
181
158
182
159
## Configure Rspack
183
160
@@ -195,7 +172,7 @@ DEBUG=rsbuild pnpm build
195
172
196
173
In debug mode, Rslib will write the Rsbuild / Rspack config to the dist directory, which is convenient for developers to view and debug.
197
174
198
-
Below is an example of a configuration for Rslib that builds both CJS and ESM output.
175
+
Here is an example of a library that sets both CJS and ESM formats:
199
176
200
177
```
201
178
Inspect config succeed, open following files to view the content:
0 commit comments