11import { type RsbuildInstance , createRsbuild } from '@rsbuild/core' ;
2- import {
3- composeRsbuildEnvironments ,
4- loadConfig ,
5- pruneEnvironments ,
6- } from '../config' ;
2+ import { composeRsbuildEnvironments , pruneEnvironments } from '../config' ;
73import { onBeforeRestartServer , watchFilesForRestart } from '../restart' ;
4+ import type { RslibConfig } from '../types/config' ;
85import type { BuildOptions } from './commands' ;
6+ import { loadRslibConfig } from './init' ;
97
108export async function build (
11- options : BuildOptions = { } ,
9+ config : RslibConfig ,
10+ options : Pick < BuildOptions , 'lib' | 'watch' > & {
11+ configFilePath ?: string ;
12+ } = { } ,
1213) : Promise < RsbuildInstance > {
13- const { content : config , filePath : configFilePath } = await loadConfig ( {
14- path : options ?. config ,
15- envMode : options ?. envMode ,
16- } ) ;
17-
1814 const environments = await composeRsbuildEnvironments ( config ) ;
1915 const rsbuildInstance = await createRsbuild ( {
2016 rsbuildConfig : {
@@ -28,12 +24,17 @@ export async function build(
2824
2925 if ( options ?. watch ) {
3026 const files : string [ ] = [ ] ;
31- files . push ( configFilePath ) ;
27+ options . configFilePath && files . push ( options ?. configFilePath ) ;
3228
3329 onBeforeRestartServer ( buildInstance . close ) ;
3430
3531 watchFilesForRestart ( files , async ( ) => {
36- await build ( options ) ;
32+ const { content : rslibConfig , filePath : configFilePath } =
33+ await loadRslibConfig ( options ) ;
34+ await build ( rslibConfig , {
35+ configFilePath,
36+ ...options ,
37+ } ) ;
3738 } ) ;
3839 } else {
3940 await buildInstance . close ( ) ;
0 commit comments