Skip to content

Commit 25faba7

Browse files
committed
cr
1 parent e1cfaff commit 25faba7

File tree

8 files changed

+46
-53
lines changed

8 files changed

+46
-53
lines changed

packages/core/src/cli/commands.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { LogLevel, RsbuildMode } from '@rsbuild/core';
22
import cac, { type CAC } from 'cac';
33
import type { ConfigLoader } from '../config';
4-
import type { Format } from '../types/config';
4+
import type { Format, Syntax } from '../types/config';
55
import { logger } from '../utils/logger';
66
import { build } from './build';
77
import { initConfig } from './initConfig';
@@ -17,11 +17,15 @@ export type CommonOptions = {
1717
lib?: string[];
1818
configLoader?: ConfigLoader;
1919
logLevel?: LogLevel;
20+
};
21+
22+
export type BuildOptions = CommonOptions & {
23+
watch?: boolean;
2024
format?: Format;
2125
entry?: string[];
2226
distPath?: string;
2327
bundle?: boolean;
24-
syntax?: string;
28+
syntax?: Syntax;
2529
target?: string;
2630
dts?: boolean;
2731
externals?: string[];
@@ -32,10 +36,6 @@ export type CommonOptions = {
3236
tsconfig?: string;
3337
};
3438

35-
export type BuildOptions = CommonOptions & {
36-
watch?: boolean;
37-
};
38-
3939
export type InspectOptions = CommonOptions & {
4040
mode?: RsbuildMode;
4141
output?: string;

packages/core/src/cli/initConfig.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ import path from 'node:path';
22
import util from 'node:util';
33
import { loadEnv, type RsbuildEntry } from '@rsbuild/core';
44
import { loadConfig } from '../config';
5-
import type {
6-
EcmaScriptVersion,
7-
RsbuildConfigOutputTarget,
8-
RslibConfig,
9-
Syntax,
10-
} from '../types';
5+
import type { RsbuildConfigOutputTarget, RslibConfig } from '../types';
116
import { getAbsolutePath } from '../utils/helper';
127
import { logger } from '../utils/logger';
13-
import type { CommonOptions } from './commands';
8+
import type { BuildOptions, CommonOptions } from './commands';
149
import { onBeforeRestart } from './restart';
1510

1611
const getEnvDir = (cwd: string, envDir?: string) => {
@@ -83,7 +78,7 @@ export const parseEntryOption = (
8378

8479
export const applyCliOptions = (
8580
config: RslibConfig,
86-
options: CommonOptions,
81+
options: BuildOptions,
8782
root: string,
8883
): void => {
8984
if (options.root) config.root = root;
@@ -101,7 +96,7 @@ export const applyCliOptions = (
10196
lib.source ||= {};
10297
lib.source.entry = entry as RsbuildEntry;
10398
}
104-
const syntax = options.syntax as Syntax | undefined;
99+
const syntax = options.syntax;
105100
if (syntax !== undefined) lib.syntax = syntax;
106101
if (options.dts !== undefined) lib.dts = options.dts;
107102
if (options.autoExtension !== undefined)

packages/core/tests/cli.test.ts

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { describe, expect, test } from '@rstest/core';
22
import type { CommonOptions } from '../src/cli/commands';
3-
import {
4-
applyCliOptions,
5-
parseEntryOption,
6-
parseSyntaxOption,
7-
} from '../src/cli/initConfig';
3+
import { applyCliOptions, parseEntryOption } from '../src/cli/initConfig';
84
import type { RslibConfig } from '../src/types';
95

106
describe('parseEntryOption', () => {
@@ -31,32 +27,6 @@ describe('parseEntryOption', () => {
3127
});
3228
});
3329

34-
describe('parseSyntaxOption', () => {
35-
test('returns undefined for missing or whitespace values', () => {
36-
expect(parseSyntaxOption()).toBeUndefined();
37-
expect(parseSyntaxOption('')).toBeUndefined();
38-
expect(parseSyntaxOption(' ')).toBeUndefined();
39-
});
40-
41-
test('returns the trimmed ECMAScript version when not a JSON array', () => {
42-
expect(parseSyntaxOption(' es2020 ')).toBe('es2020');
43-
});
44-
45-
test('parses JSON array syntax', () => {
46-
expect(parseSyntaxOption('["chrome 120", "firefox 115"]')).toEqual([
47-
'chrome 120',
48-
'firefox 115',
49-
]);
50-
});
51-
52-
test('throws descriptive error when JSON parsing fails', () => {
53-
const parseInvalidSyntax = () => parseSyntaxOption('[invalid');
54-
expect(parseInvalidSyntax).toThrowError(
55-
/Failed to parse --syntax option "\[inv.*JSON array/,
56-
);
57-
});
58-
});
59-
6030
describe('applyCliOptions', () => {
6131
test('applies CLI flags to the config and its libraries', () => {
6232
const config = {
@@ -98,7 +68,7 @@ describe('applyCliOptions', () => {
9868
bundle: false,
9969
tsconfig: './tsconfig.build.json',
10070
entry: [' main=src/main.ts ', ' src/utils.ts ', 'src/extra.ts'],
101-
syntax: '["node 18"]',
71+
syntax: ['node 18'],
10272
dts: true,
10373
autoExtension: false,
10474
autoExternal: false,

packages/core/tests/config.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { join } from 'node:path';
22
import { pluginModuleFederation } from '@module-federation/rsbuild-plugin';
33
import { inspect } from '@rslib/core';
44
import { describe, expect, rs, test } from '@rstest/core';
5+
import type { BuildOptions } from '../src/cli/commands';
56
import { initConfig } from '../src/cli/initConfig';
67
import {
78
composeCreateRsbuildConfig,
@@ -155,7 +156,7 @@ describe('CLI options', () => {
155156
const fixtureDir = join(__dirname, 'fixtures/config/cli-options');
156157
const configFilePath = join(fixtureDir, 'rslib.config.ts');
157158

158-
const { config } = await initConfig({
159+
const options: BuildOptions = {
159160
config: configFilePath,
160161
entry: ['index=src/main.ts', 'utils=src/utils.ts'],
161162
distPath: 'build',
@@ -169,8 +170,9 @@ describe('CLI options', () => {
169170
autoExtension: false,
170171
autoExternal: false,
171172
tsconfig: 'tsconfig.build.json',
172-
});
173+
};
173174

175+
const { config } = await initConfig(options);
174176
expect(config).toMatchInlineSnapshot(`
175177
{
176178
"_privateMeta": {

tests/integration/cli/build/build.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os from 'node:os';
21
import path from 'node:path';
32
import { stripVTControlCharacters as stripAnsi } from 'node:util';
43
import { describe, expect, test } from '@rstest/core';

tests/integration/preserve-jsx/index.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { join } from 'node:path';
2+
import { stripVTControlCharacters as stripAnsi } from 'node:util';
23
import { expect, test } from '@rstest/core';
34
import { buildAndGetResults, proxyConsole, queryContent } from 'test-helper';
45

@@ -48,7 +49,7 @@ test('throw error when preserve JSX with bundle mode', async () => {
4849
try {
4950
await buildAndGetResults({ fixturePath });
5051
} catch {
51-
expect(logs).toMatchInlineSnapshot(`
52+
expect(logs.map((l) => stripAnsi(l))).toMatchInlineSnapshot(`
5253
[
5354
"error Bundle mode does not support preserving JSX syntax. Set "bundle" to "false" or change the JSX runtime to \`automatic\` or \`classic\`. Check out https://rslib.rs/guide/solution/react#jsx-transform for more details.",
5455
]

website/docs/en/guide/basic/cli.mdx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@ Usage:
4646
$ rslib build
4747

4848
Options:
49-
-w --watch turn on watch mode, watch for changes and rebuild
49+
-w, --watch turn on watch mode, watch for changes and rebuild
50+
--entry <entry> set entry file or pattern (repeatable)
51+
--dist-path <dir> set output directory
52+
--bundle enable bundle mode (use --no-bundle to disable)
53+
--format <format> specify the output format (esm | cjs | umd | mf | iife)
54+
--syntax <syntax> set build syntax target (repeatable)
55+
--target <target> set runtime target (web | node)
56+
--dts emit declaration files (use --no-dts to disable)
57+
--externals <pkg> add package to externals (repeatable)
58+
--minify minify output (use --no-minify to disable)
59+
--clean clean output directory before build (use --no-clean to disable)
60+
--auto-extension control automatic extension redirect (use --no-auto-extension to disable)
61+
--auto-external control automatic dependency externalization (use --no-auto-external to disable)
62+
--tsconfig <path> use specific tsconfig (relative to project root)
5063
```
5164

5265
### Environment variables

website/docs/zh/guide/basic/cli.mdx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@ Usage:
4646
$ rslib build
4747

4848
Options:
49-
-w --watch 开启 watch 模式, 监听文件变更并重新构建
49+
-w, --watch 开启 watch 模式,监听文件变更并重新构建
50+
--entry <entry> 设置入口文件或匹配模式(可重复传递)
51+
--dist-path <dir> 设置输出目录
52+
--bundle 启用 bundle 模式(使用 --no-bundle 禁用)
53+
--format <format> 指定输出格式(esm | cjs | umd | mf | iife)
54+
--syntax <syntax> 指定构建语法目标(可重复传递)
55+
--target <target> 指定运行时目标(web | node)
56+
--dts 生成声明文件(使用 --no-dts 禁用)
57+
--externals <pkg> 将依赖加入 externals(可重复传递)
58+
--minify 压缩输出(使用 --no-minify 禁用)
59+
--clean 构建前清理输出目录(使用 --no-clean 禁用)
60+
--auto-extension 控制自动补全扩展名重定向(使用 --no-auto-extension 禁用)
61+
--auto-external 控制自动外部化依赖(使用 --no-auto-external 禁用)
62+
--tsconfig <path> 使用指定的 tsconfig(相对于项目根目录)
5063
```
5164

5265
### 环境变量

0 commit comments

Comments
 (0)