Rslib core
This chapter introduces some of the core methods provided by Rslib.
createRslib
Create an Rslib instance.
- Type:
- Example:
Options
The first parameter of createRslib is an options object with the following properties:
cwd: The root path of the current build, defaults toprocess.cwd().config: Rslib configuration object. Refer to Configuration overview for all available configuration options.loadEnv: Whether to call the loadEnv method to load environment variables and define them as global variables via source.define.
Load configuration async
config can also be an async function for dynamically loading Rslib configuration and performing custom operations.
Load environment variables
The loadEnv option in createRslib calls the loadEnv method to load environment variables:
Setting loadEnv: true automatically completes these steps:
- Call the
loadEnvmethod to load environment variables. - Add source.define configuration, defining the
publicVarsreturned byloadEnvas global variables. - Watch the
.envfile for changes, restart build or restart the dev server when the file changes, and invalidate the build cache. - Automatically call the
cleanupmethod returned byloadEnvwhen closing the build or dev server.
You can also pass in the options of the loadEnv method, for example:
loadConfig
Load Rslib configuration file.
- Type:
- Example:
If the Rslib config file does not exist in the cwd directory, the return value of the loadConfig method is { content: {}, filePath: null }.
Specify the configuration file
Use the path option to load the my-config.ts configuration file:
Passing meta object
Load the configuration file and pass in a custom meta object:
In the defineConfig configuration function, you can access the foo variable through the meta object:
loadEnv
Load the .env file and return all environment variables starting with the specified prefixes.
The usage is the same as Rsbuild, see loadEnv -- Rsbuild for details.
- Rslib CLI will automatically call the
loadEnv()method. If you are using the Rslib CLI, you can set themodeparameter through the--env-modeoption. - The
loadEnvoption in createRslib will help you call theloadEnv()method and handle related operations.
mergeRslibConfig
Used to merge multiple Rslib configuration objects.
The mergeRslibConfig function takes multiple configuration objects as parameters. It deep merges each configuration object, automatically combining multiple function values into an array of sequentially executed functions, and returns a merged configuration object.
- Type:
Basic example
Merge rules
For each item in the lib object array, the merge is processed based on the id field:
- Objects with the same
idare deeply merged - Objects without
idwill be appended to the end of the result array - Objects with
idare sorted in order of first occurrence
Other configuration fields follow the same merge rules as mergeRsbuildConfig.
rspack
If you need to access the API or plugins exported by @rspack/core, you can directly import the rspack object from @rslib/core without installing the @rspack/core package separately.
- Type:
Rspack - Example:
- Refer to Rspack plugins and Rspack JavaScript API to learn more about the available Rspack APIs.
- It's not recommended to manually install the
@rspack/corepackage, as it may conflict with the version that Rslib depends on.
rsbuild
If you need to access the API exported by @rsbuild/core, you can directly import the rsbuild object from @rslib/core without installing the @rsbuild/core package separately.
- Example:
Refer to Rsbuild core for available Rsbuild APIs.
version
The version of @rslib/core currently in use.
- Type:
string - Example:
