Skip to content

Commit e535020

Browse files
committed
feat: basic preview command support
1 parent 87c3eed commit e535020

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/cli/commands.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cac from 'cac';
22
import { logger } from '@rsbuild/core';
33
import { initRsbuild } from './rsbuild.js';
4-
import type { BuildOptions, DevOptions } from './types.js';
4+
import type { BuildOptions, DevOptions, PreviewOptions } from './types.js';
55

66
declare global {
77
const RSTACK_VERSION: string;
@@ -17,9 +17,11 @@ export function setupCommands(): void {
1717
const rsbuild = await initRsbuild({
1818
options,
1919
});
20+
2021
if (!rsbuild) {
2122
return;
2223
}
24+
2325
await rsbuild.startDevServer();
2426
} catch (err) {
2527
logger.error('Failed to start dev server.');
@@ -40,6 +42,7 @@ export function setupCommands(): void {
4042
const rsbuild = await initRsbuild({
4143
options,
4244
});
45+
4346
if (!rsbuild) {
4447
return;
4548
}
@@ -67,5 +70,25 @@ export function setupCommands(): void {
6770
}
6871
});
6972

73+
cli
74+
.command('preview', 'Preview the production build locally via Rsbuild')
75+
.action(async (options: PreviewOptions) => {
76+
try {
77+
const rsbuild = await initRsbuild({
78+
options,
79+
});
80+
81+
if (!rsbuild) {
82+
return;
83+
}
84+
85+
await rsbuild.preview();
86+
} catch (err) {
87+
logger.error('Failed to start preview server.');
88+
logger.error(err);
89+
process.exit(1);
90+
}
91+
});
92+
7093
cli.parse();
7194
}

src/cli/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ export type BuildOptions = {
88
};
99

1010
export type DevOptions = RsbuildCommonOptions;
11+
12+
export type PreviewOptions = RsbuildCommonOptions;

0 commit comments

Comments
 (0)