diff --git a/packages/core/src/plugins/rsdoctor.ts b/packages/core/src/plugins/rsdoctor.ts index f26c8b4068..7d3ff17b0c 100644 --- a/packages/core/src/plugins/rsdoctor.ts +++ b/packages/core/src/plugins/rsdoctor.ts @@ -1,3 +1,4 @@ +import type { Configuration } from '@rspack/core'; import color from 'picocolors'; import { logger } from '../logger'; import type { BundlerPluginInstance, RsbuildPlugin } from '../types'; @@ -7,15 +8,19 @@ type RsdoctorExports = { RsdoctorWebpackPlugin: { new (): BundlerPluginInstance }; }; +type MaybeRsdoctorPlugin = Configuration['plugins'] & { isRsdoctorPlugin?: boolean }; + export const pluginRsdoctor = (): RsbuildPlugin => ({ name: 'rsbuild:rsdoctor', setup(api) { api.onBeforeCreateCompiler(async ({ bundlerConfigs }) => { + // If Rsdoctor isn't enabled, skip this plugin. if (process.env.RSDOCTOR !== 'true') { return; } + // Add Rsdoctor plugin to start analysis. const isRspack = api.context.bundlerType === 'rspack'; const packageName = isRspack ? '@rsdoctor/rspack-plugin' @@ -45,9 +50,12 @@ export const pluginRsdoctor = (): RsbuildPlugin => ({ let isAutoRegister = false; + const isRsdoctorPlugin = (plugin: MaybeRsdoctorPlugin) => plugin?.isRsdoctorPlugin === true; + for (const config of bundlerConfigs) { - const registered = config.plugins?.some( - (plugin) => plugin?.constructor?.name === pluginName, + // If user has added the Rsdoctor plugin to the config file, it will return. + const registered = config.plugins?.some( + (plugin) => isRsdoctorPlugin(plugin as unknown as MaybeRsdoctorPlugin) || plugin?.constructor?.name === pluginName, ); if (registered) {