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
Specify the library ID. The ID identifies the library and is useful when using the `--lib` flag to build specific libraries with a meaningful `id` in the CLI.
7
+
8
+
:::tip
9
+
10
+
Rslib uses Rsbuild's [environments](https://rsbuild.dev/guide/advanced/environments) feature to build multiple libraries in a single project under the hood. `lib.id` will be used as the key for the generated Rsbuild environment.
11
+
12
+
:::
13
+
14
+
## Default Value
15
+
16
+
By default, Rslib automatically generates an ID for each library in the format `${format}${index}`. Here, `format` refers to the value specified in the current lib's [format](/config/lib/format), and `index` indicates the order of the library within all libraries of the same format. If there is only one library with the current format, the `index` will be empty. Otherwise, it will start from `0` and increment.
17
+
18
+
For example, the libraries in the `esm` format will start from `esm0`, followed by `esm1`, `esm2`, and so on. In contrast, `cjs` and `umd` formats do not include the `index` part since there is only one library for each format.
19
+
20
+
```ts title="rslib.config.ts"
21
+
exportdefault {
22
+
lib: [
23
+
{ format: 'esm' }, // id is `esm0`
24
+
{ format: 'cjs' }, // id is `cjs`
25
+
{ format: 'esm' }, // id is `esm1`
26
+
{ format: 'umd' }, // id is `umd`
27
+
{ format: 'esm' }, // id is `esm2`
28
+
],
29
+
};
30
+
```
31
+
32
+
## Customize ID
33
+
34
+
You can also specify a readable or meaningful ID of the library by setting the `id` field in the library configuration. The user-specified ID will take priority, while the rest will be used together to generate the default ID.
35
+
36
+
For example, `my-lib-a`, `my-lib-b`, and `my-lib-c` will be the IDs of the specified libraries, while the rest will be used to generate and apply the default ID.
37
+
38
+
{/* prettier-ignore-start */}
39
+
```ts title="rslib.config.ts"
40
+
exportdefault {
41
+
lib: [
42
+
{ format: 'esm', id: 'my-lib-a' }, // ID is `my-lib-a`
43
+
{ format: 'cjs', id: 'my-lib-b' }, // ID is `my-lib-b`
44
+
{ format: 'esm' }, // ID is `esm0`
45
+
{ format: 'umd', id: 'my-lib-c' }, // ID is `my-lib-c`
46
+
{ format: 'esm' }, // ID is `esm1`
47
+
],
48
+
};
49
+
```
50
+
{/* prettier-ignore-end */}
51
+
52
+
Then you could only build `my-lib-a` and `my-lib-b` by running the following command:
53
+
54
+
```bash
55
+
npx rslib build --lib my-lib-a --lib my-lib-b
56
+
```
57
+
58
+
:::note
59
+
The id of each library must be unique, otherwise it will cause an error.
0 commit comments