Skip to content

Commit 8770f99

Browse files
committed
docs: Configure Rslib
1 parent fc0f68b commit 8770f99

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,195 @@
11
# Configure Rslib
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.
6+
7+
## Configuration Structure
8+
9+
The configuration structure of Rslib looks like this:
10+
11+
```js title="rslib.config.mjs"
12+
export default {
13+
// Array of library configurations.
14+
// Each object represents a set of independent configurations for each output format.
15+
// Different Rslib and Rsbuild configurations can be specified in each object.
16+
lib: [
17+
// The first set of configurations
18+
{
19+
// Independent Rslib configuration of ESM format
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 configuration
45+
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
53+
},
54+
plugins: [
55+
// configure Rsbuild plugins
56+
],
57+
// other independent Rsbuild configuration options
58+
},
59+
{
60+
// The second set of configurations of CJS format
61+
format: 'cjs',
62+
// ... other independent Rslib / Rsbuild configurations
63+
},
64+
// ... Extra sets of configurations (if needed)
65+
],
66+
// Shared Rsbuild configuration which will be merged into each of the above lib configuration objects
67+
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
75+
},
76+
plugins: [
77+
// configure Rsbuild plugins
78+
],
79+
// ... other Rsbuild configuration options
80+
};
81+
```
82+
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.
84+
85+
- **Lib Configuration (required)**: The `lib` array contains multiple objects, each representing a set of independent configurations tailored for different output formats, such as ESM and CJS. Each object within the `lib` array can specify distinct configurations for both Rslib and Rsbuild.
86+
- **Independent Rslib Configuration (required)**: The configuration options provided by Rslib. Rslib will transform these options into complex Rsbuild configuration of each object.
87+
- **Independent Rsbuild Configuration (optional)**: The configuration options provided by Rsbuild which can override the shared Rsbuild configuration outside.
88+
- **Shared Rsbuild Configuration (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.
89+
90+
Rslib generates corresponding Rsbuild [environments](https://rsbuild.dev/guide/advanced/environments) configurations based on the multiple build configurations of different output formats. See [Configuration Debug](/configure-rslib#configuration-debug) for more details.
91+
92+
## Configuration Usage
93+
94+
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:
95+
96+
- `rslib.config.mjs`
97+
- `rslib.config.ts`
98+
- `rslib.config.js`
99+
- `rslib.config.cjs`
100+
- `rslib.config.mts`
101+
- `rslib.config.cts`
102+
103+
We recommend using the `.mjs` or `.ts` format for the configuration file and importing the `defineConfig` utility function from `@rslib/core`. It provides friendly TypeScript type hints and autocompletion, which can help you avoid errors in the configuration.
104+
105+
For example, in `rslib.config.ts`, you can define the Rslib [syntax](/config/lib/syntax) configuration and the Rsbuild [output.target](https://rsbuild.dev/config/output/target#outputtarget) configuration:
106+
107+
```ts title="rslib.config.ts"
108+
import { defineConfig } from '@rslib/core';
109+
110+
export default defineConfig({
111+
lib: [
112+
{
113+
format: 'esm',
114+
syntax: 'es2021',
115+
},
116+
],
117+
output: {
118+
target: 'node',
119+
},
120+
});
121+
```
122+
123+
If you are developing a non-TypeScript project, you can use the `.mjs` format for the configuration file.
124+
125+
:::tip
126+
127+
When you use the `.ts`, `.mts`, and `.cts` extensions, Rslib will use [jiti](https://github.com/unjs/jiti) to load configuration files, providing interoperability between ESM and CommonJS. The behavior of module resolution differs slightly from the native behavior of Node.js.
128+
129+
:::
130+
131+
## Specify Config File
132+
133+
Rslib CLI uses the `--config` option to specify the config file, which can be set to a relative path or an absolute path.
134+
135+
For example, if you need to use the `rslib.prod.config.mjs` file when running `build`, you can add the following scripts to `package.json`:
136+
137+
```json title="package.json"
138+
{
139+
"scripts": {
140+
"build": "rslib build --config rslib.prod.config.mjs"
141+
}
142+
}
143+
```
144+
145+
You can also abbreviate the `--config` option to `-c`:
146+
147+
```bash
148+
rslib build -c rslib.prod.config.mjs
149+
```
150+
151+
## Using Environment Variables
152+
153+
In the configuration file, you can use Node.js environment variables such as `process.env.NODE_ENV` to dynamically set different configurations:
154+
155+
```ts title="rslib.config.ts"
156+
import { defineConfig } from '@rslib/core';
157+
158+
export default defineConfig({
159+
lib: [
160+
{
161+
format: 'esm',
162+
},
163+
],
164+
source: {
165+
alias: {
166+
'@request':
167+
process.env.NODE_ENV === 'development'
168+
? './src/request.dev.js'
169+
: './src/request.prod.js',
170+
},
171+
},
172+
});
173+
```
174+
175+
## Configuration Debug
176+
177+
You can enable Rslib's debug mode by adding the `DEBUG=rsbuild` environment variable when executing a build. It will display the final Rsbuild/Rspack configuration after processing by Rslib.
178+
179+
```bash
180+
DEBUG=rsbuild pnpm build
181+
```
182+
183+
In debug mode, Rslib will write the Rsbuild/Rspack config to the dist directory, which is convenient for developers to view and debug.
184+
185+
```
186+
success Inspect config succeed, open following files to view the content:
187+
188+
- Rsbuild Config (esm): /project/dist/.rsbuild/rsbuild.config.esm.mjs
189+
- Rsbuild Config (cjs): /project/dist/.rsbuild/rsbuild.config.cjs.mjs
190+
- Rspack Config (esm): /project/dist/.rsbuild/rspack.config.esm.mjs
191+
- Rspack Config (cjs): /project/dist/.rsbuild/rspack.config.cjs.mjs
192+
```
193+
194+
- Open the generated `/dist/.rsbuild/rsbuild.config.esm.mjs` file to see the complete content of the Rsbuild config.
195+
- Open the generated `/dist/.rsbuild/rspack.config.esm.mjs` file to see the complete content of the Rspack config.

0 commit comments

Comments
 (0)