1- import type { LogLevel , RsbuildMode , RsbuildPlugin } from '@rsbuild/core' ;
1+ import type { LogLevel , RsbuildMode } from '@rsbuild/core' ;
22import cac , { type CAC } from 'cac' ;
33import type { ConfigLoader } from '../loadConfig' ;
4+ import { watchFilesForRestart } from '../restart' ;
45import type { Format , Syntax } from '../types' ;
56import { color } from '../utils/color' ;
67import { logger } from '../utils/logger' ;
7- import { build } from './build' ;
8- import { initConfig } from './initConfig' ;
9- import { inspect } from './inspect' ;
10- import { startMFDevServer } from './mf' ;
11- import { watchFilesForRestart } from './restart' ;
8+ import { init } from './init' ;
129
1310export const RSPACK_BUILD_ERROR = 'Rspack build failed.' ;
1411
1512export type CommonOptions = {
1613 root ?: string ;
1714 config ?: string ;
15+ configLoader ?: ConfigLoader ;
16+ env ?: boolean ;
1817 envDir ?: string ;
1918 envMode ?: string ;
2019 lib ?: string [ ] ;
21- configLoader ?: ConfigLoader ;
2220 logLevel ?: LogLevel ;
2321} ;
2422
@@ -53,6 +51,13 @@ const applyCommonOptions = (cli: CAC) => {
5351 '-c, --config <config>' ,
5452 'specify the configuration file, can be a relative or absolute path' ,
5553 )
54+ . option (
55+ '--config-loader <loader>' ,
56+ 'Set the config file loader (auto | jiti | native)' ,
57+ {
58+ default : 'auto' ,
59+ } ,
60+ )
5661 . option (
5762 '-r, --root <root>' ,
5863 'specify the project root directory, can be an absolute path or a path relative to cwd' ,
@@ -61,13 +66,6 @@ const applyCommonOptions = (cli: CAC) => {
6166 '--env-mode <mode>' ,
6267 'specify the env mode to load the `.env.[mode]` file' ,
6368 )
64- . option (
65- '--config-loader <loader>' ,
66- 'Set the config file loader (auto | jiti | native)' ,
67- {
68- default : 'auto' ,
69- } ,
70- )
7169 . option ( '--env-dir <dir>' , 'specify the directory to load `.env` files' )
7270 . option (
7371 '--log-level <level>' ,
@@ -80,7 +78,8 @@ const applyCommonOptions = (cli: CAC) => {
8078 type : [ String ] ,
8179 default : [ ] ,
8280 } ,
83- ) ;
81+ )
82+ . option ( '--no-env' , 'Disable loading of `.env` files' ) ;
8483} ;
8584
8685export function setupCommands ( ) : void {
@@ -141,26 +140,15 @@ export function setupCommands(): void {
141140 . action ( async ( options : BuildOptions ) => {
142141 try {
143142 const cliBuild = async ( ) => {
144- const { config , watchFiles } = await initConfig ( options ) ;
143+ const rslib = await init ( options ) ;
145144
146145 if ( options . watch ) {
147- config . plugins = config . plugins || [ ] ;
148- config . plugins . push ( {
149- name : 'rslib:on-after-build' ,
150- setup ( api ) {
151- api . onAfterBuild ( ( { isFirstCompile } ) => {
152- if ( isFirstCompile ) {
153- logger . success ( 'build complete, watching for changes...' ) ;
154- }
155- } ) ;
156- } ,
157- } satisfies RsbuildPlugin ) ;
158-
159- watchFilesForRestart ( watchFiles , async ( ) => {
146+ watchFilesForRestart ( rslib . context . watchFiles , async ( ) => {
160147 await cliBuild ( ) ;
161148 } ) ;
162149 }
163- await build ( config , options ) ;
150+
151+ await rslib . build ( options ) ;
164152 } ;
165153
166154 await cliBuild ( ) ;
@@ -189,12 +177,13 @@ export function setupCommands(): void {
189177 . action ( async ( options : InspectOptions ) => {
190178 try {
191179 // TODO: inspect should output Rslib's config
192- const { config } = await initConfig ( options ) ;
193- await inspect ( config , {
180+ const rslib = await init ( options ) ;
181+ await rslib . inspectConfig ( {
194182 lib : options . lib ,
195183 mode : options . mode ,
196- output : options . output ,
184+ outputPath : options . output ,
197185 verbose : options . verbose ,
186+ writeToDisk : true ,
198187 } ) ;
199188 } catch ( err ) {
200189 logger . error ( 'Failed to inspect config.' ) ;
@@ -206,12 +195,12 @@ export function setupCommands(): void {
206195 mfDevCommand . action ( async ( options : MfDevOptions ) => {
207196 try {
208197 const cliMfDev = async ( ) => {
209- const { config , watchFiles } = await initConfig ( options ) ;
210- await startMFDevServer ( config , {
198+ const rslib = await init ( options ) ;
199+ await rslib . startMFDevServer ( {
211200 lib : options . lib ,
212201 } ) ;
213202
214- watchFilesForRestart ( watchFiles , async ( ) => {
203+ watchFilesForRestart ( rslib . context . watchFiles , async ( ) => {
215204 await cliMfDev ( ) ;
216205 } ) ;
217206 } ;
0 commit comments