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
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@
"json.schemas": [
{
"fileMatch": ["**/_meta.json"],
"url": "./website/node_modules/rspress/meta-json-schema.json"
"url": "./website/node_modules/@rspress/core/meta-json-schema.json"
},
{
"fileMatch": ["**/_nav.json"],
"url": "./website/node_modules/rspress/nav-json-schema.json"
"url": "./website/node_modules/@rspress/core/nav-json-schema.json"
}
]
}
2 changes: 2 additions & 0 deletions packages/core/src/cli/build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createRsbuild, type RsbuildInstance } from '@rsbuild/core';
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
import type { RslibConfig } from '../types/config';
import { isDebug } from '../utils/logger';
import type { BuildOptions } from './commands';
import { onBeforeRestart } from './restart';

Expand All @@ -17,6 +18,7 @@ export async function build(
plugins: config.plugins,
dev: config.dev,
server: config.server,
logLevel: isDebug() ? 'info' : config.logLevel,
environments: pruneEnvironments(environments, options.lib),
},
});
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RsbuildMode } from '@rsbuild/core';
import type { LogLevel, RsbuildMode } from '@rsbuild/core';
import cac, { type CAC } from 'cac';
import type { ConfigLoader } from '../config';
import { logger } from '../utils/logger';
Expand All @@ -15,6 +15,7 @@ export type CommonOptions = {
envMode?: string;
lib?: string[];
configLoader?: ConfigLoader;
logLevel?: LogLevel;
};

export type BuildOptions = CommonOptions & {
Expand Down Expand Up @@ -49,6 +50,10 @@ const applyCommonOptions = (cli: CAC) => {
},
)
.option('--env-dir <dir>', 'specify the directory to load `.env` files')
.option(
'--log-level <level>',
'set the log level (info | warn | error | silent)',
)
.option(
'--lib <id>',
'specify the library (repeatable, e.g. --lib esm --lib cjs)',
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export async function init(options: CommonOptions): Promise<{
config.root = root;
}

if (options.logLevel) {
config.logLevel = options.logLevel;
}

return {
config,
configFilePath,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/cli/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createRsbuild, type RsbuildInstance } from '@rsbuild/core';
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
import type { RslibConfig } from '../types/config';
import { isDebug } from '../utils/logger';
import type { InspectOptions } from './commands';

export async function inspect(
Expand All @@ -16,6 +17,7 @@ export async function inspect(
plugins: config.plugins,
dev: config.dev,
server: config.server,
logLevel: isDebug() ? 'info' : config.logLevel,
environments: pruneEnvironments(environments, options.lib),
},
});
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/cli/mf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { RsbuildInstance } from '@rsbuild/core';
import { createRsbuild } from '@rsbuild/core';
import { composeRsbuildEnvironments, pruneEnvironments } from '../config';
import type { RslibConfig } from '../types';
import { isDebug } from '../utils/logger';
import type { CommonOptions } from './commands';
import { onBeforeRestart } from './restart';

Expand Down Expand Up @@ -53,6 +54,7 @@ async function initMFRsbuild(
plugins: config.plugins,
dev: config.dev,
server: config.server,
logLevel: isDebug() ? 'info' : config.logLevel,
environments: selectedEnvironments,
},
});
Expand Down
19 changes: 17 additions & 2 deletions packages/core/src/cli/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '../utils/logger';
import type { LogLevel } from '@rsbuild/core';
import { isDebug, logger } from '../utils/logger';

