From 19b5b067b538d116da8e35d875bd0784707d5afe Mon Sep 17 00:00:00 2001 From: easy1090 Date: Thu, 28 Nov 2024 16:42:12 +0800 Subject: [PATCH 1/2] fix(multi-plugin): fix multi-plugin options error fix(multi-plugin): fix multi-plugin options error fix(multi-plugin): fix multi-plugin options error fix(multi-plugin): fix multi-plugin options error fix(multi-plugin): fix multi-plugin options error --- .../plugins/multi-plugin-brief.test.ts | 56 +++++++++++++++++++ e2e/cases/doctor-webpack/test-utils.ts | 36 +++++++++++- e2e/playwright.config.ts | 1 + packages/rspack-plugin/src/multiple.ts | 13 ++++- packages/sdk/src/sdk/multiple/controller.ts | 2 + packages/sdk/src/sdk/multiple/slave.ts | 3 + packages/sdk/src/sdk/sdk/index.ts | 2 +- packages/webpack-plugin/src/multiple.ts | 12 +++- 8 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 e2e/cases/doctor-webpack/plugins/multi-plugin-brief.test.ts diff --git a/e2e/cases/doctor-webpack/plugins/multi-plugin-brief.test.ts b/e2e/cases/doctor-webpack/plugins/multi-plugin-brief.test.ts new file mode 100644 index 00000000..163e0a4e --- /dev/null +++ b/e2e/cases/doctor-webpack/plugins/multi-plugin-brief.test.ts @@ -0,0 +1,56 @@ +import { expect, test } from '@playwright/test'; +import { getSDK } from '@rsdoctor/core/plugins'; +import { compileByWebpack5 } from '@scripts/test-helper'; +import path from 'path'; +import { Compiler } from 'webpack'; +import { createRsdoctorMultiPlugin } from '../test-utils'; + +async function webpack(tapName: string, compile: typeof compileByWebpack5) { + const file = path.resolve(__dirname, '../fixtures/a.js'); + const loader = path.resolve(__dirname, '../fixtures/loaders/comment.js'); + const res = await compile(file, { + module: { + rules: [ + { + test: /\.js/, + use: loader, + }, + ], + }, + plugins: [ + createRsdoctorMultiPlugin({ + mode: 'brief', + brief: { + reportHtmlName: '111.html', + writeDataJson: false, + }, + }), + { + name: tapName, + apply(compiler: Compiler) { + compiler.hooks.done.tapPromise(tapName, async () => { + // nothing + }); + compiler.hooks.thisCompilation.tap(tapName, (compilation) => { + compilation.hooks.seal.tap(tapName, () => { + return 'seal end'; + }); + }); + }, + }, + ], + }); + return res; +} + +test('rsdoctor webpack5 multi-plugins options tests', async () => { + const tapName = 'Foo'; + await webpack(tapName, compileByWebpack5); + const sdk = getSDK(); + expect(sdk.type).toBe(0); + expect(sdk.extraConfig?.mode).toBe('brief'); + expect(sdk.extraConfig?.brief).toMatchObject({ + reportHtmlName: '111.html', + writeDataJson: false, + }); +}); diff --git a/e2e/cases/doctor-webpack/test-utils.ts b/e2e/cases/doctor-webpack/test-utils.ts index 39be0b85..67f32c3d 100644 --- a/e2e/cases/doctor-webpack/test-utils.ts +++ b/e2e/cases/doctor-webpack/test-utils.ts @@ -1,5 +1,8 @@ import { RsdoctorWebpackPluginOptions } from '@rsdoctor/core/types'; -import { RsdoctorWebpackPlugin } from '@rsdoctor/webpack-plugin'; +import { + RsdoctorWebpackPlugin, + RsdoctorWebpackMultiplePlugin, +} from '@rsdoctor/webpack-plugin'; import { Linter } from '@rsdoctor/types'; import { File } from '@rsdoctor/utils/build'; import { tmpdir } from 'os'; @@ -35,3 +38,34 @@ export function createRsdoctorPlugin( return plugin; } + +export function createRsdoctorMultiPlugin( + options: RsdoctorWebpackPluginOptions = {}, +) { + const plugin = new RsdoctorWebpackMultiplePlugin({ + ...options, + disableClientServer: + typeof options.disableClientServer === 'boolean' + ? options.disableClientServer + : true, + }); + + const outdir = path.resolve( + tmpdir(), + `./${Date.now()}/web_doctor_webpack_plugin_test`, + ); + + plugin.sdk.hooks.afterSaveManifest.tapPromise( + { name: 'REMOVE_TMP_DIR', stage: -9999 }, + async () => { + plugin.sdk.setOutputDir(outdir); + try { + await File.fse.remove(plugin.sdk.outputDir); + } catch (e) { + console.error(e); + } + }, + ); + + return plugin; +} diff --git a/e2e/playwright.config.ts b/e2e/playwright.config.ts index 353f9164..cdfb8e00 100644 --- a/e2e/playwright.config.ts +++ b/e2e/playwright.config.ts @@ -2,4 +2,5 @@ import { defineConfig } from '@playwright/test'; export default defineConfig({ testMatch: ['/cases/**/**.test.ts'], + timeout: 60000, }); diff --git a/packages/rspack-plugin/src/multiple.ts b/packages/rspack-plugin/src/multiple.ts index 7d581875..11440976 100644 --- a/packages/rspack-plugin/src/multiple.ts +++ b/packages/rspack-plugin/src/multiple.ts @@ -3,6 +3,7 @@ import type { Linter } from '@rsdoctor/types'; import type { RsdoctorMultiplePluginOptions } from '@rsdoctor/core'; import { RsdoctorRspackPlugin } from './plugin'; +import { normalizeUserConfig } from '@rsdoctor/core/plugins'; let globalController: RsdoctorSDKController | undefined; @@ -22,17 +23,25 @@ export class RsdoctorRspackMultiplePlugin< return controller; })(); + const normallizedOptions = normalizeUserConfig(options); + const instance = controller.createSlave({ name: options.name || 'Builder', stage: options.stage, - extraConfig: { disableTOSUpload: options.disableTOSUpload || false }, + extraConfig: { + disableTOSUpload: normallizedOptions.disableTOSUpload || false, + innerClientPath: normallizedOptions.innerClientPath, + printLog: normallizedOptions.printLog, + mode: normallizedOptions.mode ? normallizedOptions.mode : undefined, + brief: normallizedOptions.brief, + }, + type: normallizedOptions.reportCodeType, }); super({ ...options, sdkInstance: instance, }); - this.controller = controller; } } diff --git a/packages/sdk/src/sdk/multiple/controller.ts b/packages/sdk/src/sdk/multiple/controller.ts index db4a85b0..6a3e5d7d 100644 --- a/packages/sdk/src/sdk/multiple/controller.ts +++ b/packages/sdk/src/sdk/multiple/controller.ts @@ -42,12 +42,14 @@ export class RsdoctorSDKController { name, stage, extraConfig, + type, }: Omit[0], 'controller'>) { const slave = new RsdoctorPrimarySDK({ name, stage, controller: this, extraConfig, + type, }); this.slaves.push(slave); // sort by stage after create slave sdk. diff --git a/packages/sdk/src/sdk/multiple/slave.ts b/packages/sdk/src/sdk/multiple/slave.ts index e4d20f39..8b7f196a 100644 --- a/packages/sdk/src/sdk/multiple/slave.ts +++ b/packages/sdk/src/sdk/multiple/slave.ts @@ -16,6 +16,7 @@ interface RsdoctorSlaveSDKOptions { stage?: number; extraConfig?: SDK.SDKOptionsType; controller: RsdoctorSDKController; + type: SDK.ToDataType; } export class RsdoctorPrimarySDK extends RsdoctorSDK { @@ -34,6 +35,7 @@ export class RsdoctorPrimarySDK extends RsdoctorSDK { stage, controller, extraConfig, + type, }: RsdoctorSlaveSDKOptions) { super({ name, root: controller.root }); @@ -45,6 +47,7 @@ export class RsdoctorPrimarySDK extends RsdoctorSDK { this.extraConfig = extraConfig; this.parent = controller; this.server = new RsdoctorSlaveServer(this, port); + this.type = type; this.setName(name); this.clearSwitch(); } diff --git a/packages/sdk/src/sdk/sdk/index.ts b/packages/sdk/src/sdk/sdk/index.ts index b756c661..ba96d76b 100644 --- a/packages/sdk/src/sdk/sdk/index.ts +++ b/packages/sdk/src/sdk/sdk/index.ts @@ -25,7 +25,7 @@ export class RsdoctorSDK< public extraConfig: SDK.SDKOptionsType | undefined; - private type: SDK.ToDataType; + public type: SDK.ToDataType; private _summary: SDK.SummaryData = { costs: [] }; diff --git a/packages/webpack-plugin/src/multiple.ts b/packages/webpack-plugin/src/multiple.ts index 3ea9b118..ad783ed2 100644 --- a/packages/webpack-plugin/src/multiple.ts +++ b/packages/webpack-plugin/src/multiple.ts @@ -3,6 +3,7 @@ import type { Linter } from '@rsdoctor/types'; import type { RsdoctorMultiplePluginOptions } from '@rsdoctor/core'; import { RsdoctorWebpackPlugin } from './plugin'; +import { normalizeUserConfig } from '@rsdoctor/core/plugins'; let globalController: RsdoctorSDKController | undefined; @@ -22,10 +23,19 @@ export class RsdoctorWebpackMultiplePlugin< return controller; })(); + const normallizedOptions = normalizeUserConfig(options); + const instance = controller.createSlave({ name: options.name || 'Builder', stage: options.stage, - extraConfig: { disableTOSUpload: options.disableTOSUpload || false }, + extraConfig: { + disableTOSUpload: normallizedOptions.disableTOSUpload || false, + innerClientPath: normallizedOptions.innerClientPath, + printLog: normallizedOptions.printLog, + mode: normallizedOptions.mode ? normallizedOptions.mode : undefined, + brief: normallizedOptions.brief, + }, + type: normallizedOptions.reportCodeType, }); super({ From a562eda53a818c4327625be954a1477562f47ff3 Mon Sep 17 00:00:00 2001 From: easy1090 Date: Thu, 28 Nov 2024 18:43:10 +0800 Subject: [PATCH 2/2] fix(multi-plugin): fix multi-plugin options error --- .changeset/strong-games-beam.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/strong-games-beam.md diff --git a/.changeset/strong-games-beam.md b/.changeset/strong-games-beam.md new file mode 100644 index 00000000..1aeb9cac --- /dev/null +++ b/.changeset/strong-games-beam.md @@ -0,0 +1,7 @@ +--- +'@rsdoctor/webpack-plugin': patch +'@rsdoctor/rspack-plugin': patch +'@rsdoctor/sdk': patch +--- + +fix(multi-plugin): fix multi-plugin options error