Rslib comes with a lightweight CLI that includes commands such as build and inspect.
To view all available CLI commands, run the following command in the project directory:
The output is shown below:
The rslib build
command will build the outputs for production in the dist/
directory by default.
Rslib supports injecting env variables or expressions into the code during build, which is helpful for distinguishing the running environment or replacing constants.
You can see more details in Environment Variables.
process.env.NODE_ENV
will be preserved in the build output when format is set to esm
or cjs
. For mf
format, it will be preserved to make output directly usable.
Rslib supports reading .env.[mode]
and .env.[mode].local
files. You can specify the env mode using the --env-mode <mode>
flag.
For example, set the env mode as test
:
Rslib will then read the following files in sequence:
.env
.env.local
.env.test
.env.test.local
The --env-mode
option takes precedence over process.env.NODE_ENV
.
It is recommended to use --env-mode
to set the env mode, and not to modify process.env.NODE_ENV
.
By default, the .env
file is located in the root directory of the project. You can specify the env directory by using the --env-dir <dir>
option in the CLI.
For example, to specify the env directory as config
:
In this case, Rslib will read the ./config/.env
and other env files.
For example, create a .env
file and add the following contents:
Then in the rslib.config.ts
file, you can access the above env variables using import.meta.env.[name]
or process.env.[name]
:
Now, create a .env.local
file and add the following contents:
The value of BAR
is overwritten to '2'
:
You can use rslib build --watch
to turn on watch mode for watching for changes and rebuild.
You can use the --lib
option to build specific libraries. The --lib
option can be repeated to build multiple libraries. Check out the lib.id to learn how to get or set the library ID.
The rslib inspect
command is used to view the Rsbuild config and Rspack config of the Rslib project.
When you run the command npx rslib inspect
in the project root directory, the following files will be generated in the dist/.rsbuild
directory of the project:
rsbuild.config.mjs
: Represents the Rsbuild configuration used during the build.rspack.config.web.mjs
: Represents the Rspack configuration used during the build.By default, the inspect command omits the content of functions in the configuration object. You can add the --verbose
option to output the complete content of functions:
If the current project has multiple output formats, such as ESM artifact and CJS artifact simultaneously, multiple Rspack configuration files will be generated in the dist/.rsbuild
directory.
You can use the --lib
option to inspect specific libraries. The --lib
option can be repeated to inspect multiple libraries. Check out the lib.id to learn how to get or set the library ID.
The rslib mf dev
command is utilized to start Rsbuild dev server of Module Federation format.
This enables you to develop your mf format module within the host app more rapidly.