function initNodeEnv() {
if (!process.env.NODE_ENV) {
Expand All @@ -9,8 +10,22 @@ function initNodeEnv() {
}
}

// ensure log level is set before any log is printed
function setupLogLevel() {
const logLevelIndex = process.argv.findIndex(
(item) => item === '--log-level' || item === '--logLevel',
);
if (logLevelIndex !== -1) {
const level = process.argv[logLevelIndex + 1];
if (level && ['warn', 'error', 'silent'].includes(level) && !isDebug()) {
logger.level = level as LogLevel;
}
}
}

export function prepareCli(): void {
initNodeEnv();
setupLogLevel();

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

logger.greet(` Rslib v${RSLIB_VERSION}\n`);
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ import {
pick,
readPackageJson,
} from './utils/helper';
import { logger } from './utils/logger';
import { isDebug, logger } from './utils/logger';
import {
ESX_TO_BROWSERSLIST,
transformSyntaxToBrowserslist,
Expand Down Expand Up @@ -1804,8 +1804,13 @@ export async function composeCreateRsbuildConfig(
plugins: sharedPlugins,
dev: _dev,
server: _server,
logLevel,
...sharedRsbuildConfig
} = rslibConfig;
// debug mode should always verbose logs
if (logLevel && !isDebug()) {
logger.level = logLevel;
}

if (!Array.isArray(libConfigsArray) || libConfigsArray.length === 0) {
throw new Error(
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export const isDebug = (): boolean => {
}

const values = process.env.DEBUG.toLocaleLowerCase().split(',');
return ['rslib', 'rs*', 'rstack', '*'].some((key) => values.includes(key));
return ['rslib', 'rsbuild', 'rs*', 'rstack', '*'].some((key) =>
values.includes(key),
);
};

// setup the logger level
Expand Down
3 changes: 2 additions & 1 deletion packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
name: 'rsbuild:nonce',
setup() {}
}
]
],
logLevel: undefined
}"
`;

Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-dts/src/dts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
extension: false,
},
tsgo,
loggerLevel,
} = data;
logger.level = loggerLevel;

if (!isWatch) {
logger.start(`generating declaration files... ${color.dim(`(${name})`)}`);
}
Expand Down
12 changes: 11 additions & 1 deletion packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { type ChildProcess, fork } from 'node:child_process';
import { dirname, extname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { logger, type RsbuildConfig, type RsbuildPlugin } from '@rsbuild/core';
import {
type LogLevel,
logger,
type RsbuildConfig,
type RsbuildPlugin,
} from '@rsbuild/core';
import color from 'picocolors';
import type { ParsedCommandLine } from 'typescript';

Expand Down Expand Up @@ -66,6 +71,7 @@ export type DtsGenOptions = Omit<PluginDtsOptions, 'bundle'> & {
tsConfigResult: ParsedCommandLine;
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
apiExtractorOptions?: ApiExtractorOptions;
loggerLevel: LogLevel;
};

interface TaskResult {
Expand All @@ -81,6 +87,9 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
name: PLUGIN_DTS_NAME,

setup(api) {
const loggerLevel = api.logger.level;
logger.level = loggerLevel;

let apiExtractorOptions = {};

if (options.bundle && typeof options.bundle === 'object') {
Expand Down Expand Up @@ -179,6 +188,7 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
name: environment.name,
cwd,
isWatch,
loggerLevel: loggerLevel as LogLevel,
};

childProcess.send(dtsGenOptions);
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

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

42 changes: 42 additions & 0 deletions tests/integration/cli/log-level/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe } from 'node:test';
import { stripVTControlCharacters as stripAnsi } from 'node:util';
import { expect, test } from '@rstest/core';
import { runCliSync } from 'test-helper';

describe('log level', async () => {
test('should run build command with log level: info', async () => {
const stdout = stripAnsi(
runCliSync('build --log-level info', {
cwd: __dirname,
}).toString(),
);
expect(stdout).toContain('Rslib v');
expect(stdout).toContain('build started...');
expect(stdout).toContain('built in');
});

test('should run build command with log level: warn', async () => {
const stdout = stripAnsi(
runCliSync('build --log-level warn', {
cwd: __dirname,
}).toString(),
);
expect(stdout).not.toContain('Rslib v');
expect(stdout).not.toContain('build started...');
expect(stdout).not.toContain('built in');
});

test('should always print verbose logs when debug mode is enabled', async () => {
const stdout = stripAnsi(
runCliSync('build --log-level warn', {
cwd: __dirname,
env: {
...process.env,
DEBUG: 'rsbuild',
},
}).toString(),
);
expect(stdout).toContain('creating compiler');
expect(stdout).toContain('config inspection completed');
});
});
6 changes: 6 additions & 0 deletions tests/integration/cli/log-level/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "cli-log-level-test",
"version": "1.0.0",
"private": true,
"type": "module"
}
6 changes: 6 additions & 0 deletions tests/integration/cli/log-level/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from '@rslib/core';
import { generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [generateBundleEsmConfig()],
});
1 change: 1 addition & 0 deletions tests/integration/cli/log-level/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
5 changes: 5 additions & 0 deletions website/docs/en/config/rsbuild/_meta.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"type": "file",
"name": "log-level",
"label": "logLevel"
},
"resolve",
"source",
"output",
Expand Down
2 changes: 1 addition & 1 deletion website/docs/en/config/rsbuild/index.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { RsbuildDocBadge } from '@components/RsbuildDocBadge';
import { Overview } from 'rspress/theme';

# Rsbuild configurations

Expand All @@ -11,6 +10,7 @@ To learn more about Rslib configurations, check out [Configure Rslib](/guide/bas

## Overview

- [logLevel](/config/rsbuild/log-level): Specify the log level.
- [resolve](/config/rsbuild/resolve): Options for module resolution.
- [source](/config/rsbuild/source): Options for input source code.
- [output](/config/rsbuild/output): Options for build outputs.
Expand Down
8 changes: 8 additions & 0 deletions website/docs/en/config/rsbuild/log-level.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RsbuildDocBadge } from '@components/RsbuildDocBadge';

# logLevel <RsbuildDocBadge path="/config/log-level" text="logLevel" />

- **Type:** `'info' | 'warn' | 'error' | 'silent'`
- **Default:** `'info'`

Specify the log level, the default value is `info`.
1 change: 1 addition & 0 deletions website/docs/en/guide/basic/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Rslib CLI provides several common flags that can be used with all commands:
| `--env-mode <mode>` | Specify the env mode to load the `.env.[mode]` file, see [Rsbuild - Env mode](https://rsbuild.rs/guide/advanced/env-vars#env-mode) |
| `-h, --help` | Display help for command |
| `--lib <id>` | Specify the library to run commands (repeatable, e.g. `--lib esm --lib cjs`), see [lib.id](/config/lib/id) to learn how to get or set the ID of the library |
| `--log-level <level>` | Set the log level (`info` \| `warn` \| `error` \| `silent`), see [logLevel](/config/rsbuild/log-level) |
| `-r, --root <root>` | Specify the project root directory, can be an absolute path or a path relative to cwd |

## rslib build
Expand Down
5 changes: 5 additions & 0 deletions website/docs/zh/config/rsbuild/_meta.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"type": "file",
"name": "log-level",
"label": "logLevel"
},
"resolve",
"source",
"output",
Expand Down
1 change: 1 addition & 0 deletions website/docs/zh/config/rsbuild/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Rslib 继承了 Rsbuild 的配置,所以你也可以配置 <RsbuildDocBadge pa

## 概览

- [logLevel](/config/rsbuild/log-level): 指定日志级别。
- [resolve](/config/rsbuild/resolve): 与模块解析相关的选项。
- [source](/config/rsbuild/source): 与输入的源代码相关的选项。
- [output](/config/rsbuild/output): 与构建产物相关的选项。
Expand Down
8 changes: 8 additions & 0 deletions website/docs/zh/config/rsbuild/log-level.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { RsbuildDocBadge } from '@components/RsbuildDocBadge';

# logLevel <RsbuildDocBadge path="/config/log-level" text="logLevel" />

- **类型:** `'info' | 'warn' | 'error' | 'silent'`
- **默认值:** `'info'`

指定日志级别,默认值为 `info`。
1 change: 1 addition & 0 deletions website/docs/zh/guide/basic/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Rslib CLI 提供了一些公共选项,可以用于所有命令:
| `--env-mode <mode>` | 指定 env 模式来加载 `.env.[mode]` 文件,详见 [Rsbuild - Env 模式](https://rsbuild.rs/zh/guide/advanced/env-vars#env-模式) |
| `-h, --help` | 显示命令帮助 |
| `--lib <id>` | 指定运行命令的库(可重复,例如:`--lib esm --lib cjs`),查看 [lib.id](/config/lib/id) 了解如何获取或设置库的 ID |
| `--log-level <level>` | 指定日志级别(`info` \| `warn` \| `error` \| `silent`),详见 [logLevel](/config/rsbuild/log-level) |
| `-r, --root <root>` | 指定项目根目录,可以是绝对路径或者相对于 cwd 的路径 |

## rslib build
Expand Down
Loading