Skip to content

Commit de065fa

Browse files
authored
feat(CLI): support logLevel (#1238)
1 parent 86fe688 commit de065fa

File tree

25 files changed

+147
-10
lines changed

25 files changed

+147
-10
lines changed

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@
5757
"json.schemas": [
5858
{
5959
"fileMatch": ["**/_meta.json"],
60-
"url": "./website/node_modules/rspress/meta-json-schema.json"
60+
"url": "./website/node_modules/@rspress/core/meta-json-schema.json"
6161
},
6262
{
6363
"fileMatch": ["**/_nav.json"],
64-
"url": "./website/node_modules/rspress/nav-json-schema.json"
64+
"url": "./website/node_modules/@rspress/core/nav-json-schema.json"
6565
}
6666
]
6767
}

packages/core/src/cli/build.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createRsbuild, type RsbuildInstance } from '@rsbuild/core';
22
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
33
import type { RslibConfig } from '../types/config';
4+
import { isDebug } from '../utils/logger';
45
import type { BuildOptions } from './commands';
56
import { onBeforeRestart } from './restart';
67

@@ -17,6 +18,7 @@ export async function build(
1718
plugins: config.plugins,
1819
dev: config.dev,
1920
server: config.server,
21+
logLevel: isDebug() ? 'info' : config.logLevel,
2022
environments: pruneEnvironments(environments, options.lib),
2123
},
2224
});

packages/core/src/cli/commands.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { RsbuildMode } from '@rsbuild/core';
1+
import type { LogLevel, RsbuildMode } from '@rsbuild/core';
22
import cac, { type CAC } from 'cac';
33
import type { ConfigLoader } from '../config';
44
import { logger } from '../utils/logger';
@@ -15,6 +15,7 @@ export type CommonOptions = {
1515
envMode?: string;
1616
lib?: string[];
1717
configLoader?: ConfigLoader;
18+
logLevel?: LogLevel;
1819
};
1920

2021
export type BuildOptions = CommonOptions & {
@@ -49,6 +50,10 @@ const applyCommonOptions = (cli: CAC) => {
4950
},
5051
)
5152
.option('--env-dir <dir>', 'specify the directory to load `.env` files')
53+
.option(
54+
'--log-level <level>',
55+
'set the log level (info | warn | error | silent)',
56+
)
5257
.option(
5358
'--lib <id>',
5459
'specify the library (repeatable, e.g. --lib esm --lib cjs)',

packages/core/src/cli/init.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export async function init(options: CommonOptions): Promise<{
4444
config.root = root;
4545
}
4646

47+
if (options.logLevel) {
48+
config.logLevel = options.logLevel;
49+
}
50+
4751
return {
4852
config,
4953
configFilePath,

packages/core/src/cli/inspect.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createRsbuild, type RsbuildInstance } from '@rsbuild/core';
22
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
33
import type { RslibConfig } from '../types/config';
4+
import { isDebug } from '../utils/logger';
45
import type { InspectOptions } from './commands';
56

67
export async function inspect(
@@ -16,6 +17,7 @@ export async function inspect(
1617
plugins: config.plugins,
1718
dev: config.dev,
1819
server: config.server,
20+
logLevel: isDebug() ? 'info' : config.logLevel,
1921
environments: pruneEnvironments(environments, options.lib),
2022
},
2123
});

packages/core/src/cli/mf.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { RsbuildInstance } from '@rsbuild/core';
22
import { createRsbuild } from '@rsbuild/core';
33
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
44
import type { RslibConfig } from '../types';
5+
import { isDebug } from '../utils/logger';
56
import type { CommonOptions } from './commands';
67
import { onBeforeRestart } from './restart';
78

@@ -53,6 +54,7 @@ async function initMFRsbuild(
5354
plugins: config.plugins,
5455
dev: config.dev,
5556
server: config.server,
57+
logLevel: isDebug() ? 'info' : config.logLevel,
5658
environments: selectedEnvironments,
5759
},
5860
});

packages/core/src/cli/prepare.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { logger } from '../utils/logger';
1+
import type { LogLevel } from '@rsbuild/core';
2+
import { isDebug, logger } from '../utils/logger';
23

34
function initNodeEnv() {
45
if (!process.env.NODE_ENV) {
@@ -9,8 +10,22 @@ function initNodeEnv() {
910
}
1011
}
1112

13+
// ensure log level is set before any log is printed
14+
function setupLogLevel() {
15+
const logLevelIndex = process.argv.findIndex(
16+
(item) => item === '--log-level' || item === '--logLevel',
17+
);
18+
if (logLevelIndex !== -1) {
19+
const level = process.argv[logLevelIndex + 1];
20+
if (level && ['warn', 'error', 'silent'].includes(level) && !isDebug()) {
21+
logger.level = level as LogLevel;
22+
}
23+
}
24+
}
25+
1226
export function prepareCli(): void {
1327
initNodeEnv();
28+
setupLogLevel();
1429

1530
// Print a blank line to keep the greet log nice.
1631
// Some package managers automatically output a blank line, some do not.
@@ -20,7 +35,7 @@ export function prepareCli(): void {
2035
npm_execpath.includes('npx-cli.js') ||
2136
npm_execpath.includes('.bun')
2237
) {
23-
console.log();
38+
logger.log();
2439
}
2540

2641
logger.greet(` Rslib v${RSLIB_VERSION}\n`);

packages/core/src/config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import {
7373
pick,
7474
readPackageJson,
7575
} from './utils/helper';
76-
import { logger } from './utils/logger';
76+
import { isDebug, logger } from './utils/logger';
7777
import {
7878
ESX_TO_BROWSERSLIST,
7979
transformSyntaxToBrowserslist,
@@ -1804,8 +1804,13 @@ export async function composeCreateRsbuildConfig(
18041804
plugins: sharedPlugins,
18051805
dev: _dev,
18061806
server: _server,
1807+
logLevel,
18071808
...sharedRsbuildConfig
18081809
} = rslibConfig;
1810+
// debug mode should always verbose logs
1811+
if (logLevel && !isDebug()) {
1812+
logger.level = logLevel;
1813+
}
18091814

18101815
if (!Array.isArray(libConfigsArray) || libConfigsArray.length === 0) {
18111816
throw new Error(

packages/core/src/utils/logger.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export const isDebug = (): boolean => {
2121
}
2222

2323
const values = process.env.DEBUG.toLocaleLowerCase().split(',');
24-
return ['rslib', 'rs*', 'rstack', '*'].some((key) => values.includes(key));
24+
return ['rslib', 'rsbuild', 'rs*', 'rstack', '*'].some((key) =>
25+
values.includes(key),
26+
);
2527
};
2628

2729
// setup the logger level

packages/core/tests/__snapshots__/config.test.ts.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
287287
name: 'rsbuild:nonce',
288288
setup() {}
289289
}
290-
]
290+
],
291+
logLevel: undefined
291292
}"
292293
`;
293294

0 commit comments

Comments
 (0)