Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion packages/core/prebundle.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export default {
typescript: 'typescript',
},
dependencies: [
'commander',
{
name: 'chokidar',
// strip sourcemap comment
Expand Down
1 change: 0 additions & 1 deletion packages/core/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down
91 changes: 47 additions & 44 deletions packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -25,14 +25,14 @@ export type InspectOptions = CommonOptions & {
verbose?: boolean;
};

const applyCommonOptions = (command: Command) => {
command
const applyCommonOptions = (cli: CAC) => {
cli
.option(
'-c --config <config>',
'-c, --config <config>',
'specify the configuration file, can be a relative or absolute path',
)
.option(
'-r --root <root>',
'-r, --root <root>',
'specify the project root directory, can be an absolute path or a path relative to cwd',
)
.option(
Expand All @@ -43,26 +43,33 @@ const applyCommonOptions = (command: Command) => {
.option(
'--lib <id>',
'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('<command> [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 () => {
Expand Down Expand Up @@ -92,12 +99,9 @@ export function runCli(): void {
});

inspectCommand
.description('inspect the Rsbuild / Rspack configs of Rslib projects')
.option(
'--output <output>',
'specify inspect content output path',
'.rsbuild',
)
.option('--output <output>', 'specify inspect content output path', {
default: '.rsbuild',
})
.option('--verbose', 'show full function definitions in output')
.action(async (options: InspectOptions) => {
try {
Expand All @@ -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();
}
2 changes: 1 addition & 1 deletion packages/core/src/cli/mf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1780,7 +1780,7 @@ export const pruneEnvironments = (
environments: Record<string, EnvironmentConfig>,
libs?: string[],
): Record<string, EnvironmentConfig> => {
if (!libs) {
if (!libs || libs.length === 0) {
return environments;
}

Expand Down
1 change: 0 additions & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"module": "ESNext",
"moduleResolution": "Bundler",
"paths": {
"commander": ["./compiled/commander"],
"chokidar": ["./compiled/chokidar"],
"picocolors": ["./compiled/picocolors"],
"rslog": ["./compiled/rslog"]
Expand Down
12 changes: 3 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading