@rspack/test-tools 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -87,6 +87,8 @@ function compareContent(sourceContent, distContent) {
87
87
  const lines = sourceContent.trim().split("\n").length;
88
88
  return {
89
89
  type: type_1.ECompareResultType.Same,
90
+ source: sourceContent,
91
+ dist: distContent,
90
92
  lines: {
91
93
  source: lines,
92
94
  common: lines,
@@ -100,6 +102,8 @@ function compareContent(sourceContent, distContent) {
100
102
  return {
101
103
  type: type_1.ECompareResultType.Different,
102
104
  detail: difference,
105
+ source: sourceContent,
106
+ dist: distContent,
103
107
  lines: {
104
108
  source: diffLines.filter(l => l[0] < 0).length,
105
109
  common: diffLines.filter(l => l[0] === 0).length,
@@ -111,6 +115,7 @@ function compareContent(sourceContent, distContent) {
111
115
  else {
112
116
  return {
113
117
  type: type_1.ECompareResultType.OnlySource,
118
+ source: sourceContent,
114
119
  lines: {
115
120
  source: sourceContent.trim().split("\n").length,
116
121
  common: 0,
@@ -123,6 +128,7 @@ function compareContent(sourceContent, distContent) {
123
128
  if (distContent) {
124
129
  return {
125
130
  type: type_1.ECompareResultType.OnlyDist,
131
+ dist: distContent,
126
132
  lines: {
127
133
  source: 0,
128
134
  common: 0,
@@ -32,7 +32,7 @@ function parseModules(content) {
32
32
  // parse bootstrap code
33
33
  const bootstrap = getStringBetween(content, 0, BOOTSTRAP_SPLIT_LINE, BOOTSTRAP_SPLIT_LINE);
34
34
  if (bootstrap.result) {
35
- runtimeModules.set("webpack/bootstrap", bootstrap.result);
35
+ runtimeModules.set("webpack/runtime/bootstrap", bootstrap.result);
36
36
  }
37
37
  // parse module & runtime module code
38
38
  let moduleName = getStringBetween(content, currentPosition, MODULE_START_FLAG, MODULE_FLAG_END).result;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./type";
2
2
  export * from "./test/tester";
3
- export * from "./processor/diff";
3
+ export * from "./processor";
4
+ export * from "./reporter";
package/dist/index.js CHANGED
@@ -16,4 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./type"), exports);
18
18
  __exportStar(require("./test/tester"), exports);
19
- __exportStar(require("./processor/diff"), exports);
19
+ __exportStar(require("./processor"), exports);
20
+ __exportStar(require("./reporter"), exports);
@@ -0,0 +1 @@
1
+ export * from "./diff";
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./diff"), exports);
@@ -0,0 +1,15 @@
1
+ import { ITestReporter, TModuleCompareResult } from "../type";
2
+ export interface IDiffHtmlReporterOptions {
3
+ dist: string;
4
+ ignore?: RegExp;
5
+ }
6
+ export declare class DiffHtmlReporter implements ITestReporter<TModuleCompareResult[]> {
7
+ private options;
8
+ private failed;
9
+ private results;
10
+ constructor(options: IDiffHtmlReporterOptions);
11
+ init(data?: TModuleCompareResult[]): Promise<void>;
12
+ failure(id: string): Promise<void>;
13
+ increment(id: string, data: TModuleCompareResult[]): Promise<void>;
14
+ output(): Promise<void>;
15
+ }
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DiffHtmlReporter = void 0;
7
+ const fs_extra_1 = __importDefault(require("fs-extra"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const VIEWER_DIR = path_1.default.join(__dirname, "../viewer");
10
+ const DIFF_STATS_PLACEHOLDER = "$$RSPACK_DIFF_STATS_PLACEHOLDER$$";
11
+ const DEFAULT_IGNORE = /node_modules/;
12
+ class DiffHtmlReporter {
13
+ constructor(options) {
14
+ this.options = options;
15
+ this.failed = new Set();
16
+ this.results = new Map();
17
+ }
18
+ async init(data = []) { }
19
+ async failure(id) {
20
+ this.failed.add(id);
21
+ this.results.delete(id);
22
+ }
23
+ async increment(id, data) {
24
+ if (this.failed.has(id))
25
+ return;
26
+ if (!this.results.has(id)) {
27
+ this.results.set(id, []);
28
+ }
29
+ const ignore = this.options.ignore || DEFAULT_IGNORE;
30
+ const current = this.results.get(id);
31
+ for (let i of data) {
32
+ if (!ignore.test(i.name)) {
33
+ current.push({
34
+ name: i.name,
35
+ source: i.source || "",
36
+ dist: i.dist || "",
37
+ type: i.type
38
+ });
39
+ }
40
+ }
41
+ }
42
+ async output() {
43
+ fs_extra_1.default.ensureDirSync(this.options.dist);
44
+ for (let viewerFile of fs_extra_1.default
45
+ .readdirSync(VIEWER_DIR)
46
+ .filter(file => file.startsWith("diff"))) {
47
+ const sourceFile = path_1.default.join(VIEWER_DIR, viewerFile);
48
+ if (path_1.default.extname(viewerFile) === ".html") {
49
+ const template = fs_extra_1.default.readFileSync(sourceFile, "utf-8");
50
+ for (let [id, items] of this.results.entries()) {
51
+ const data = {
52
+ root: id,
53
+ data: items
54
+ };
55
+ const content = template.replace(DIFF_STATS_PLACEHOLDER, JSON.stringify(data));
56
+ const casename = path_1.default.basename(id);
57
+ const extname = path_1.default.extname(viewerFile);
58
+ const filename = path_1.default.basename(viewerFile, extname);
59
+ fs_extra_1.default.writeFileSync(path_1.default.join(this.options.dist, `${filename}_${casename}${extname}`), content, "utf-8");
60
+ }
61
+ }
62
+ else {
63
+ fs_extra_1.default.copyFileSync(sourceFile, path_1.default.join(this.options.dist, viewerFile));
64
+ }
65
+ }
66
+ }
67
+ }
68
+ exports.DiffHtmlReporter = DiffHtmlReporter;
@@ -0,0 +1,24 @@
1
+ import { ECompilerType, ITestReporter, TModuleCompareResult } from "../type";
2
+ export interface IDiffStatsReporterOptions {
3
+ header?: string[];
4
+ footer?: string[];
5
+ file: string;
6
+ report?: boolean;
7
+ }
8
+ export type TCompilerTypeId = ECompilerType.Rspack | ECompilerType.Webpack | "common";
9
+ export type TModuleTypeId = "normal" | "runtime";
10
+ export type TDimenTypeId = "modules" | "lines" | "lines-in-common";
11
+ export type TCaseSummaryId = `${TCompilerTypeId}|${TModuleTypeId}|${TDimenTypeId}`;
12
+ export type TCaseSummary = Record<TCaseSummaryId, number>;
13
+ export declare class DiffStatsReporter implements ITestReporter<TModuleCompareResult[]> {
14
+ private options;
15
+ private summary;
16
+ private failed;
17
+ constructor(options: IDiffStatsReporterOptions);
18
+ init(data?: TModuleCompareResult[]): Promise<void>;
19
+ failure(id: string): Promise<void>;
20
+ increment(id: string, data: TModuleCompareResult[]): Promise<void>;
21
+ output(): Promise<void>;
22
+ private stringifySummary;
23
+ private createSummary;
24
+ }
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DiffStatsReporter = void 0;
7
+ const type_1 = require("../type");
8
+ const fs_extra_1 = __importDefault(require("fs-extra"));
9
+ const csv_to_markdown_table_1 = __importDefault(require("csv-to-markdown-table"));
10
+ const toPercent = (d) => (d * 100).toFixed(2) + "%";
11
+ const toFirstLetterUpperCase = (s) => (s.charAt(0).toUpperCase() + s.slice(1)).split("-").join(" ");
12
+ const GITHUB_RUN_ID = process.env.GITHUB_RUN_ID;
13
+ class DiffStatsReporter {
14
+ constructor(options) {
15
+ this.options = options;
16
+ this.summary = new Map();
17
+ this.failed = new Set();
18
+ }
19
+ async init(data = []) { }
20
+ async failure(id) {
21
+ this.failed.add(id);
22
+ if (this.summary.has(id)) {
23
+ this.summary.delete(id);
24
+ }
25
+ }
26
+ async increment(id, data) {
27
+ var _a, _b, _c, _d, _e, _f;
28
+ if (this.failed.has(id))
29
+ return;
30
+ if (!this.summary.has(id)) {
31
+ this.summary.set(id, this.createSummary());
32
+ }
33
+ const current = this.summary.get(id);
34
+ for (let item of data) {
35
+ if (item.type === type_1.ECompareResultType.Missing)
36
+ continue;
37
+ const moduleType = item.name.startsWith("webpack/runtime")
38
+ ? "normal"
39
+ : "runtime";
40
+ // handle modules
41
+ if (item.type === type_1.ECompareResultType.OnlySource) {
42
+ current[`${type_1.ECompilerType.Rspack}|${moduleType}|${"modules"}`]++;
43
+ }
44
+ else if (item.type === type_1.ECompareResultType.OnlyDist) {
45
+ current[`${type_1.ECompilerType.Webpack}|${moduleType}|${"modules"}`]++;
46
+ }
47
+ else {
48
+ current[`${"common"}|${moduleType}|${"modules"}`]++;
49
+ }
50
+ // handle lines
51
+ current[`${type_1.ECompilerType.Rspack}|${moduleType}|${"lines"}`] +=
52
+ ((_a = item.lines) === null || _a === void 0 ? void 0 : _a.source) || 0;
53
+ current[`${type_1.ECompilerType.Webpack}|${moduleType}|${"lines"}`] +=
54
+ ((_b = item.lines) === null || _b === void 0 ? void 0 : _b.dist) || 0;
55
+ current[`${"common"}|${moduleType}|${"lines"}`] +=
56
+ ((_c = item.lines) === null || _c === void 0 ? void 0 : _c.common) || 0;
57
+ // handle lines in common modules
58
+ if (item.type === type_1.ECompareResultType.Same ||
59
+ item.type === type_1.ECompareResultType.Different) {
60
+ current[`${type_1.ECompilerType.Rspack}|${moduleType}|${"lines-in-common"}`] +=
61
+ ((_d = item.lines) === null || _d === void 0 ? void 0 : _d.source) || 0;
62
+ current[`${type_1.ECompilerType.Webpack}|${moduleType}|${"lines-in-common"}`] += ((_e = item.lines) === null || _e === void 0 ? void 0 : _e.dist) || 0;
63
+ current[`${"common"}|${moduleType}|${"lines-in-common"}`] +=
64
+ ((_f = item.lines) === null || _f === void 0 ? void 0 : _f.common) || 0;
65
+ }
66
+ }
67
+ }
68
+ async output() {
69
+ const chunks = [];
70
+ for (let [id, summary] of this.summary.entries()) {
71
+ chunks.push(this.stringifySummary(id, summary));
72
+ }
73
+ for (let id of this.failed.values()) {
74
+ chunks.push(`### ${id}\n\n> Failed\n\n`);
75
+ }
76
+ const output = [
77
+ ...(this.options.header || []),
78
+ chunks.join("\n---\n"),
79
+ ...(this.options.footer || [])
80
+ ].join("\n\n");
81
+ fs_extra_1.default.ensureFileSync(this.options.file);
82
+ fs_extra_1.default.writeFileSync(this.options.file, output);
83
+ }
84
+ stringifySummary(id, summary) {
85
+ let output = `### ${id}\n\n`;
86
+ for (let moduleType of ["runtime", "normal"]) {
87
+ const csv = [];
88
+ csv.push(`${moduleType.charAt(0).toUpperCase() + moduleType.slice(1)} Modules,Rspack Only,Common,Webpack Only,Common Percent`);
89
+ for (let dimen of [
90
+ "modules",
91
+ "lines",
92
+ "lines-in-common"
93
+ ]) {
94
+ const counts = [
95
+ type_1.ECompilerType.Rspack,
96
+ "common",
97
+ type_1.ECompilerType.Webpack
98
+ ].map(i => summary[`${i}|${moduleType}|${dimen}`]);
99
+ csv.push(`${dimen === "lines-in-common"
100
+ ? "Lines(Common Modules)"
101
+ : toFirstLetterUpperCase(dimen)},${counts.join(",")},${toPercent(counts[1] / (counts[0] + counts[1] + counts[2]))}`);
102
+ }
103
+ output += (0, csv_to_markdown_table_1.default)(csv.join("\n"), ",", true);
104
+ output += "\n\n";
105
+ }
106
+ if (this.options.report && GITHUB_RUN_ID) {
107
+ output += `> [View diff report](https://web-infra-dev.github.io/rspack-report-website/diff/${GITHUB_RUN_ID}/diff_${id}.html)`;
108
+ output += "\n\n";
109
+ }
110
+ return output;
111
+ }
112
+ createSummary() {
113
+ let result = {};
114
+ for (let i of [
115
+ type_1.ECompilerType.Rspack,
116
+ type_1.ECompilerType.Webpack,
117
+ "common"
118
+ ]) {
119
+ for (let j of ["runtime", "normal"]) {
120
+ for (let k of [
121
+ "modules",
122
+ "lines",
123
+ "lines-in-common"
124
+ ]) {
125
+ result[`${i}|${j}|${k}`] = 0;
126
+ }
127
+ }
128
+ }
129
+ return result;
130
+ }
131
+ }
132
+ exports.DiffStatsReporter = DiffStatsReporter;
@@ -0,0 +1,2 @@
1
+ export * from "./diff-stats";
2
+ export * from "./diff-html";
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./diff-stats"), exports);
18
+ __exportStar(require("./diff-html"), exports);
package/dist/type.d.ts CHANGED
@@ -54,6 +54,12 @@ export interface ITestProcessor {
54
54
  run?(context: ITestContext): Promise<void>;
55
55
  check?(context: ITestContext): Promise<unknown>;
56
56
  }
57
+ export interface ITestReporter<T> {
58
+ init(data?: T): Promise<void>;
59
+ increment(id: string, data: T): Promise<void>;
60
+ failure(id: string): Promise<void>;
61
+ output(): Promise<void>;
62
+ }
57
63
  export declare enum ECompareResultType {
58
64
  Same = "same",
59
65
  Missing = "missing",
@@ -65,6 +71,8 @@ export type TCompareModules = string[] | true;
65
71
  export type TCompareResult = {
66
72
  type: ECompareResultType;
67
73
  detail?: unknown;
74
+ source?: string;
75
+ dist?: string;
68
76
  lines?: {
69
77
  common: number;
70
78
  source: number;
@@ -81,3 +89,13 @@ export type TFileCompareResult = TCompareResult & {
81
89
  };
82
90
  modules: Partial<Record<"modules" | "runtimeModules", TModuleCompareResult[]>>;
83
91
  };
92
+ export type TDiffStatsItem = {
93
+ name: string;
94
+ source: string;
95
+ dist: string;
96
+ type: ECompareResultType;
97
+ };
98
+ export type TDiffStats = {
99
+ root: string;
100
+ data: Array<TDiffStatsItem>;
101
+ };
package/dist/type.js CHANGED
@@ -5,7 +5,7 @@ var ECompilerType;
5
5
  (function (ECompilerType) {
6
6
  ECompilerType["Rspack"] = "rspack";
7
7
  ECompilerType["Webpack"] = "webpack";
8
- })(ECompilerType = exports.ECompilerType || (exports.ECompilerType = {}));
8
+ })(ECompilerType || (exports.ECompilerType = ECompilerType = {}));
9
9
  var ECompareResultType;
10
10
  (function (ECompareResultType) {
11
11
  ECompareResultType["Same"] = "same";
@@ -13,4 +13,4 @@ var ECompareResultType;
13
13
  ECompareResultType["OnlyDist"] = "only-dist";
14
14
  ECompareResultType["OnlySource"] = "only-source";
15
15
  ECompareResultType["Different"] = "different";
16
- })(ECompareResultType = exports.ECompareResultType || (exports.ECompareResultType = {}));
16
+ })(ECompareResultType || (exports.ECompareResultType = ECompareResultType = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspack/test-tools",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "license": "MIT",
5
5
  "description": "Test tools for rspack",
6
6
  "main": "dist/index.js",
@@ -32,16 +32,31 @@
32
32
  "@babel/template": "7.22.15",
33
33
  "@babel/traverse": "7.23.2",
34
34
  "@babel/types": "7.23.0",
35
+ "csv-to-markdown-table": "^1.3.0",
35
36
  "deepmerge": "^4.3.1",
36
37
  "fs-extra": "^11.1.1",
37
38
  "jest-diff": "^29.7.0",
38
39
  "webpack-sources": "3.2.3",
39
- "@rspack/core": "0.4.1"
40
+ "@rspack/core": "0.4.2"
40
41
  },
41
42
  "devDependencies": {
43
+ "@arco-design/web-react": "^2.56.1",
44
+ "@monaco-editor/react": "^4.6.0",
45
+ "@types/prettier": "^2.7.2",
46
+ "@types/react": "^18.0.25",
47
+ "@types/react-dom": "^18.0.8",
42
48
  "@types/webpack": "5.28.3",
43
49
  "@types/webpack-sources": "3.2.0",
44
- "webpack": "5.89.0"
50
+ "core-js": "3.25.0",
51
+ "monaco-editor": "^0.34.1",
52
+ "normalize.css": "^8.0.0",
53
+ "prettier": "^2.8.3",
54
+ "react": "18.0.0",
55
+ "react-dom": "18.0.0",
56
+ "react-refresh": "0.13.0",
57
+ "typescript": "5.1.6",
58
+ "webpack": "5.89.0",
59
+ "@rspack/cli": "0.4.2"
45
60
  },
46
61
  "peerDependenciesMeta": {},
47
62
  "jest": {
@@ -53,6 +68,8 @@
53
68
  },
54
69
  "scripts": {
55
70
  "build": "rimraf dist/ && tsc -b ./tsconfig.build.json --force",
71
+ "build:viewer": "rspack build",
72
+ "dev:viewer": "rspack serve",
56
73
  "dev": "tsc -b -w"
57
74
  }
58
75
  }