Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(multi-plugin): fix multi-plugin options error #608

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions e2e/cases/doctor-webpack/plugins/multi-plugin-brief.test.ts
Original file line number Diff line number Diff line change
@@ -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,
});
});
36 changes: 35 additions & 1 deletion e2e/cases/doctor-webpack/test-utils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -35,3 +38,34 @@ export function createRsdoctorPlugin<T extends Linter.ExtendRuleData[]>(

return plugin;
}

export function createRsdoctorMultiPlugin<T extends Linter.ExtendRuleData[]>(
options: RsdoctorWebpackPluginOptions<T> = {},
) {
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;
}
1 change: 1 addition & 0 deletions e2e/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { defineConfig } from '@playwright/test';

export default defineConfig({
testMatch: ['/cases/**/**.test.ts'],
timeout: 60000,
});
13 changes: 11 additions & 2 deletions packages/rspack-plugin/src/multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,17 +23,25 @@ export class RsdoctorRspackMultiplePlugin<
return controller;
})();

const normallizedOptions = normalizeUserConfig<Rules>(options);

const instance = controller.createSlave({
name: options.name || 'Builder',
stage: options.stage,
extraConfig: { disableTOSUpload: options.disableTOSUpload || false },
extraConfig: {
disableTOSUpload: normallizedOptions.disableTOSUpload || false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This options should be removed

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;
}
}
2 changes: 2 additions & 0 deletions packages/sdk/src/sdk/multiple/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ export class RsdoctorSDKController {
name,
stage,
extraConfig,
type,
}: Omit<ConstructorParameters<typeof RsdoctorPrimarySDK>[0], 'controller'>) {
const slave = new RsdoctorPrimarySDK({
name,
stage,
controller: this,
extraConfig,
type,
});
this.slaves.push(slave);
// sort by stage after create slave sdk.
Expand Down
3 changes: 3 additions & 0 deletions packages/sdk/src/sdk/multiple/slave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface RsdoctorSlaveSDKOptions {
stage?: number;
extraConfig?: SDK.SDKOptionsType;
controller: RsdoctorSDKController;
type: SDK.ToDataType;
}

export class RsdoctorPrimarySDK extends RsdoctorSDK {
Expand All @@ -34,6 +35,7 @@ export class RsdoctorPrimarySDK extends RsdoctorSDK {
stage,
controller,
extraConfig,
type,
}: RsdoctorSlaveSDKOptions) {
super({ name, root: controller.root });

Expand All @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/sdk/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [] };

Expand Down
12 changes: 11 additions & 1 deletion packages/webpack-plugin/src/multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -22,10 +23,19 @@ export class RsdoctorWebpackMultiplePlugin<
return controller;
})();

const normallizedOptions = normalizeUserConfig<Rules>(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({
Expand Down