typeStartDevServerOptions={// Whether to output URL infos, the default is true printURLs?:boolean|Function;// custom Compiler object compiler?: Compiler | MultiCompiler;// Whether to get the port silently, the default is false getPortSilently?:boolean;// custom logger logger?: Logger;};typeStartServerResult={ urls:string[]; port:number; server: Server;};functionStartDevServer( options?: StartDevServerOptions,):Promise<StartServerResult>;
Example:
Start Dev Server:
import{ logger }from'@rsbuild/core';// Start dev serverawait rsbuild.startDevServer();// Start dev server and handle the errortry{await rsbuild.startDevServer();}catch(err){ logger.error('Failed to start dev server.'); logger.error(err); process.exit(1);}
After successfully starting Dev Server, you can see the following logs:
const{ urls, port, server }=await rsbuild.startDevServer();console.log(urls);// ['http://localhost:8080', 'http://192.168.0.1:8080']console.log(port);// 8080// Close the dev serverawait server.close();
In some cases, the default startup port number is already occupied. In this situation, Rsbuild will automatically increment the port number until it finds an available one. This process will output a prompt log. If you do not want this log, you can set getPortSilently to true.
By default, Rsbuild uses rslog to output logs. You can customize the log output object through the logger parameter.
const customLogger ={// You need to define the following methodsinfo(msg:string){console.log(msg);},error(msg:string){console.error(msg);},warn(msg:string){console.warn(msg);},success(msg:string){console.log(`✅ msg`);},debug(msg:string){if(process.env.DEBUG){console.log(msg);}},log(msg:string){console.log(msg);};}await rsbuild.startDevServer({ logger: customLogger,});
Then Rsbuild will use the custom logger to output logs.
import{ logger }from'@rsbuild/core';// Start preview serverawait rsbuild.preview();// Start preview server and hanble the errortry{await rsbuild.preview();}catch(err){ logger.error('Failed to start preview server.'); logger.error(err); process.exit(1);}
preview returns the following parameters:
urls: URLs to access server.
port: The actual listening port number.
server: Server instance object.
const{ urls, port, server }=await rsbuild.preview();console.log(urls);// ['http://localhost:8080', 'http://192.168.0.1:8080']console.log(port);// 8080// Close the serverawait server.close();
When the target option of createRsbuild contains only one value, the return value is Compiler; when target contains multiple values, the return value is MultiCompiler.
rsbuild.addPlugins([pluginFoo(),pluginBar()]);// Insert before the bar pluginrsbuild.addPlugins([pluginFoo()],{ before:'bar'});// Insert after the bar pluginrsbuild.addPlugins([pluginFoo()],{ after:'bar'});
Inspect the final generated Rsbuild config and bundler config.
TIP
The inspectConfig method does not support simultaneous use with the startDevServer / build method.
When you need to view the complete Rsbuild and bundler configurations during the build process, you can use the debug mode or obtain them through hooks such as onBeforeBuild and onBeforeCreateCompile.
Type:
typeInspectConfigOptions={// View the config in the specified environment, the default is "development", can be set to "production" env?: RsbuildMode;// Whether to enable verbose mode, display the complete content of the function in the config, the default is `false` verbose?:boolean;// Specify the output path, defaults to the value configured by `output.distPath.root` outputPath?:string;// Whether to write the result to disk, the default is `false` writeToDisk?:boolean;};asyncfunctionInspectConfig(options?: InspectConfigOptions):Promise<{ rsbuildConfig:string; bundlerConfigs:string[]; origin:{ rsbuildConfig: RsbuildConfig; bundlerConfigs: BundlerConfigs[];};}>;
onBeforeCreateCompiler is a callback function that is triggered after the Compiler instance has been created, but before the build process begins. This hook is called when you run rsbuild.startDevServer, rsbuild.build, or rsbuild.createCompiler.
You can access the Compiler instance object through the compiler parameter:
If the current bundler is Rspack, you will get the Rspack Compiler object.
If the current bundler is webpack, you will get the webpack Compiler object.
onAfterCreateCompiler is a callback function that is triggered after the compiler instance has been created, but before the build process. This hook is called when you run rsbuild.startDevServer, rsbuild.build, or rsbuild.createCompiler.
You can access the Compiler instance object through the compiler parameter:
If the current bundler is Rspack, you will get the Rspack Compiler object.
If the current bundler is webpack, you will get the webpack Compiler object.
onBeforeBuild is a callback function that is triggered before the production build is executed. You can access the final configuration array of the underlying bundler through the `bundlerConfigs' parameter:
If the current bundler is Rspack, you will get an Rspack configuration array.
If the current bundler is webpack, you will get a webpack configuration array.
The configuration array can contain one or more configurations, depending on the current target config of Rsbuild.
onAfterBuild is a callback function that is triggered after running the production build. You can access the build result information via the `stats' parameter:
If the current bundler is Rspack, you will get Rspack Stats.
If the current bundler is webpack, you will get webpack Stats.
You can specify the type of Rsbuild config to read by using the type parameter:
// Get the original Rsbuild config defined by the user.getRsbuildConfig('original');// Get the current Rsbuild config.// The content of this config will change at different execution stages of Rsbuild.// For example, the content of the current Rsbuild config will be modified after running the `modifyRsbuildConfig` hook.getRsbuildConfig('current');// Get the normalized Rsbuild config.// This method must be called after the `modifyRsbuildConfig` hook has been executed.// It is equivalent to the `getNormalizedConfig` method.getRsbuildConfig('normalized');
Get the normalized Rsbuild config, this method must be called after the modifyRsbuildConfig hook is executed.
Compared with the api.getRsbuildConfig method, the config returned by this method has been normalized, and the type definition of the config will be narrowed. For example, the undefined type of config.html will be removed.
It is recommended to use this method to get the Rsbuild config.