typeStartDevServerOptions={// Whether to open the page in browser on startup open?:boolean;// Whether to output URL infos, the default is true printURLs?:boolean|Function;// Whether to throw an exception when the port is occupied, the default is false strictPort?:boolean;// custom Compiler object compiler?: Compiler | MultiCompiler;// Whether to get the port silently, the default is false getPortSliently?:boolean;// custom logger logger?: Logger;};typeStartServerResult={ urls:string[]; port:number; server: Server;};functionStartDevServer( options?: StartDevServerOptions,):Promise<StartServerResult>;
Example
Start Dev Server:
await rsbuild.startDevServer();
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 getPortSliently 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.
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 webpack, you will get the webpack Compiler object.
If the current bundler is Rspack, you will get the Rspack 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 webpack, you will get the webpack Compiler object.
If the current bundler is Rspack, you will get the Rspack 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 webpack, you will get a webpack configuration array.
If the current bundler is Rspack, you will get an Rspack 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 webpack, you will get webpack Stats.
If the current bundler is Rspack, you will get Rspack Stats.
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.