@rspack/test-tools 0.4.4 → 0.4.5

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.
@@ -0,0 +1,16 @@
1
+ import { ITestReporter, TCompareModules } from "../type";
2
+ import { IFormatCodeOptions } from "./format-code";
3
+ export interface IDiffComparatorOptions {
4
+ rspackDist: string;
5
+ webpackDist: string;
6
+ files: string[];
7
+ modules?: TCompareModules;
8
+ runtimeModules?: TCompareModules;
9
+ reporters: ITestReporter<unknown>[];
10
+ formatOptions?: IFormatCodeOptions;
11
+ }
12
+ export declare class DiffComparator {
13
+ private options;
14
+ constructor(options: IDiffComparatorOptions);
15
+ compare(): Promise<void>;
16
+ }
@@ -0,0 +1,49 @@
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.DiffComparator = void 0;
7
+ const compare_1 = require("./compare");
8
+ const replace_runtime_module_name_1 = require("./replace-runtime-module-name");
9
+ const path_1 = __importDefault(require("path"));
10
+ const deepmerge_1 = __importDefault(require("deepmerge"));
11
+ class DiffComparator {
12
+ constructor(options) {
13
+ this.options = options;
14
+ }
15
+ async compare() {
16
+ for (let file of this.options.files) {
17
+ try {
18
+ const result = (0, compare_1.compareFile)(path_1.default.join(this.options.rspackDist, file), path_1.default.join(this.options.webpackDist, file), {
19
+ modules: this.options.modules,
20
+ runtimeModules: this.options.runtimeModules,
21
+ format: (0, deepmerge_1.default)({
22
+ replacements: {},
23
+ ignorePropertyQuotationMark: true,
24
+ ignoreModuleId: true,
25
+ ignoreModuleArguments: true,
26
+ ignoreBlockOnlyStatement: true,
27
+ ignoreSwcHelpersPath: true,
28
+ ignoreObjectPropertySequence: true,
29
+ ignoreCssFilePath: true
30
+ }, this.options.formatOptions || {}),
31
+ renameModule: replace_runtime_module_name_1.replaceRuntimeModuleName
32
+ });
33
+ for (let reporter of this.options.reporters) {
34
+ reporter.increment(file, result.modules["modules"] || []);
35
+ }
36
+ for (let reporter of this.options.reporters) {
37
+ reporter.increment(file, result.modules["runtimeModules"] || []);
38
+ }
39
+ }
40
+ catch (e) {
41
+ for (let reporter of this.options.reporters) {
42
+ reporter.failure(file);
43
+ }
44
+ }
45
+ }
46
+ await Promise.all(this.options.reporters.map(r => r.output()));
47
+ }
48
+ }
49
+ exports.DiffComparator = DiffComparator;
@@ -2,3 +2,4 @@ export * from "./replace-module-argument";
2
2
  export * from "./replace-runtime-module-name";
3
3
  export * from "./format-code";
4
4
  export * from "./compare";
5
+ export * from "./comparator";
@@ -18,3 +18,4 @@ __exportStar(require("./replace-module-argument"), exports);
18
18
  __exportStar(require("./replace-runtime-module-name"), exports);
19
19
  __exportStar(require("./format-code"), exports);
20
20
  __exportStar(require("./compare"), exports);
21
+ __exportStar(require("./comparator"), exports);
package/dist/index.d.ts CHANGED
@@ -2,3 +2,5 @@ export * from "./type";
2
2
  export * from "./test/tester";
3
3
  export * from "./processor";
4
4
  export * from "./reporter";
5
+ export * from "./compare";
6
+ export * from "./plugin";
package/dist/index.js CHANGED
@@ -18,3 +18,5 @@ __exportStar(require("./type"), exports);
18
18
  __exportStar(require("./test/tester"), exports);
19
19
  __exportStar(require("./processor"), exports);
20
20
  __exportStar(require("./reporter"), exports);
21
+ __exportStar(require("./compare"), exports);
22
+ __exportStar(require("./plugin"), exports);
@@ -0,0 +1,2 @@
1
+ export * from "./rspack-diff-config-plugin";
2
+ export * from "./webpack-diff-config-plugin";
@@ -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("./rspack-diff-config-plugin"), exports);
18
+ __exportStar(require("./webpack-diff-config-plugin"), exports);
@@ -0,0 +1,7 @@
1
+ import { Compiler, RspackPluginInstance } from "@rspack/core";
2
+ export declare class RspackDiffConfigPlugin implements RspackPluginInstance {
3
+ private rspackPath;
4
+ name: string;
5
+ constructor(rspackPath: string);
6
+ apply(compiler: Compiler): void;
7
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RspackDiffConfigPlugin = void 0;
4
+ const PLUGIN_NAME = "RspackDiffConfigPlugin";
5
+ class RspackDiffConfigPlugin {
6
+ constructor(rspackPath) {
7
+ this.rspackPath = rspackPath;
8
+ this.name = PLUGIN_NAME;
9
+ process.env["RSPACK_DIFF"] = "true"; // enable rspack diff
10
+ }
11
+ apply(compiler) {
12
+ var _a, _b, _c;
13
+ var _d;
14
+ const { options } = compiler;
15
+ options.mode = "development";
16
+ options.devtool = false;
17
+ (_a = options.optimization) !== null && _a !== void 0 ? _a : (options.optimization = {});
18
+ options.optimization.minimize = false;
19
+ options.optimization.chunkIds = "named";
20
+ options.optimization.moduleIds = "named";
21
+ options.optimization.mangleExports = false;
22
+ (_b = options.experiments) !== null && _b !== void 0 ? _b : (options.experiments = {});
23
+ (_c = (_d = options.experiments).rspackFuture) !== null && _c !== void 0 ? _c : (_d.rspackFuture = {});
24
+ options.experiments.rspackFuture.disableTransformByDefault = true;
25
+ }
26
+ }
27
+ exports.RspackDiffConfigPlugin = RspackDiffConfigPlugin;
@@ -0,0 +1,7 @@
1
+ import { Compiler } from "webpack";
2
+ export declare class WebpackDiffConfigPlugin {
3
+ private webpackPath;
4
+ name: string;
5
+ constructor(webpackPath: string);
6
+ apply(compiler: Compiler): void;
7
+ }
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebpackDiffConfigPlugin = void 0;
4
+ const webpack_module_placeholder_plugin_1 = require("./webpack-module-placeholder-plugin");
5
+ const PLUGIN_NAME = "WebpackDiffConfigPlugin";
6
+ class WebpackDiffConfigPlugin {
7
+ constructor(webpackPath) {
8
+ this.webpackPath = webpackPath;
9
+ this.name = PLUGIN_NAME;
10
+ }
11
+ apply(compiler) {
12
+ var _a, _b, _c;
13
+ var _d;
14
+ const { options } = compiler;
15
+ options.mode = "development";
16
+ options.devtool = false;
17
+ (_a = options.optimization) !== null && _a !== void 0 ? _a : (options.optimization = {});
18
+ options.optimization.minimize = false;
19
+ options.optimization.chunkIds = "named";
20
+ options.optimization.moduleIds = "named";
21
+ options.optimization.mangleExports = false;
22
+ options.optimization.concatenateModules = false;
23
+ (_b = options.output) !== null && _b !== void 0 ? _b : (options.output = {});
24
+ options.output.pathinfo = false;
25
+ (_c = (_d = options.output).environment) !== null && _c !== void 0 ? _c : (_d.environment = {});
26
+ options.output.environment.arrowFunction = false;
27
+ options.output.environment.bigIntLiteral = false;
28
+ options.output.environment.const = false;
29
+ options.output.environment.destructuring = false;
30
+ options.output.environment.dynamicImport = false;
31
+ options.output.environment.dynamicImportInWorker = false;
32
+ options.output.environment.forOf = false;
33
+ options.output.environment.globalThis = false;
34
+ options.output.environment.module = false;
35
+ options.output.environment.optionalChaining = false;
36
+ options.output.environment.templateLiteral = false;
37
+ new webpack_module_placeholder_plugin_1.WebpackModulePlaceholderPlugin(this.webpackPath).apply(compiler);
38
+ }
39
+ }
40
+ exports.WebpackDiffConfigPlugin = WebpackDiffConfigPlugin;
@@ -0,0 +1,5 @@
1
+ export declare class WebpackModulePlaceholderPlugin {
2
+ private webpackPath;
3
+ constructor(webpackPath: string);
4
+ apply(compiler: any): void;
5
+ }
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebpackModulePlaceholderPlugin = void 0;
4
+ // @ts-nocheck
5
+ const { ConcatSource, RawSource, CachedSource, PrefixSource } = require("webpack-sources");
6
+ const path = require("path");
7
+ function createRenderRuntimeModulesFn(Template) {
8
+ return function renderRuntimeModules(runtimeModules, renderContext) {
9
+ const source = new ConcatSource();
10
+ for (const module of runtimeModules) {
11
+ const codeGenerationResults = renderContext.codeGenerationResults;
12
+ let runtimeSource;
13
+ if (codeGenerationResults) {
14
+ runtimeSource = codeGenerationResults.getSource(module, renderContext.chunk.runtime, "runtime");
15
+ }
16
+ else {
17
+ const codeGenResult = module.codeGeneration({
18
+ chunkGraph: renderContext.chunkGraph,
19
+ dependencyTemplates: renderContext.dependencyTemplates,
20
+ moduleGraph: renderContext.moduleGraph,
21
+ runtimeTemplate: renderContext.runtimeTemplate,
22
+ runtime: renderContext.chunk.runtime,
23
+ codeGenerationResults
24
+ });
25
+ if (!codeGenResult)
26
+ continue;
27
+ runtimeSource = codeGenResult.sources.get("runtime");
28
+ }
29
+ if (runtimeSource) {
30
+ let identifier = module.identifier();
31
+ source.add(Template.toNormalComment(`start::${identifier}`) + "\n");
32
+ if (!module.shouldIsolate()) {
33
+ source.add(runtimeSource);
34
+ source.add("\n\n");
35
+ }
36
+ else if (renderContext.runtimeTemplate.supportsArrowFunction()) {
37
+ source.add("(() => {\n");
38
+ source.add(new PrefixSource("\t", runtimeSource));
39
+ source.add("\n})();\n\n");
40
+ }
41
+ else {
42
+ source.add("!function() {\n");
43
+ source.add(new PrefixSource("\t", runtimeSource));
44
+ source.add("\n}();\n\n");
45
+ }
46
+ source.add(Template.toNormalComment(`end::${identifier}`) + "\n");
47
+ }
48
+ }
49
+ return source;
50
+ };
51
+ }
52
+ const caches = new WeakMap();
53
+ class WebpackModulePlaceholderPlugin {
54
+ constructor(webpackPath) {
55
+ this.webpackPath = webpackPath;
56
+ const Template = require(path.join(path.dirname(this.webpackPath), "Template.js"));
57
+ Template.renderRuntimeModules = createRenderRuntimeModulesFn(Template);
58
+ }
59
+ apply(compiler) {
60
+ const JavascriptModulesPlugin = require(path.join(path.dirname(this.webpackPath), "javascript/JavascriptModulesPlugin.js"));
61
+ compiler.hooks.compilation.tap("RuntimeDiffPlugin", compilation => {
62
+ const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
63
+ hooks.inlineInRuntimeBailout.tap("RuntimeDiffPlugin", () => "not allow inline startup");
64
+ hooks.renderModulePackage.tap("RuntimeDiffPlugin", (moduleSource, module, { chunk, chunkGraph, moduleGraph, runtimeTemplate }) => {
65
+ const { requestShortener } = runtimeTemplate;
66
+ let cacheEntry;
67
+ let cache = caches.get(requestShortener);
68
+ if (cache === undefined) {
69
+ caches.set(requestShortener, (cache = new WeakMap()));
70
+ cache.set(module, (cacheEntry = {
71
+ header: undefined,
72
+ footer: undefined,
73
+ full: new WeakMap()
74
+ }));
75
+ }
76
+ else {
77
+ cacheEntry = cache.get(module);
78
+ if (cacheEntry === undefined) {
79
+ cache.set(module, (cacheEntry = {
80
+ header: undefined,
81
+ footer: undefined,
82
+ full: new WeakMap()
83
+ }));
84
+ }
85
+ else {
86
+ const cachedSource = cacheEntry.full.get(moduleSource);
87
+ if (cachedSource !== undefined)
88
+ return cachedSource;
89
+ }
90
+ }
91
+ const source = new ConcatSource();
92
+ let header = cacheEntry.header;
93
+ let footer = cacheEntry.footer;
94
+ if (header === undefined) {
95
+ const req = module.readableIdentifier(requestShortener);
96
+ let reqStr = req.replace(/\*\//g, "*_/");
97
+ // handle css module identifier
98
+ if (reqStr.startsWith("css ")) {
99
+ reqStr = reqStr.replace(/^css[\s]+/, "").trim();
100
+ }
101
+ header = new RawSource(`\n/* start::${reqStr} */\n`);
102
+ footer = new RawSource(`\n/* end::${reqStr} */\n`);
103
+ cacheEntry.header = header;
104
+ cacheEntry.footer = footer;
105
+ }
106
+ source.add(header);
107
+ source.add(moduleSource);
108
+ source.add(footer);
109
+ const cachedSource = new CachedSource(source);
110
+ cacheEntry.full.set(moduleSource, cachedSource);
111
+ return cachedSource;
112
+ });
113
+ });
114
+ }
115
+ }
116
+ exports.WebpackModulePlaceholderPlugin = WebpackModulePlaceholderPlugin;
@@ -6,10 +6,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.DiffProcessor = void 0;
7
7
  const type_1 = require("../type");
8
8
  const path_1 = __importDefault(require("path"));
9
- const module_placeholder_plugin_1 = require("../webpack/module-placeholder-plugin");
10
9
  const compare_1 = require("../compare");
11
10
  const helper_1 = require("../helper");
12
11
  const deepmerge_1 = __importDefault(require("deepmerge"));
12
+ const plugin_1 = require("../plugin");
13
13
  class DiffProcessor {
14
14
  constructor(options) {
15
15
  this.options = options;
@@ -67,59 +67,19 @@ class DiffProcessor {
67
67
  entry: path_1.default.join(src, "./src/index.js"),
68
68
  context: src,
69
69
  output: {
70
+ path: dist,
70
71
  filename: "bundle.js",
71
72
  chunkFilename: "[name].chunk.js"
72
73
  },
73
- mode: "development",
74
- devtool: false,
75
- optimization: {
76
- chunkIds: "named",
77
- moduleIds: "named"
78
- }
74
+ plugins: [
75
+ type === type_1.ECompilerType.Webpack &&
76
+ new plugin_1.WebpackDiffConfigPlugin(this.options.webpackPath),
77
+ type === type_1.ECompilerType.Rspack &&
78
+ new plugin_1.RspackDiffConfigPlugin(this.options.rspackPath)
79
+ ].filter(Boolean)
80
+ }, {
81
+ arrayMerge: (a, b) => [...a, ...b]
79
82
  });
80
- if (type === type_1.ECompilerType.Webpack) {
81
- result = (0, deepmerge_1.default)(result, {
82
- output: {
83
- pathinfo: false,
84
- environment: {
85
- arrowFunction: false,
86
- bigIntLiteral: false,
87
- const: false,
88
- destructuring: false,
89
- dynamicImport: false,
90
- dynamicImportInWorker: false,
91
- forOf: false,
92
- globalThis: false,
93
- module: false,
94
- optionalChaining: false,
95
- templateLiteral: false
96
- },
97
- path: dist
98
- },
99
- optimization: {
100
- mangleExports: false,
101
- concatenateModules: false
102
- },
103
- plugins: [(0, module_placeholder_plugin_1.createModulePlaceholderPlugin)(this.options.webpackPath)]
104
- }, {
105
- arrayMerge: (a, b) => [...a, ...b]
106
- });
107
- }
108
- if (type === type_1.ECompilerType.Rspack) {
109
- result = (0, deepmerge_1.default)(result, {
110
- output: {
111
- path: dist
112
- },
113
- optimization: {
114
- mangleExports: false
115
- },
116
- experiments: {
117
- rspackFuture: {
118
- disableTransformByDefault: true
119
- }
120
- }
121
- });
122
- }
123
83
  return result;
124
84
  }
125
85
  createFormatOptions() {
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.DiffHtmlReporter = void 0;
7
7
  const fs_extra_1 = __importDefault(require("fs-extra"));
8
8
  const path_1 = __importDefault(require("path"));
9
- const VIEWER_DIR = path_1.default.join(__dirname, "../viewer");
9
+ const VIEWER_DIR = path_1.default.join(__dirname, "../../template");
10
10
  const DIFF_STATS_PLACEHOLDER = "$$RSPACK_DIFF_STATS_PLACEHOLDER$$";
11
11
  const DEFAULT_IGNORE = /node_modules/;
12
12
  class DiffHtmlReporter {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspack/test-tools",
3
- "version": "0.4.4",
3
+ "version": "0.4.5",
4
4
  "license": "MIT",
5
5
  "description": "Test tools for rspack",
6
6
  "main": "dist/index.js",
@@ -13,7 +13,8 @@
13
13
  },
14
14
  "files": [
15
15
  "client",
16
- "dist"
16
+ "dist",
17
+ "template"
17
18
  ],
18
19
  "publishConfig": {
19
20
  "access": "public"
@@ -37,7 +38,7 @@
37
38
  "fs-extra": "^11.1.1",
38
39
  "jest-diff": "^29.7.0",
39
40
  "webpack-sources": "3.2.3",
40
- "@rspack/core": "0.4.4"
41
+ "@rspack/core": "0.4.5"
41
42
  },
42
43
  "devDependencies": {
43
44
  "@arco-design/web-react": "^2.56.1",
@@ -56,7 +57,7 @@
56
57
  "react-refresh": "0.13.0",
57
58
  "typescript": "5.1.6",
58
59
  "webpack": "5.89.0",
59
- "@rspack/cli": "0.4.4"
60
+ "@rspack/cli": "0.4.5"
60
61
  },
61
62
  "peerDependenciesMeta": {},
62
63
  "jest": {
@@ -67,7 +68,7 @@
67
68
  "testEnvironment": "../../scripts/test/patch-node-env.cjs"
68
69
  },
69
70
  "scripts": {
70
- "build": "rimraf dist/ && tsc -b ./tsconfig.build.json --force",
71
+ "build": "tsc -b ./tsconfig.build.json",
71
72
  "build:viewer": "rspack build",
72
73
  "dev:viewer": "rspack serve",
73
74
  "dev": "tsc -b -w"