Used to decide how to handle different types of modules in a project.
Object
{}
Rule[]
[]
An array of rules that match the module's requests when it is created. These rules can modify the creation behavior of the module. They can apply Loader, etc. to the module.
Rule
{}
Rule defines the conditions for matching a module and the behavior of handling those modules.
Rule behavior
Defines the processing behavior of the corresponding matching module, e.g. :
Rule.use
)Rule.type
)Rule.resolve
)string | RegExp | Condition[] | LogicalConditions
Defines a module's match conditions, common matches are resource
, resourceQuery
, include
, and exclude
.
Example: app.js imports ./image.png?inline#foo
resource
is /path/to/image.png
, and will match against with Rule.resource
ConditionresourceQuery
is ?inline
, and will match against with Rule.resourceQuery
ConditionresourceFragment
is #foo
, and will match against with Rule.resourceFragment
ConditionCondition represents the form of matching a given input, and it supports the following types:
String
: Given an input, the match is successful when the input string satisfies startsWith. Note: You can think of it as input.startsWith(condition)
.RegExp
: Given an input, the match is successful when the input string satisfies the regular expression. Note: You can think of it as condition.test(input)
.Condition[]
: A list of conditions. At least one of the Conditions must match.LogicalConditions
: All Conditions must match.
{ and: Condition[] }
: All Conditions must match.{ or: Condition[] }
: At least one of the Conditions must match.{ not: Condition }
: All Conditions must NOT match.(value: string) => boolean
: If it's called with the input and return a truthy value, the match is succeeds.Nested Rule can be specified under the properties Rule.rules
and Rule.oneOf
, These rules are evaluated only when the parent Rule condition matches. Each nested rule can contain its own conditions.
The order of evaluation is as follows:
Rule.rules
Rule.oneOf
Condition
undefined
Excludes all modules that match this condition and will match against the absolute path of the resource (without query and fragment). This option cannot be present together with Rule.resource
.
Condition
undefined
Matches all modules that match this condition against the absolute path of the resource (without query and fragment). This option cannot be present together with Rule.resource
.
Condition
undefined
Matches all modules that match this resource, and will match against Resource (the absolute path without query and fragment). This option cannot be present together with Rule.test
.
Condition
undefined
Matches all modules that match this resource against the Resource's query. Note: Containing ?
, when Rule.resourceQuery
is ?raw
, it will match the resource request of xxx?raw
Condition
undefined
Matches all modules that match this resource against the Resource's fragment. Note: Containing #
, when Rule.resourceFragment
is #abc
, it will match the resource request of xxx#abc
Condition
undefined
Matches all modules that match this resource, and will match against Resource (the absolute path without query and fragment). This option cannot be present together with Rule.resource
.
Condition
undefined
Matches all modules that match this resource, and will match against Resource (the absolute path without query and fragment) of the module that issued the current module.
Condition
undefined
Matches all modules that match this resource, and will match against the category of the dependency that introduced the current module, for example esm
for import
and import()
, cjs
for require()
, url
for new URL()
and url()
.
Condition
undefined
Matches all modules that match this resource, and will match against the Resource's scheme.
For example, you can treat the inline data uri resource as a separate resource with the following configuration:
Condition
undefined
Matches all modules that match this resource, and will match against the Resource's mimetype.
{ [k: string]: Condition }
undefined
Matches all modules that match this resource, and will match against the data in the Resource's package.json.
This option is deprecated, please use Rule.use
instead
Rule.loader
is a shortcut to Rule.use: [ { loader } ]
. See Rule.use for details.
Rule.options
is a shortcut to Rule.use: [ { options } ]
. See Rule.use for details.
Object
{}
Module's parser configuration
object = { maxSize: number = 8096 }
{}
If the current module size is less than or equal to maxSize
, then the module will be Base64 encoded, otherwise a file will be created. This option can be used only for Asset Module scenarios.
Object
{}
Generator options for the module, e.g. for Asset Module you can specify the output filename.
string
undefined
Same as output.assetModuleFilename
, but for specific rules. Overrides output.assetModuleFilename
and only works for asset and asset/resource module types.
string
undefined
Override output.publicPath
, only for modules with module type 'asset'
or 'asset/resource'
.
Object
{}
Only for modules with module type 'asset'
or 'asset/inline'
.
false | 'base64'
'base64'
When set to 'base64', module source will be encoded using Base64 algorithm. Setting encoding to false will disable encoding. Only for modules with module type 'asset'
or 'asset/inline'
.
string
require('mime-types').lookup(ext)
A mimetype for data URI. Resolves from module resource extension by default. Only for modules with module type 'asset'
or 'asset/inline'
.
boolean
Flag the module for side effects
'javascript/auto' | 'typescript' | 'css' | 'css/module' | 'css/auto' | 'json' | 'asset' | 'asset/source' | 'asset/resource' | 'asset/inline'
Used to mark the type of the matching module, which affects how the module is handled by Rspack's built-in processing. For example, when a module is marked as 'typescript'
then the module is processed using the TS parser/generator.
'javascript/auto'
: JavaScript modules, supported module systems: CommonJS, ESM, no plans for AMD module support at this time.'javascript/esm'
:JavaScript modules, treated as ES Module.'javascript/dynamic'
:JavaScript modules, treated as Script.'typescript'
: TypeScript module (🚧 deprecated, checkout experiments.rspackFuture.disableTransformByDefault for more details)'css'
: CSS module'css/module'
: CSS modules module'css/auto'
: CSS modules module if filename matches /\.module(s)?\.[^.]+$/
, otherwise CSS module'json'
: JSON data module'asset' | 'asset/source' | 'asset/resource' | 'asset/inline'
: See Asset ModuleAn array to pass the Loader package name and its options. string[]
e.g.: use: ['svgr-loader']
is shorthand for use: [ { loader: 'svgr-loader' } ]
.
Loaders will be executed in right-to-left order.
A function can also be used:
Set specific module resolve options based on the matching modules
Rule[]
undefined
A kind of Nested Rule, an array of Rules that is also used when the parent Rule matches.
Rule[]
undefined
A kind of Nested Rule, an array of Rules from which only the first matching Rule is used when the parent Rule matches.
Object
{}
Configure all parsers' options in one place with module.parser
.
Configure options for JavaScript parser.
Specifies global mode for dynamic import.
'lazy' | 'eager'
'lazy'
Specifies global prefetch for dynamic import.
boolean | number
false
Specifies global preload for dynamic import.
boolean | number
false
Enable parsing of new URL()
syntax.
true | false | 'relative'
true
When use 'relative', rspack would generate relative URLs for new URL()
syntax, i.e., there's no base URL included in the result URL:
Object
{}
Configure all generators' options in one place with module.generator
.