Interface: Source
The Source option changes the behavior of source files.
Properties
alias?
optionalalias?:Record<string,string|false|string[]>
Create aliases to import or require certain modules more easily.
Deprecated
Use Resolve.alias instead.
Default Value
assetsInclude?
optionalassetsInclude?:RuleSetCondition
Include additional files that should be treated as static assets.
Default Value
Remarks
By default, Rsbuild treats common image, font, audio, and video files as static assets. Through the source.assetsInclude config, you can specify additional file types that should be treated as static assets. These added static assets are processed using the same rules as the built-in supported static assets。
The usage of source.assetsInclude is consistent with Condition
in Rspack, which supports passing in strings, regular expressions, arrays of conditions, or logical conditions
to match the module path or assets.
Example
decorators?
optionaldecorators?:Decorators
Used to configure the decorators syntax.
Default Value
Remarks
See Decorators.version for more information.
define?
optionaldefine?:DefinePluginOptions
The define options is used to define some values or expressions at compile time.
Default Value
Examples
Using define for environment variables.
Expressions will be replaced with the corresponding code fragments:
Using define for typeof.
The typeof expressions will be replaced with the corresponding code fragments:
Using define with objects.
Expressions will be replaced with the corresponding code fragments:
Remarks
-
If the value provided is a string, it will be utilized as a code fragment.
-
If the value provided is an object, all its keys will be defined in the same manner.
-
If the value isn't a string, it will be stringified, with functions included.
-
Notably, if a
typeofprefix is attached to the key, it will be exclusively defined fortypeofcalls."
entry?
optionalentry?:Entry
The Entry option is used to set the entry module.
Default Value
'./src/index.js'
Examples
- Use a single entry:
- Use a single entry with multiple entry modules:
- Use multiple entries(with multiple entry modules):
- Use multiple entries with EntryDescription:
exclude?
optionalexclude?:RuleSetCondition[]
The source.exclude is used to specify JavaScript files that should be excluded from compilation.
Default Value
Remarks
By default, Rsbuild compiles JavaScript files in the current directory and TypeScript/JSX files
in all directories. Through the source.exclude config, you can specify files or directories
that should be excluded from compilation.
The usage of source.exclude is consistent with Rule.exclude
in Rspack, which supports passing in strings or regular expressions to match module paths.
Examples
- Exclude specific files or directories
You can exclude specific files or directories from compilation to improve build performance or avoid processing certain files:
- Exclude third-party dependencies
You can exclude specific third-party dependencies that don't need compilation:
include?
optionalinclude?:RuleSetCondition[]
The source.include is used to specify additional JavaScript files that need to be compiled.
Default Value
When unset, Rsbuild compiles JavaScript files in the current directory and TypeScript or JSX files in all directories, while excluding JavaScript files under node_modules.
Remarks
To avoid redundant compilation, by default, Rsbuild only compiles JavaScript
files in the current directory and TypeScript and JSX files in all directories.
It does not compile JavaScript files under node_modules.
Through the source.include config, you can specify directories or modules
that need to be compiled by Rsbuild.
The usage of source.include is consistent with Rule.include
in Rspack, which supports passing in strings or regular expressions to match the module path.
Examples
- Compile Npm Packages
A typical usage scenario is to compile npm packages under node_modules,
because some third-party dependencies have ESNext syntax, which may not be supported in Lynx.
You can solve the problem by using this config to specify the dependencies
that need to be compiled.
- Compile Libraries in Monorepo
preEntry?
optionalpreEntry?:string|string[]
Add a script before the entry file of each page. This script will be executed before the page code. It can be used to execute global logics, such as injecting polyfills, setting global styles, etc.
Default Value
Remarks
See source.preEntry for more details.
Example
Relative path will be resolved relative to the project root directory.
transformImport?
optionaltransformImport?:TransformImport[]
The TransformImport option transforms the import paths to enable modular imports from subpaths of third-party packages, similar to the functionality provided by babel-plugin-import.
Default Value
Example
When using the TUX component library, you can import components on demand with the following config:
This will transform the following source code:
to:
tsconfigPath?
optionaltsconfigPath?:string
Configure a custom tsconfig.json file path to use, can be a relative or absolute path.
Default Value
'./tsconfig.json'
Remarks
The tsconfigPath configuration affects the following behaviors:
-
The
pathsfield is used to configure Path Aliases. -
Sets the scope and rules for the Type Check Plugin.
Example
Relative path will be resolved relative to the project root directory.