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
23 changes: 23 additions & 0 deletions e2e/cases/plugin-api/logger/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { build } from '@e2e/helper';
import { expect, test } from '@playwright/test';
import { type Logger, logger, type RsbuildPlugin } from '@rsbuild/core';

test('should allow plugin to custom resolver', async () => {
let pluginLogger: Logger | undefined;

const loggerPlugin: RsbuildPlugin = {
name: 'logger-plugin',
setup(api) {
pluginLogger = api.logger;
},
};

await build({
cwd: __dirname,
rsbuildConfig: {
plugins: [loggerPlugin],
},
});

expect(pluginLogger).toEqual(logger);
});
1 change: 1 addition & 0 deletions e2e/cases/plugin-api/logger/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// empty
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export { PLUGIN_CSS_NAME, PLUGIN_SWC_NAME } from './constants';
export { defaultAllowedOrigins } from './defaultConfig';
export { ensureAssetPrefix } from './helpers';
// Helpers
export { logger } from './logger';
export { type Logger, logger } from './logger';
export { mergeRsbuildConfig } from './mergeConfig';
export type { RsbuildDevServer } from './server/devServer';
export type { StartServerResult } from './server/helper';
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/initPlugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ export function initPluginAPI({
return (environment?: string) => ({
context: publicContext,
expose,
logger,
transform: getTransformHook(environment),
useExposed,
processAssets: setProcessAssets(environment),
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
} from 'webpack';
import type RspackChain from '../../compiled/rspack-chain';
import type { ChainIdentifier } from '../configChain';
import type { Logger } from '../logger';
import type {
ModifyRspackConfigUtils,
NarrowedRspackConfig,
Expand Down Expand Up @@ -502,6 +503,12 @@ export type RsbuildPluginAPI = Readonly<{
* `modifyRsbuildConfig` hook is executed.
*/
getNormalizedConfig: typeof getNormalizedConfig;
/**
* A logger instance used to output log information in a unified format.
* Use this instead of `console.log` to maintain consistent logging with Rsbuild.
* Equivalent to `import { logger } from '@rsbuild/core'`.
*/
logger: Logger;
/**
* Determines if a plugin has been registered in the current Rsbuild instance.
*/
Expand Down
12 changes: 7 additions & 5 deletions website/docs/en/api/javascript-api/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ In Rsbuild, most options that support function values use this rule, such as `to

## logger

Used to output log information in a unified format, based on [rslog](https://github.com/rspack-contrib/rslog).
A logger instance used to output log information in a unified format. Use this instead of `console.log` to maintain consistent logging with Rsbuild.

Based on [rslog](https://github.com/rspack-contrib/rslog).

- **Example:**

Expand All @@ -415,13 +417,13 @@ import { logger } from '@rsbuild/core';
logger.greet(`\n➜ Rsbuild v1.0.0\n`);

// Info
logger.info('This is a info message');
logger.info('This is an info message');

// Start
logger.start('This is a start message');

// Warn
logger.warn('This is a warn message');
logger.warn('This is a warning message');

// Ready
logger.ready('This is a ready message');
Expand All @@ -430,8 +432,8 @@ logger.ready('This is a ready message');
logger.success('This is a success message');

// Error
logger.error('This is a error message');
logger.error(new Error('This is a error message with stack'));
logger.error('This is an error message');
logger.error(new Error('This is an error message with stack'));

// Debug
logger.debug('This is a debug message');
Expand Down
21 changes: 21 additions & 0 deletions website/docs/en/plugins/dev/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,27 @@ const pluginFoo = () => ({
});
```

## api.logger

A logger instance used to output log information in a unified format. Use this instead of `console.log` to maintain consistent logging with Rsbuild.

Equivalent to `import { logger } from '@rsbuild/core'`.

- **Version:** `>= 1.4.0`
- **Example:**

```ts
const pluginLogging = () => ({
setup(api) {
api.logger.info('This is an info message');
api.logger.warn('This is a warning message');
api.logger.error('This is an error message');
},
});
```

> `api.logger` is based on [rslog](https://github.com/rspack-contrib/rslog), see [logger](/api/javascript-api/core#logger) for more details.

## api.isPluginExists

import IsPluginExists from '@en/shared/isPluginExists.mdx';
Expand Down
12 changes: 7 additions & 5 deletions website/docs/zh/api/javascript-api/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ const mergedConfig = {

## logger

用于输出格式统一的日志信息,基于 [rslog](https://github.com/rspack-contrib/rslog)。
提供统一日志输出格式的实例,可以用于代替 `console.log`,保持与 Rsbuild 一致的日志输出格式。

基于 [rslog](https://github.com/rspack-contrib/rslog)。

- **示例:**

Expand All @@ -415,13 +417,13 @@ import { logger } from '@rsbuild/core';
logger.greet(`\n➜ Rsbuild v1.0.0\n`);

// Info
logger.info('This is a info message');
logger.info('This is an info message');

// Start
logger.start('This is a start message');

// Warn
logger.warn('This is a warn message');
logger.warn('This is a warning message');

// Ready
logger.ready('This is a ready message');
Expand All @@ -430,8 +432,8 @@ logger.ready('This is a ready message');
logger.success('This is a success message');

// Error
logger.error('This is a error message');
logger.error(new Error('This is a error message with stack'));
logger.error('This is an error message');
logger.error(new Error('This is an error message with stack'));

// Debug
logger.debug('This is a debug message');
Expand Down
21 changes: 21 additions & 0 deletions website/docs/zh/plugins/dev/core.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ const pluginFoo = () => ({
});
```

## api.logger

提供统一日志输出格式的实例,可以用于代替 `console.log`,保持与 Rsbuild 一致的日志输出格式。

等价于 `import { logger } from '@rsbuild/core'`。

- **版本:** `>= 1.4.0`
- **示例:**

```ts
const pluginLogging = () => ({
setup(api) {
api.logger.info('This is an info message');
api.logger.warn('This is a warning message');
api.logger.error('This is an error message');
},
});
```

> `api.logger` 基于 [rslog](https://github.com/rspack-contrib/rslog),查看 [logger](/api/javascript-api/core#logger) 了解更多。

## api.isPluginExists

import IsPluginExists from '@zh/shared/isPluginExists.mdx';
Expand Down
Loading