1- import { type RsbuildMode , createRsbuild } from '@rsbuild/core' ;
1+ import type { RsbuildMode } from '@rsbuild/core' ;
22import { type Command , program } from 'commander' ;
3- import { build } from '../build' ;
4- import {
5- composeRsbuildEnvironments ,
6- loadConfig ,
7- pruneEnvironments ,
8- } from '../config' ;
9- import { startMFDevServer } from '../mf' ;
103import { logger } from '../utils/logger' ;
4+ import { build } from './build' ;
5+ import { getRslibConfig } from './init' ;
6+ import { inspect } from './inspect' ;
7+ import { startMFDevServer } from './mf' ;
118
129export type CommonOptions = {
10+ root ?: string ;
1311 config ?: string ;
1412 envMode ?: string ;
1513 lib ?: string [ ] ;
@@ -20,8 +18,8 @@ export type BuildOptions = CommonOptions & {
2018} ;
2119
2220export type InspectOptions = CommonOptions & {
23- mode : RsbuildMode ;
24- output : string ;
21+ mode ? : RsbuildMode ;
22+ output ? : string ;
2523 verbose ?: boolean ;
2624} ;
2725
@@ -31,6 +29,10 @@ const applyCommonOptions = (command: Command) => {
3129 '-c --config <config>' ,
3230 'specify the configuration file, can be a relative or absolute path' ,
3331 )
32+ . option (
33+ '-r --root <root>' ,
34+ 'specify the project root directory, can be an absolute path or a path relative to cwd' ,
35+ )
3436 . option (
3537 '--env-mode <mode>' ,
3638 'specify the env mode to load the `.env.[mode]` file' ,
@@ -60,11 +62,12 @@ export function runCli(): void {
6062 . description ( 'build the library for production' )
6163 . action ( async ( options : BuildOptions ) => {
6264 try {
63- const rslibConfig = await loadConfig ( {
64- path : options . config ,
65- envMode : options . envMode ,
65+ const { root, rslibConfig } = await getRslibConfig ( options ) ;
66+ await build ( rslibConfig , {
67+ root,
68+ lib : options . lib ,
69+ watch : options . watch ,
6670 } ) ;
67- await build ( rslibConfig , options ) ;
6871 } catch ( err ) {
6972 logger . error ( 'Failed to build.' ) ;
7073 logger . error ( err ) ;
@@ -88,21 +91,13 @@ export function runCli(): void {
8891 . action ( async ( options : InspectOptions ) => {
8992 try {
9093 // TODO: inspect should output Rslib's config
91- const rslibConfig = await loadConfig ( {
92- path : options . config ,
93- envMode : options . envMode ,
94- } ) ;
95- const environments = await composeRsbuildEnvironments ( rslibConfig ) ;
96- const rsbuildInstance = await createRsbuild ( {
97- rsbuildConfig : {
98- environments : pruneEnvironments ( environments , options . lib ) ,
99- } ,
100- } ) ;
101- await rsbuildInstance . inspectConfig ( {
94+ const { root, rslibConfig } = await getRslibConfig ( options ) ;
95+ await inspect ( rslibConfig , {
96+ root,
97+ lib : options . lib ,
10298 mode : options . mode ,
99+ output : options . output ,
103100 verbose : options . verbose ,
104- outputPath : options . output ,
105- writeToDisk : true ,
106101 } ) ;
107102 } catch ( err ) {
108103 logger . error ( 'Failed to inspect config.' ) ;
@@ -115,11 +110,11 @@ export function runCli(): void {
115110 . description ( 'start Rsbuild dev server of Module Federation format' )
116111 . action ( async ( options : CommonOptions ) => {
117112 try {
118- const rslibConfig = await loadConfig ( {
119- path : options . config ,
120- envMode : options . envMode ,
113+ const { root, rslibConfig } = await getRslibConfig ( options ) ;
114+ // TODO: support lib option in mf dev server
115+ await startMFDevServer ( rslibConfig , {
116+ root,
121117 } ) ;
122- await startMFDevServer ( rslibConfig ) ;
123118 } catch ( err ) {
124119 logger . error ( 'Failed to start mf dev.' ) ;
125120 logger . error ( err ) ;
0 commit comments