Skip to content

Commit

Permalink
fix: skip resolution if Rsdoctor plugin is registered (#3370)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Sep 4, 2024
1 parent 81bd1bb commit 2451307
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 35 deletions.
43 changes: 35 additions & 8 deletions e2e/cases/rsdoctor/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import fs from 'node:fs';
import path from 'node:path';
import { build, proxyConsole, rspackOnlyTest } from '@e2e/helper';
import { expect } from '@playwright/test';
import { expect, test } from '@playwright/test';

const packagePath = path.join(
__dirname,
'node_modules/@rsdoctor/rspack-plugin',
);
const testFile = path.join(packagePath, 'test.txt');
const testFile = path.join(packagePath, 'test-temp.txt');

test.beforeEach(() => {
fs.rmSync(packagePath, { recursive: true, force: true });
fs.cpSync(path.join(__dirname, 'mock'), packagePath, { recursive: true });
});

rspackOnlyTest(
'should register Rsdoctor plugin when process.env.RSDOCTOR is true',
async () => {
fs.rmSync(packagePath, { recursive: true, force: true });
fs.cpSync(path.join(__dirname, 'mock'), packagePath, { recursive: true });

const { logs, restore } = proxyConsole();
process.env.RSDOCTOR = 'true';

Expand All @@ -35,9 +37,6 @@ rspackOnlyTest(
rspackOnlyTest(
'should not register Rsdoctor plugin when process.env.RSDOCTOR is false',
async () => {
fs.rmSync(packagePath, { recursive: true, force: true });
fs.cpSync(path.join(__dirname, 'mock'), packagePath, { recursive: true });

process.env.RSDOCTOR = 'false';

await build({
Expand All @@ -48,3 +47,31 @@ rspackOnlyTest(
process.env.RSDOCTOR = '';
},
);

rspackOnlyTest(
'should not register Rsdoctor plugin when process.env.RSDOCTOR is true and the plugin has been registered',
async () => {
const { logs, restore } = proxyConsole();
const { RsdoctorRspackPlugin } = require('@rsdoctor/rspack-plugin');

process.env.RSDOCTOR = 'true';

await build({
cwd: __dirname,
rsbuildConfig: {
tools: {
rspack: {
plugins: [new RsdoctorRspackPlugin()],
},
},
},
});

expect(
logs.some((log) => log.includes('@rsdoctor') && log.includes('enabled')),
).toBe(false);

process.env.RSDOCTOR = '';
restore();
},
);
2 changes: 1 addition & 1 deletion e2e/cases/rsdoctor/mock/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class RsdoctorRspackPlugin {

apply(compiler) {
compiler.hooks.done.tap('rsdoctor:test', () => {
fse.outputFileSync(path.join(__dirname, './test.txt'), 'test');
fse.outputFileSync(path.join(__dirname, './test-temp.txt'), 'test');
});
}
}
Expand Down
57 changes: 31 additions & 26 deletions packages/core/src/plugins/rsdoctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,56 +24,61 @@ export const pluginRsdoctor = (): RsbuildPlugin => ({

// Add Rsdoctor plugin to start analysis.
const isRspack = api.context.bundlerType === 'rspack';
const pluginName = isRspack
? 'RsdoctorRspackPlugin'
: 'RsdoctorWebpackPlugin';

const isRsdoctorPlugin = (plugin: MaybeRsdoctorPlugin) =>
plugin?.isRsdoctorPlugin === true ||
plugin?.constructor?.name === pluginName;

for (const config of bundlerConfigs) {
// If user has added the Rsdoctor plugin manually, skip the auto-registration.
const registered = config.plugins?.some((plugin) =>
isRsdoctorPlugin(plugin as unknown as MaybeRsdoctorPlugin),
);

if (registered) {
return;
}
}

const packageName = isRspack
? '@rsdoctor/rspack-plugin'
: '@rsdoctor/webpack-plugin';

let module: RsdoctorExports;
let packagePath: string;

try {
const path = require.resolve(packageName, {
packagePath = 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';
let module: RsdoctorExports;
try {
module = await import(packagePath);
} catch (err) {
logger.error(
`\`process.env.RSDOCTOR\` enabled, but failed to load ${color.bold(color.yellow(packageName))} module.`,
);
return;
}

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

let isAutoRegister = false;

const isRsdoctorPlugin = (plugin: MaybeRsdoctorPlugin) =>
plugin?.isRsdoctorPlugin === true ||
plugin?.constructor?.name === pluginName;

for (const config of bundlerConfigs) {
// 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),
);

if (registered) {
return;
}

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

if (isAutoRegister) {
logger.info(`${color.bold(color.yellow(packageName))} enabled.`);
}
logger.info(`${color.bold(color.yellow(packageName))} enabled.`);
});
},
});

0 comments on commit 2451307

Please sign in to comment.