Skip to content

Commit

Permalink
refactor: support compressJson option and refactor the reportDir & re…
Browse files Browse the repository at this point in the history
…portCodeType (#707)
  • Loading branch information
easy1090 authored Jan 22, 2025
1 parent b4d7b9b commit 2483a48
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export const ensureModulesChunksGraphFn = (
ChunksBuildUtils.TileGraphReportName,
),
reportTitle: 'bundle-analyzer',
reportDir: _this.options.reportDir,
reportDir: _this.options.output.reportDir,
},
compiler.outputPath,
);
Expand Down
66 changes: 46 additions & 20 deletions packages/core/src/inner-plugins/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
RsdoctorPluginOptionsNormalized,
IReportCodeType,
} from '@/types';
import { chalk, logger } from '@rsdoctor/utils/logger';

function defaultBoolean(v: unknown, dft: boolean): boolean {
return typeof v === 'boolean' ? v : dft;
Expand All @@ -28,15 +29,17 @@ export function normalizeUserConfig<Rules extends Linter.ExtendRuleData[]>(
loaderInterceptorOptions = {},
disableClientServer = false,
sdkInstance,
reportCodeType = {
noModuleSource: false,
noAssetsAndModuleSource: false,
noCode: false,
sourceCode: true,
assetsCode: true,
},
disableTOSUpload = false,
innerClientPath = '',
output = {
reportCodeType: {
noModuleSource: false,
noAssetsAndModuleSource: false,
noCode: false,
},
reportDir: '',
compressData: true,
},
supports = {
parseBundle: true,
banner: undefined,
Expand All @@ -50,7 +53,6 @@ export function normalizeUserConfig<Rules extends Linter.ExtendRuleData[]>(
reportHtmlName: undefined,
writeDataJson: false,
},
reportDir = '',
} = config;

assert(linter && typeof linter === 'object');
Expand Down Expand Up @@ -99,24 +101,48 @@ export function normalizeUserConfig<Rules extends Linter.ExtendRuleData[]>(
},
disableClientServer,
sdkInstance,
/**
* Data storage is divided into three types:
* The first type: normal mode, all codes are saved.
* The second type: lite is the same as reportCodeType.noModuleSource, which lacks module source code.
* The third type: reportCodeType.noAssetsAndModuleSource means there is no module source code nor the packaged product code.
*
* */
reportCodeType: normalizeReportType(reportCodeType, mode),
output: {
/**
* Data storage is divided into three types:
* The first type: normal mode, all codes are saved.
* The second type: lite is the same as reportCodeType.noModuleSource, which lacks module source code.
* The third type: reportCodeType.noAssetsAndModuleSource means there is no module source code nor the packaged product code.
*
* */
reportCodeType: output.reportCodeType
? normalizeReportType(output.reportCodeType, mode)
: normalizeReportType(
{
noModuleSource: false,
noAssetsAndModuleSource: false,
noCode: false,
},
mode,
),
reportDir: output.reportDir || '',
compressData:
output.compressData !== undefined ? output.compressData : true,
},
disableTOSUpload,
innerClientPath,
supports,
port,
printLog,
mode,
brief,
reportDir,
};

if (
!output.compressData &&
((output.reportCodeType &&
!output.reportCodeType?.noAssetsAndModuleSource) ||
!output.reportCodeType)
) {
logger.info(
chalk.yellow(
`[RSDOCTOR]: When you use compressData: false, it is recommended to set output.reportCodeType to { noAssetsAndModuleSource: true }.`,
),
);
}
return res;
}

Expand Down Expand Up @@ -198,10 +224,10 @@ export const normalizeReportType = (
default: {
if (reportCodeType.noCode) {
globalReportCodeType = SDK.ToDataType.NoCode;
} else if (reportCodeType.noModuleSource) {
globalReportCodeType = SDK.ToDataType.NoSource;
} else if (reportCodeType.noAssetsAndModuleSource) {
globalReportCodeType = SDK.ToDataType.NoSourceAndAssets;
} else if (reportCodeType.noModuleSource) {
globalReportCodeType = SDK.ToDataType.NoSource;
} else {
globalReportCodeType = SDK.ToDataType.Normal;
}
Expand Down
50 changes: 29 additions & 21 deletions packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface RsdoctorWebpackPluginOptions<
mode?: keyof typeof SDK.IMode;

/**
* configuration of the interceptor for webpack loaders.
* configuration of the interceptor for webpack loaders. TODO: delete this options.
* @description worked when the `features.loader === true`.
*/
loaderInterceptorOptions?: {
Expand All @@ -54,8 +54,9 @@ export interface RsdoctorWebpackPluginOptions<
* @default false
*/
disableClientServer?: boolean;

/**
* sdk instance of outside
* sdk instance of outside. TODO: delete this options
*/
sdkInstance?: RsdoctorSDK;

Expand All @@ -64,16 +65,6 @@ export interface RsdoctorWebpackPluginOptions<
*/
supports?: ISupport;

/**
* The directory where the report files will be output.
*/
reportDir?: string;

/**
* Control the Rsdoctor reporter codes records.
*/
reportCodeType?: IReportCodeType | undefined;

/**
* The port of the Rsdoctor server.
*/
Expand All @@ -90,16 +81,34 @@ export interface RsdoctorWebpackPluginOptions<
brief?: SDK.BriefConfig;

/**
* control the Rsdoctor upload data to TOS, used by inner-rsdoctor.
* control the Rsdoctor upload data to TOS, used by inner-rsdoctor. TODO: delete this options
* @default false
*/
disableTOSUpload?: boolean;

/**
* The name of inner rsdoctor's client package, used by inner-rsdoctor.
* The name of inner rsdoctor's client package, used by inner-rsdoctor. TODO: delete this options
* @default false
*/
innerClientPath?: string;

output?: {
/**
* The directory where the report files will be output.
*/
reportDir?: string;

/**
* Control the Rsdoctor reporter codes records.
*/
reportCodeType?: IReportCodeType | undefined;

/**
* Configure whether to compress data.
* @default false
*/
compressData?: boolean;
};
}

export interface RsdoctorMultiplePluginOptions<
Expand All @@ -123,19 +132,18 @@ export interface RsdoctorPluginOptionsNormalized<
> extends Common.DeepRequired<
Omit<
RsdoctorWebpackPluginOptions<Rules>,
| 'sdkInstance'
| 'linter'
| 'reportCodeType'
| 'supports'
| 'port'
| 'brief'
'sdkInstance' | 'linter' | 'output' | 'supports' | 'port' | 'brief'
>
> {
features: Common.DeepRequired<Plugin.RsdoctorWebpackPluginFeatures>;
linter: Required<LinterType.Options<Rules, InternalRules>>;
sdkInstance?: RsdoctorSDK;
output: {
reportCodeType: SDK.ToDataType;
reportDir: string;
compressData: boolean;
};
port?: number;
reportCodeType: SDK.ToDataType;
supports: ISupport;
brief: SDK.BriefConfig;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-plugin/src/multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class RsdoctorRspackMultiplePlugin<
mode: normallizedOptions.mode ? normallizedOptions.mode : undefined,
brief: normallizedOptions.brief,
},
type: normallizedOptions.reportCodeType,
type: normallizedOptions.output.reportCodeType,
});

super({
Expand Down
9 changes: 5 additions & 4 deletions packages/rspack-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ export class RsdoctorRspackPlugin<Rules extends Linter.ExtendRuleData[]>
port: this.options.port,
name: pluginTapName,
root: process.cwd(),
type: this.options.reportCodeType,
type: this.options.output.reportCodeType,
config: {
disableTOSUpload: this.options.disableTOSUpload,
innerClientPath: this.options.innerClientPath,
printLog: this.options.printLog,
mode: this.options.mode ? this.options.mode : undefined,
brief: this.options.brief,
compressData: this.options.output.compressData,
},
});
this.outsideInstance = Boolean(this.options.sdkInstance);
Expand Down Expand Up @@ -191,15 +192,15 @@ export class RsdoctorRspackPlugin<Rules extends Linter.ExtendRuleData[]>
if (this.outsideInstance && 'parent' in this.sdk) {
this.sdk.parent.master.setOutputDir(
path.resolve(
this.options.reportDir || compiler.outputPath,
this.options.output.reportDir || compiler.outputPath,
`./${Constants.RsdoctorOutputFolder}`,
),
);
}

this.sdk.setOutputDir(
path.resolve(
this.options.reportDir || compiler.outputPath,
this.options.output.reportDir || compiler.outputPath,
`./${Constants.RsdoctorOutputFolder}`,
),
);
Expand Down Expand Up @@ -251,7 +252,7 @@ export class RsdoctorRspackPlugin<Rules extends Linter.ExtendRuleData[]>

this.sdk.setOutputDir(
path.resolve(
this.options.reportDir || compiler.outputPath,
this.options.output.reportDir || compiler.outputPath,
`./${Constants.RsdoctorOutputFolder}`,
),
);
Expand Down
20 changes: 16 additions & 4 deletions packages/sdk/src/sdk/sdk/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export abstract class SDKCore<T extends RsdoctorSDKOptions>

protected hash!: string;

extraConfig: SDK.SDKOptionsType | undefined;
public extraConfig: SDK.SDKOptionsType | undefined;

public readonly root: string;

Expand Down Expand Up @@ -155,11 +155,19 @@ export abstract class SDKCore<T extends RsdoctorSDKOptions>

if (Array.isArray(jsonStr)) {
const urls = jsonStr.map((str, index) => {
return this.writeToFolder(str, outputDir, key, index + 1);
return this.writeToFolder(
str,
outputDir,
key,
this.extraConfig,
index + 1,
);
});
urlsPromiseList.push(...urls);
} else {
urlsPromiseList.push(this.writeToFolder(jsonStr, outputDir, key));
urlsPromiseList.push(
this.writeToFolder(jsonStr, outputDir, key, this.extraConfig),
);
}
}

Expand Down Expand Up @@ -212,9 +220,13 @@ export abstract class SDKCore<T extends RsdoctorSDKOptions>
jsonStr: string,
dir: string,
key: string,
extraConfig: SDK.SDKOptionsType | undefined,
index?: number,
): Promise<DataWithUrl> {
const sharding = new File.FileSharding(Algorithm.compressText(jsonStr));
const { compressData } = extraConfig || { compressData: true };
const sharding = compressData
? new File.FileSharding(Algorithm.compressText(jsonStr))
: new File.FileSharding(jsonStr);
const folder = path.resolve(dir, key);
const writer = sharding.writeStringToFolder(folder, '', index);
return writer.then((item) => {
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/sdk/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export type SDKOptionsType = {
printLog?: IPrintLog;
mode?: keyof typeof IMode;
brief?: BriefConfig;
compressData?: boolean;
};

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/webpack-plugin/src/multiple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export class RsdoctorWebpackMultiplePlugin<
printLog: normallizedOptions.printLog,
mode: normallizedOptions.mode ? normallizedOptions.mode : undefined,
brief: normallizedOptions.brief,
compressData: normallizedOptions.output.compressData,
},
type: normallizedOptions.reportCodeType,
type: normallizedOptions.output.reportCodeType,
});

super({
Expand Down
8 changes: 5 additions & 3 deletions packages/webpack-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ export class RsdoctorWebpackPlugin<Rules extends Linter.ExtendRuleData[]>
port: this.options.port,
name: pluginTapName,
root: process.cwd(),
type: this.options.reportCodeType,
type: this.options.output.reportCodeType,
config: {
disableTOSUpload: this.options.disableTOSUpload,
innerClientPath: this.options.innerClientPath,
printLog: this.options.printLog,
mode: this.options.mode ? this.options.mode : undefined,
mode: this.options.mode,
brief: this.options.brief,
compressData: this.options.output.compressData,
},
});

this.modulesGraph = new ModuleGraph();
this.chunkGraph = new ChunkGraph();
this.isRsdoctorPlugin = true;
Expand Down Expand Up @@ -163,7 +165,7 @@ export class RsdoctorWebpackPlugin<Rules extends Linter.ExtendRuleData[]>

this.sdk.setOutputDir(
path.resolve(
this.options.reportDir || compiler.outputPath,
this.options.output.reportDir || compiler.outputPath,
`./${Constants.RsdoctorOutputFolder}`,
),
);
Expand Down

0 comments on commit 2483a48

Please sign in to comment.