rspack
Type: Object | Function | undefined
Default: undefined
Bundler: only support Rspack
tools.rspack is used to configure Rspack .
Object Type
tools.rspack can be configured as an object to be deep merged with the built-in Rspack configuration through webpack-merge .
For example, add resolve.alias configuration:
export default {
tools : {
rspack : {
resolve : {
alias : {
'@util' : 'src/util' ,
} ,
} ,
} ,
} ,
} ;
Function Type
tools.rspack can be configured as a function. The first parameter of this function is the built-in Rspack configuration object, you can modify this object, and then return it. For example:
export default {
tools : {
rspack : ( config ) => {
config . resolve . alias [ '@util' ] = 'src/util' ;
return config ;
} ,
} ,
} ;
TIP
The object returned by the tools.rspack function is used directly as the final Rspack configuration and is not merged with the built-in Rspack configuration.
The second parameter of this function is an object, which contains some utility functions and properties, as follows:
Utils
env
Type: 'development' | 'production' | 'test'
The env parameter can be used to determine whether the current environment is development, production or test. For example:
export default {
tools : {
rspack : ( config , { env } ) => {
if ( env === 'development' ) {
config . devtool = 'cheap-module-eval-source-map' ;
}
return config ;
} ,
} ,
} ;
isProd
The isProd parameter can be used to determine whether the current environment is production. For example:
export default {
tools : {
rspack : ( config , { isProd } ) => {
if ( isProd ) {
config . devtool = 'source-map' ;
}
return config ;
} ,
} ,
} ;
target
Type: 'web' | 'node' | 'web-worker'
The target parameter can be used to determine the current target. For example:
export default {
tools : {
rspack : ( config , { target } ) => {
if ( target === 'node' ) {
// ...
}
return config ;
} ,
} ,
} ;
isServer
Determines whether the target environment is node, equivalent to target === 'node'.
export default {
tools : {
rspack : ( config , { isServer } ) => {
if ( isServer ) {
// ...
}
return config ;
} ,
} ,
} ;
isWebWorker
Determines whether the target environment is web-worker, equivalent to target === 'web-worker'.
export default {
tools : {
rspack : ( config , { isWebWorker } ) => {
if ( isWebWorker ) {
// ...
}
return config ;
} ,
} ,
} ;
rspack
Type: typeof import('@rspack/core')
The Rspack instance. For example:
export default {
tools : {
rspack : ( config , { rspack } ) => {
config . plugins . push ( new rspack . BannerPlugin ( ) ) ;
return config ;
} ,
} ,
} ;
addRules
Type: (rules: RuleSetRule | RuleSetRule[]) => void
Add additional Rspack rules .
For example:
export default {
tools : {
rspack : ( config , { addRules } ) => {
// add a single rule
addRules ( {
test : / \.foo / ,
loader : require . resolve ( 'foo-loader' ) ,
} ) ;
// Add multiple rules as an array
addRules ( [
{
test : / \.foo / ,
loader : require . resolve ( 'foo-loader' ) ,
} ,
{
test : / \.bar / ,
loader : require . resolve ( 'bar-loader' ) ,
} ,
] ) ;
} ,
} ,
} ;
prependPlugins
Type: (plugins: BundlerPluginInstance | BundlerPluginInstance[]) => void
Add additional plugins to the head of the internal Rspack plugins array, and the plugin will be executed first.
export default {
tools : {
rspack : ( config , { prependPlugins } ) => {
// add a single plugin
prependPlugins ( new PluginA ( ) ) ;
// Add multiple plugins
prependPlugins ( [ new PluginA ( ) , new PluginB ( ) ] ) ;
} ,
} ,
} ;
appendPlugins
Type: (plugins: BundlerPluginInstance | BundlerPluginInstance[]) => void
Add additional plugins at the end of the internal Rspack plugins array, the plugin will be executed last.
export default {
tools : {
rspack : ( config , { appendPlugins } ) => {
// add a single plugin
appendPlugins ( [ new PluginA ( ) ] ) ;
// Add multiple plugins
appendPlugins ( [ new PluginA ( ) , new PluginB ( ) ] ) ;
} ,
} ,
} ;
removePlugin
Type: (name: string) => void
Remove the internal Rspack plugin, the parameter is the constructor.name of the plugin.
For example, remove the internal webpack-bundle-analyzer :
export default {
tools : {
rspack : ( config , { removePlugin } ) => {
removePlugin ( 'BundleAnalyzerPlugin' ) ;
} ,
} ,
} ;
mergeConfig
Type: (...configs: RspackConfig[]) => RspackConfig
Used to merge multiple Rspack configs, same as webpack-merge .
export default {
tools : {
rspack : ( config , { mergeConfig } ) => {
return mergeConfig ( config , {
devtool : 'eval' ,
} ) ;
} ,
} ,
} ;