From e605d86ca6d1fac9cb0174b9d4a3032bc1b0debe Mon Sep 17 00:00:00 2001 From: Timeless0911 <1604889533@qq.com> Date: Thu, 22 May 2025 17:09:31 +0800 Subject: [PATCH] perf: replace `commander` with `cac` --- packages/core/package.json | 2 +- packages/core/prebundle.config.mjs | 1 - packages/core/rslib.config.ts | 1 - packages/core/src/cli/commands.ts | 91 +++++++++++++++--------------- packages/core/src/cli/mf.ts | 2 +- packages/core/src/config.ts | 2 +- packages/core/tsconfig.json | 1 - pnpm-lock.yaml | 12 +--- 8 files changed, 53 insertions(+), 59 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 71d8acfe7..7831aab67 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -50,8 +50,8 @@ "@module-federation/rsbuild-plugin": "^0.14.0", "@rslib/tsconfig": "workspace:*", "@types/fs-extra": "^11.0.4", + "cac": "^6.7.14", "chokidar": "^4.0.3", - "commander": "^14.0.0", "fs-extra": "^11.3.0", "memfs": "^4.17.2", "picocolors": "1.1.1", diff --git a/packages/core/prebundle.config.mjs b/packages/core/prebundle.config.mjs index 81cbefc54..a7f747d0a 100644 --- a/packages/core/prebundle.config.mjs +++ b/packages/core/prebundle.config.mjs @@ -12,7 +12,6 @@ export default { typescript: 'typescript', }, dependencies: [ - 'commander', { name: 'chokidar', // strip sourcemap comment diff --git a/packages/core/rslib.config.ts b/packages/core/rslib.config.ts index 6c42b6e3d..c266d4af3 100644 --- a/packages/core/rslib.config.ts +++ b/packages/core/rslib.config.ts @@ -50,7 +50,6 @@ export default defineConfig({ output: { externals: { picocolors: '../compiled/picocolors/index.js', - commander: '../compiled/commander/index.js', chokidar: '../compiled/chokidar/index.js', rslog: '../compiled/rslog/index.js', }, diff --git a/packages/core/src/cli/commands.ts b/packages/core/src/cli/commands.ts index 9d73cdb22..77fe7188a 100644 --- a/packages/core/src/cli/commands.ts +++ b/packages/core/src/cli/commands.ts @@ -1,5 +1,5 @@ import type { RsbuildMode } from '@rsbuild/core'; -import { type Command, program } from 'commander'; +import cac, { type CAC } from 'cac'; import { logger } from '../utils/logger'; import { build } from './build'; import { init } from './init'; @@ -25,14 +25,14 @@ export type InspectOptions = CommonOptions & { verbose?: boolean; }; -const applyCommonOptions = (command: Command) => { - command +const applyCommonOptions = (cli: CAC) => { + cli .option( - '-c --config ', + '-c, --config ', 'specify the configuration file, can be a relative or absolute path', ) .option( - '-r --root ', + '-r, --root ', 'specify the project root directory, can be an absolute path or a path relative to cwd', ) .option( @@ -43,26 +43,33 @@ const applyCommonOptions = (command: Command) => { .option( '--lib ', 'specify the library (repeatable, e.g. --lib esm --lib cjs)', - repeatableOption, + { + type: [String], + default: [], + }, ); }; -const repeatableOption = (value: string, previous: string[]) => { - return (previous ?? []).concat([value]); -}; - export function runCli(): void { - program.name('rslib').usage(' [options]').version(RSLIB_VERSION); + const cli = cac('rslib'); + + cli.help(); + cli.version(RSLIB_VERSION); - const buildCommand = program.command('build'); - const inspectCommand = program.command('inspect'); - const mfDevCommand = program.command('mf-dev'); + applyCommonOptions(cli); - [buildCommand, inspectCommand, mfDevCommand].forEach(applyCommonOptions); + const buildCommand = cli.command('build', 'build the library for production'); + const inspectCommand = cli.command( + 'inspect', + 'inspect the Rsbuild / Rspack configs of Rslib projects', + ); + const mfDevCommand = cli.command( + 'mf-dev', + 'start Rsbuild dev server of Module Federation format', + ); buildCommand - .option('-w --watch', 'turn on watch mode, watch for changes and rebuild') - .description('build the library for production') + .option('-w, --watch', 'turn on watch mode, watch for changes and rebuild') .action(async (options: BuildOptions) => { try { const cliBuild = async () => { @@ -92,12 +99,9 @@ export function runCli(): void { }); inspectCommand - .description('inspect the Rsbuild / Rspack configs of Rslib projects') - .option( - '--output ', - 'specify inspect content output path', - '.rsbuild', - ) + .option('--output ', 'specify inspect content output path', { + default: '.rsbuild', + }) .option('--verbose', 'show full function definitions in output') .action(async (options: InspectOptions) => { try { @@ -116,27 +120,26 @@ export function runCli(): void { } }); - mfDevCommand - .description('start Rsbuild dev server of Module Federation format') - .action(async (options: CommonOptions) => { - try { - const cliMfDev = async () => { - const { config, watchFiles } = await init(options); - await startMFDevServer(config, { - lib: options.lib, - }); + mfDevCommand.action(async (options: CommonOptions) => { + try { + const cliMfDev = async () => { + const { config, watchFiles } = await init(options); + await startMFDevServer(config, { + lib: options.lib, + }); - watchFilesForRestart(watchFiles, async () => { - await cliMfDev(); - }); - }; + watchFilesForRestart(watchFiles, async () => { + await cliMfDev(); + }); + }; - await cliMfDev(); - } catch (err) { - logger.error('Failed to start mf-dev.'); - logger.error(err); - process.exit(1); - } - }); - program.parse(); + await cliMfDev(); + } catch (err) { + logger.error('Failed to start mf-dev.'); + logger.error(err); + process.exit(1); + } + }); + + cli.parse(); } diff --git a/packages/core/src/cli/mf.ts b/packages/core/src/cli/mf.ts index 49fa6346c..832ea5e69 100644 --- a/packages/core/src/cli/mf.ts +++ b/packages/core/src/cli/mf.ts @@ -23,7 +23,7 @@ async function initMFRsbuild( const selectedEnvironmentIds = environmentWithInfos .filter((env) => { const isMf = env.format === 'mf'; - if (!options?.lib) { + if (!options?.lib || options.lib.length === 0) { return isMf; } return env.id && options.lib.includes(env.id); diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index a5ee65312..c8b82851a 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -1780,7 +1780,7 @@ export const pruneEnvironments = ( environments: Record, libs?: string[], ): Record => { - if (!libs) { + if (!libs || libs.length === 0) { return environments; } diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index 39c91b9a3..ac8556ea2 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -11,7 +11,6 @@ "module": "ESNext", "moduleResolution": "Bundler", "paths": { - "commander": ["./compiled/commander"], "chokidar": ["./compiled/chokidar"], "picocolors": ["./compiled/picocolors"], "rslog": ["./compiled/rslog"] diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 45b90a64d..4be8c14e0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -352,12 +352,12 @@ importers: '@types/fs-extra': specifier: ^11.0.4 version: 11.0.4 + cac: + specifier: ^6.7.14 + version: 6.7.14 chokidar: specifier: ^4.0.3 version: 4.0.3 - commander: - specifier: ^14.0.0 - version: 14.0.0 fs-extra: specifier: ^11.3.0 version: 11.3.0 @@ -3784,10 +3784,6 @@ packages: resolution: {integrity: sha512-Vw8qHK3bZM9y/P10u3Vib8o/DdkvA2OtPtZvD871QKjy74Wj1WSKFILMPRPSdUSx5RFK1arlJzEtA4PkFgnbuA==} engines: {node: '>=18'} - commander@14.0.0: - resolution: {integrity: sha512-2uM9rYjPvyq39NwLRqaiLtWHyDC1FvryJDa2ATTVims5YAS4PupsEQsDvP14FqhFr0P49CYDugi59xaxJlTXRA==} - engines: {node: '>=20'} - commander@2.20.3: resolution: {integrity: sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==} @@ -10640,8 +10636,6 @@ snapshots: commander@12.1.0: {} - commander@14.0.0: {} - commander@2.20.3: {} commander@4.1.1: {}