|
1 | 1 | # 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 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 | + lib: [ |
| 17 | + // The first set of Lib configurations |
| 18 | + { |
| 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 |
| 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 configurations |
| 58 | + }, |
| 59 | + { |
| 60 | + // The second set of Lib configurations |
| 61 | + format: 'cjs', |
| 62 | + // ... other independent Rslib specific configurations and Rsbuild configurations |
| 63 | + }, |
| 64 | + // ... Additional sets of Lib configurations (if needed) |
| 65 | + ], |
| 66 | + // Shared Rsbuild configurations |
| 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 configurations |
| 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 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. |
| 88 | + |
| 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 |
| 92 | + |
| 93 | +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: |
| 94 | + |
| 95 | +- `rslib.config.mjs` |
| 96 | +- `rslib.config.ts` |
| 97 | +- `rslib.config.js` |
| 98 | +- `rslib.config.cjs` |
| 99 | +- `rslib.config.mts` |
| 100 | +- `rslib.config.cts` |
| 101 | + |
| 102 | +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. |
| 103 | + |
| 104 | +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: |
| 105 | + |
| 106 | +```ts title="rslib.config.ts" |
| 107 | +import { defineConfig } from '@rslib/core'; |
| 108 | + |
| 109 | +export default defineConfig({ |
| 110 | + lib: [ |
| 111 | + { |
| 112 | + format: 'esm', |
| 113 | + syntax: 'es2021', |
| 114 | + }, |
| 115 | + ], |
| 116 | + output: { |
| 117 | + target: 'node', |
| 118 | + }, |
| 119 | +}); |
| 120 | +``` |
| 121 | + |
| 122 | +If you are developing a non-TypeScript project, you can use the `.mjs` format for the configuration file. |
| 123 | + |
| 124 | +:::tip |
| 125 | + |
| 126 | +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. |
| 127 | + |
| 128 | +::: |
| 129 | + |
| 130 | +## Specify Config File |
| 131 | + |
| 132 | +Rslib CLI uses the `--config` option to specify the config file, which can be set to a relative path or an absolute path. |
| 133 | + |
| 134 | +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`: |
| 135 | + |
| 136 | +```json title="package.json" |
| 137 | +{ |
| 138 | + "scripts": { |
| 139 | + "build": "rslib build --config rslib.prod.config.mjs" |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +You can also abbreviate the `--config` option to `-c`: |
| 145 | + |
| 146 | +```bash |
| 147 | +rslib build -c rslib.prod.config.mjs |
| 148 | +``` |
| 149 | + |
| 150 | +## Using Environment Variables |
| 151 | + |
| 152 | +In the configuration file, you can use Node.js environment variables such as `process.env.NODE_ENV` to dynamically set different configurations: |
| 153 | + |
| 154 | +```ts title="rslib.config.ts" |
| 155 | +import { defineConfig } from '@rslib/core'; |
| 156 | + |
| 157 | +export default defineConfig({ |
| 158 | + lib: [ |
| 159 | + { |
| 160 | + format: 'esm', |
| 161 | + }, |
| 162 | + ], |
| 163 | + source: { |
| 164 | + alias: { |
| 165 | + '@request': |
| 166 | + process.env.NODE_ENV === 'development' |
| 167 | + ? './src/request.dev.js' |
| 168 | + : './src/request.prod.js', |
| 169 | + }, |
| 170 | + }, |
| 171 | +}); |
| 172 | +``` |
| 173 | + |
| 174 | +## Configure Rsbuild |
| 175 | + |
| 176 | +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). |
| 179 | + |
| 180 | +You can also refer to the [Rsbuild Configuration](https://rsbuild.dev/config/index#config-overview) for checking the complete configuration items. |
| 181 | + |
| 182 | +## Configure Rspack |
| 183 | + |
| 184 | +Rslib is built on top of Rsbuild and Rsbuild supports directly modifying the Rspack configuration object and also supports modifying the built-in Rspack configuration of Rsbuild through `rspack-chain`. This means you can configure Rspack related configurations in an Rslib project as well. |
| 185 | + |
| 186 | +For more details, refer to [Configure Rspack](https://rsbuild.dev/guide/basic/configure-rspack). |
| 187 | + |
| 188 | +## Configuration Debug |
| 189 | + |
| 190 | +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. |
| 191 | + |
| 192 | +```bash |
| 193 | +DEBUG=rsbuild pnpm build |
| 194 | +``` |
| 195 | + |
| 196 | +In debug mode, Rslib will write the Rsbuild / Rspack config to the dist directory, which is convenient for developers to view and debug. |
| 197 | + |
| 198 | +Below is an example of a configuration for Rslib that builds both CJS and ESM output. |
| 199 | + |
| 200 | +``` |
| 201 | +Inspect config succeed, open following files to view the content: |
| 202 | +
|
| 203 | + - Rsbuild Config (esm): /project/dist/.rsbuild/rsbuild.config.esm.mjs |
| 204 | + - Rsbuild Config (cjs): /project/dist/.rsbuild/rsbuild.config.cjs.mjs |
| 205 | + - Rspack Config (esm): /project/dist/.rsbuild/rspack.config.esm.mjs |
| 206 | + - Rspack Config (cjs): /project/dist/.rsbuild/rspack.config.cjs.mjs |
| 207 | +``` |
| 208 | + |
| 209 | +- Open the generated `/dist/.rsbuild/rsbuild.config.esm.mjs` file to see the complete content of the Rsbuild config. |
| 210 | +- Open the generated `/dist/.rsbuild/rspack.config.esm.mjs` file to see the complete content of the Rspack config. |
0 commit comments