Skip to content

Commit f4a2c10

Browse files
authored
perf: replace commander with cac (#1012)
1 parent 129d69e commit f4a2c10

File tree

8 files changed

+53
-59
lines changed

8 files changed

+53
-59
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
"@module-federation/rsbuild-plugin": "^0.14.0",
5151
"@rslib/tsconfig": "workspace:*",
5252
"@types/fs-extra": "^11.0.4",
53+
"cac": "^6.7.14",
5354
"chokidar": "^4.0.3",
54-
"commander": "^14.0.0",
5555
"fs-extra": "^11.3.0",
5656
"memfs": "^4.17.2",
5757
"picocolors": "1.1.1",

packages/core/prebundle.config.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export default {
1212
typescript: 'typescript',
1313
},
1414
dependencies: [
15-
'commander',
1615
{
1716
name: 'chokidar',
1817
// strip sourcemap comment

packages/core/rslib.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ export default defineConfig({
5050
output: {
5151
externals: {
5252
picocolors: '../compiled/picocolors/index.js',
53-
commander: '../compiled/commander/index.js',
5453
chokidar: '../compiled/chokidar/index.js',
5554
rslog: '../compiled/rslog/index.js',
5655
},

packages/core/src/cli/commands.ts

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RsbuildMode } from '@rsbuild/core';
2-
import { type Command, program } from 'commander';
2+
import cac, { type CAC } from 'cac';
33
import { logger } from '../utils/logger';
44
import { build } from './build';
55
import { init } from './init';
@@ -25,14 +25,14 @@ export type InspectOptions = CommonOptions & {
2525
verbose?: boolean;
2626
};
2727

28-
const applyCommonOptions = (command: Command) => {
29-
command
28+
const applyCommonOptions = (cli: CAC) => {
29+
cli
3030
.option(
31-
'-c --config <config>',
31+
'-c, --config <config>',
3232
'specify the configuration file, can be a relative or absolute path',
3333
)
3434
.option(
35-
'-r --root <root>',
35+
'-r, --root <root>',
3636
'specify the project root directory, can be an absolute path or a path relative to cwd',
3737
)
3838
.option(
@@ -43,26 +43,33 @@ const applyCommonOptions = (command: Command) => {
4343
.option(
4444
'--lib <id>',
4545
'specify the library (repeatable, e.g. --lib esm --lib cjs)',
46-
repeatableOption,
46+
{
47+
type: [String],
48+
default: [],
49+
},
4750
);
4851
};
4952

50-
const repeatableOption = (value: string, previous: string[]) => {
51-
return (previous ?? []).concat([value]);
52-
};
53-
5453
export function runCli(): void {
55-
program.name('rslib').usage('<command> [options]').version(RSLIB_VERSION);
54+
const cli = cac('rslib');
55+
56+
cli.help();
57+
cli.version(RSLIB_VERSION);
5658

57-
const buildCommand = program.command('build');
58-
const inspectCommand = program.command('inspect');
59-
const mfDevCommand = program.command('mf-dev');
59+
applyCommonOptions(cli);
6060

61-
[buildCommand, inspectCommand, mfDevCommand].forEach(applyCommonOptions);
61+
const buildCommand = cli.command('build', 'build the library for production');
62+
const inspectCommand = cli.command(
63+
'inspect',
64+
'inspect the Rsbuild / Rspack configs of Rslib projects',
65+
);
66+
const mfDevCommand = cli.command(
67+
'mf-dev',
68+
'start Rsbuild dev server of Module Federation format',
69+
);
6270

6371
buildCommand
64-
.option('-w --watch', 'turn on watch mode, watch for changes and rebuild')
65-
.description('build the library for production')
72+
.option('-w, --watch', 'turn on watch mode, watch for changes and rebuild')
6673
.action(async (options: BuildOptions) => {
6774
try {
6875
const cliBuild = async () => {
@@ -92,12 +99,9 @@ export function runCli(): void {
9299
});
93100

94101
inspectCommand
95-
.description('inspect the Rsbuild / Rspack configs of Rslib projects')
96-
.option(
97-
'--output <output>',
98-
'specify inspect content output path',
99-
'.rsbuild',
100-
)
102+
.option('--output <output>', 'specify inspect content output path', {
103+
default: '.rsbuild',
104+
})
101105
.option('--verbose', 'show full function definitions in output')
102106
.action(async (options: InspectOptions) => {
103107
try {
@@ -116,27 +120,26 @@ export function runCli(): void {
116120
}
117121
});
118122

119-
mfDevCommand
120-
.description('start Rsbuild dev server of Module Federation format')
121-
.action(async (options: CommonOptions) => {
122-
try {
123-
const cliMfDev = async () => {
124-
const { config, watchFiles } = await init(options);
125-
await startMFDevServer(config, {
126-
lib: options.lib,
127-
});
123+
mfDevCommand.action(async (options: CommonOptions) => {
124+
try {
125+
const cliMfDev = async () => {
126+
const { config, watchFiles } = await init(options);
127+
await startMFDevServer(config, {
128+
lib: options.lib,
129+
});
128130

129-
watchFilesForRestart(watchFiles, async () => {
130-
await cliMfDev();
131-
});
132-
};
131+
watchFilesForRestart(watchFiles, async () => {
132+
await cliMfDev();
133+
});
134+
};
133135

134-
await cliMfDev();
135-
} catch (err) {
136-
logger.error('Failed to start mf-dev.');
137-
logger.error(err);
138-
process.exit(1);
139-
}
140-
});
141-
program.parse();
136+
await cliMfDev();
137+
} catch (err) {
138+
logger.error('Failed to start mf-dev.');
139+
logger.error(err);
140+
process.exit(1);
141+
}
142+
});
143+
144+
cli.parse();
142145
}

packages/core/src/cli/mf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async function initMFRsbuild(
2323
const selectedEnvironmentIds = environmentWithInfos
2424
.filter((env) => {
2525
const isMf = env.format === 'mf';
26-
if (!options?.lib) {
26+
if (!options?.lib || options.lib.length === 0) {
2727
return isMf;
2828
}
2929
return env.id && options.lib.includes(env.id);

packages/core/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ export const pruneEnvironments = (
17801780
environments: Record<string, EnvironmentConfig>,
17811781
libs?: string[],
17821782
): Record<string, EnvironmentConfig> => {
1783-
if (!libs) {
1783+
if (!libs || libs.length === 0) {
17841784
return environments;
17851785
}
17861786

packages/core/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"module": "ESNext",
1212
"moduleResolution": "Bundler",
1313
"paths": {
14-
"commander": ["./compiled/commander"],
1514
"chokidar": ["./compiled/chokidar"],
1615
"picocolors": ["./compiled/picocolors"],
1716
"rslog": ["./compiled/rslog"]

pnpm-lock.yaml

Lines changed: 3 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)