Skip to content

Commit

Permalink
feat: support automatically enable Rsdoctor
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Jan 26, 2024
1 parent ff81b97 commit 6010f5e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/compat/webpack/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const applyDefaultPlugins = (plugins: Plugins) =>
import('./plugins/sass').then((m) => m.pluginSass()),
import('./plugins/less').then((m) => m.pluginLess()),
plugins.bundleAnalyzer(),
plugins.rsdoctor(),
plugins.splitChunks(),
plugins.startUrl?.(),
plugins.inlineChunk(),
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const plugins: Plugins = {
inlineChunk: () => import('./inlineChunk').then((m) => m.pluginInlineChunk()),
bundleAnalyzer: () =>
import('./bundleAnalyzer').then((m) => m.pluginBundleAnalyzer()),
rsdoctor: () => import('./rsdoctor').then((m) => m.pluginRsdoctor()),
asset: () => import('./asset').then((m) => m.pluginAsset()),
wasm: () => import('./wasm').then((m) => m.pluginWasm()),
moment: () => import('./moment').then((m) => m.pluginMoment()),
Expand Down
66 changes: 66 additions & 0 deletions packages/core/src/plugins/rsdoctor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { color, logger, type BundlerPluginInstance } from '@rsbuild/shared';
import type { RsbuildPlugin } from '../types';

export function pluginRsdoctor(): RsbuildPlugin {
return {
name: 'rsbuild:rsdoctor',

setup(api) {
api.onBeforeCreateCompiler(async ({ bundlerConfigs }) => {
if (process.env.RSDOCTOR !== 'true') {
return;
}

const isRspack = api.context.bundlerType === 'rspack';
const packageName = isRspack
? '@rsdoctor/rspack-plugin'
: '@rsdoctor/webpack-plugin';

let module: {
RsdoctorRspackPlugin: { new (): BundlerPluginInstance };
RsdoctorWebpackPlugin: { new (): BundlerPluginInstance };
};

try {
const path = require.resolve(packageName, {
paths: [api.context.rootPath],
});
module = await import(path);
} catch (err) {
logger.warn(
`\`process.env.RSDOCTOR\` enabled, please install ${color.bold(color.yellow(packageName))} package.`,
);
return;
}

const pluginName = isRspack
? 'RsdoctorRspackPlugin'
: 'RsdoctorWebpackPlugin';

if (!module || !module[pluginName]) {
return;
}

let isAutoRegister = false;

bundlerConfigs.forEach((config) => {
const registered = config.plugins?.some(
(plugin) => plugin?.constructor?.name === pluginName,
);

if (registered) {
return;
}

config.plugins ||= [];
config.plugins.push(new module[pluginName]());
isAutoRegister = true;
});

if (isAutoRegister) {
logger.info(`${color.bold(color.yellow(packageName))} enabled.`);
}
});
},
};
}
1 change: 1 addition & 0 deletions packages/core/src/provider/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const applyDefaultPlugins = (plugins: Plugins) =>
plugins.startUrl(),
plugins.inlineChunk(),
plugins.bundleAnalyzer(),
plugins.rsdoctor(),
plugins.networkPerformance(),
plugins.preloadOrPrefetch(),
plugins.performance(),
Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export type Plugins = {
splitChunks: PluginsFn;
inlineChunk: PluginsFn;
bundleAnalyzer: PluginsFn;
rsdoctor: PluginsFn;
asset: PluginsFn;
html: PluginsFn;
wasm: PluginsFn;
Expand Down

0 comments on commit 6010f5e

Please sign in to comment.