diff --git a/e2e/cases/plugin-api/logger/index.test.ts b/e2e/cases/plugin-api/logger/index.test.ts new file mode 100644 index 0000000000..d62213c952 --- /dev/null +++ b/e2e/cases/plugin-api/logger/index.test.ts @@ -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); +}); diff --git a/e2e/cases/plugin-api/logger/src/index.js b/e2e/cases/plugin-api/logger/src/index.js new file mode 100644 index 0000000000..8b1a393741 --- /dev/null +++ b/e2e/cases/plugin-api/logger/src/index.js @@ -0,0 +1 @@ +// empty diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 1e1f9e1f54..b3b8b5c795 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -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'; diff --git a/packages/core/src/initPlugins.ts b/packages/core/src/initPlugins.ts index a8ff79255b..07d6a2f416 100644 --- a/packages/core/src/initPlugins.ts +++ b/packages/core/src/initPlugins.ts @@ -347,6 +347,7 @@ export function initPluginAPI({ return (environment?: string) => ({ context: publicContext, expose, + logger, transform: getTransformHook(environment), useExposed, processAssets: setProcessAssets(environment), diff --git a/packages/core/src/types/plugin.ts b/packages/core/src/types/plugin.ts index 399a4db3d4..26f41671f1 100644 --- a/packages/core/src/types/plugin.ts +++ b/packages/core/src/types/plugin.ts @@ -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, @@ -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. */ diff --git a/website/docs/en/api/javascript-api/core.mdx b/website/docs/en/api/javascript-api/core.mdx index 293652c1d2..63278f6767 100644 --- a/website/docs/en/api/javascript-api/core.mdx +++ b/website/docs/en/api/javascript-api/core.mdx @@ -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:** @@ -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'); @@ -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'); diff --git a/website/docs/en/plugins/dev/core.mdx b/website/docs/en/plugins/dev/core.mdx index e45f8faf61..567ad4814b 100644 --- a/website/docs/en/plugins/dev/core.mdx +++ b/website/docs/en/plugins/dev/core.mdx @@ -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'; diff --git a/website/docs/zh/api/javascript-api/core.mdx b/website/docs/zh/api/javascript-api/core.mdx index 2a65649b5d..6bc0c52640 100644 --- a/website/docs/zh/api/javascript-api/core.mdx +++ b/website/docs/zh/api/javascript-api/core.mdx @@ -404,7 +404,9 @@ const mergedConfig = { ## logger -用于输出格式统一的日志信息,基于 [rslog](https://github.com/rspack-contrib/rslog)。 +提供统一日志输出格式的实例,可以用于代替 `console.log`,保持与 Rsbuild 一致的日志输出格式。 + +基于 [rslog](https://github.com/rspack-contrib/rslog)。 - **示例:** @@ -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'); @@ -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'); diff --git a/website/docs/zh/plugins/dev/core.mdx b/website/docs/zh/plugins/dev/core.mdx index 1354b34c06..22919909ee 100644 --- a/website/docs/zh/plugins/dev/core.mdx +++ b/website/docs/zh/plugins/dev/core.mdx @@ -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';