@rspack/test-tools 0.3.14-canary-020060b-20231122074120

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.
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-present Bytedance, Inc. and its affiliates.
4
+
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,16 @@
1
+ <picture>
2
+ <source media="(prefers-color-scheme: dark)" srcset="https://lf3-static.bytednsdoc.com/obj/eden-cn/rjhwzy/ljhwZthlaukjlkulzlp/rspack-banner-1610-dark.png">
3
+ <img alt="Rspack Banner" src="https://lf3-static.bytednsdoc.com/obj/eden-cn/rjhwzy/ljhwZthlaukjlkulzlp/rspack-banner-1610.png">
4
+ </picture>
5
+
6
+ # @rspack/test-tools
7
+
8
+ Test tools for rspack.
9
+
10
+ ## Documentation
11
+
12
+ See [https://rspack.dev](https://rspack.dev) for details.
13
+
14
+ ## License
15
+
16
+ Rspack is [MIT licensed](https://github.com/web-infra-dev/rspack/blob/main/LICENSE).
@@ -0,0 +1,11 @@
1
+ import { IFormatCodeOptions } from "./format-code";
2
+ import { TCompareModules, TCompareResult, TFileCompareResult, TModuleCompareResult } from "../type";
3
+ export interface ICompareOptions {
4
+ modules?: TCompareModules;
5
+ runtimeModules?: TCompareModules;
6
+ format: IFormatCodeOptions;
7
+ renameModule?: (name: string) => string;
8
+ }
9
+ export declare function compareFile(sourceFile: string, distFile: string, compareOptions: ICompareOptions): TFileCompareResult;
10
+ export declare function compareModules(modules: string[], sourceModules: Map<string, string>, distModules: Map<string, string>, formatOptions: IFormatCodeOptions): TModuleCompareResult[];
11
+ export declare function compareContent(sourceContent: string | false, distContent: string | false): TCompareResult;
@@ -0,0 +1,145 @@
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.compareContent = exports.compareModules = exports.compareFile = void 0;
7
+ const jest_diff_1 = require("jest-diff");
8
+ const format_code_1 = require("./format-code");
9
+ const replace_runtime_module_name_1 = require("./replace-runtime-module-name");
10
+ const helper_1 = require("../helper");
11
+ const fs_extra_1 = __importDefault(require("fs-extra"));
12
+ const type_1 = require("../type");
13
+ function compareFile(sourceFile, distFile, compareOptions) {
14
+ const result = {
15
+ type: type_1.ECompareResultType.Same,
16
+ file: {
17
+ source: sourceFile,
18
+ dist: distFile
19
+ },
20
+ modules: {}
21
+ };
22
+ const sourceExists = fs_extra_1.default.existsSync(sourceFile);
23
+ const distExists = fs_extra_1.default.existsSync(distFile);
24
+ if (!sourceExists && !distExists) {
25
+ result.type = type_1.ECompareResultType.Missing;
26
+ return result;
27
+ }
28
+ else if (!sourceExists && distExists) {
29
+ result.type = type_1.ECompareResultType.OnlyDist;
30
+ return result;
31
+ }
32
+ else if (sourceExists && !distExists) {
33
+ result.type = type_1.ECompareResultType.OnlySource;
34
+ return result;
35
+ }
36
+ const sourceContent = (0, replace_runtime_module_name_1.replaceRuntimeModuleName)(fs_extra_1.default.readFileSync(sourceFile, "utf-8"));
37
+ const distContent = (0, replace_runtime_module_name_1.replaceRuntimeModuleName)(fs_extra_1.default.readFileSync(distFile, "utf-8"));
38
+ // const compareContentResult = compareContent(sourceContent, distContent);
39
+ // result.detail = compareContentResult.detail;
40
+ // result.lines = compareContentResult.lines;
41
+ result.type = type_1.ECompareResultType.Different;
42
+ const sourceModules = (0, helper_1.parseModules)(sourceContent);
43
+ const distModules = (0, helper_1.parseModules)(distContent);
44
+ for (let type of ["modules", "runtimeModules"]) {
45
+ const t = type;
46
+ let moduleList = [];
47
+ if (compareOptions[t] === true) {
48
+ moduleList = [
49
+ ...sourceModules[t].keys(),
50
+ ...distModules[t].keys()
51
+ ].filter((i, idx, arr) => arr.indexOf(i) === idx);
52
+ }
53
+ else if (Array.isArray(compareOptions[t])) {
54
+ moduleList = compareOptions[t];
55
+ }
56
+ else {
57
+ continue;
58
+ }
59
+ if (typeof compareOptions.renameModule === "function") {
60
+ moduleList = moduleList.map(compareOptions.renameModule);
61
+ }
62
+ result.modules[t] = compareModules(moduleList, sourceModules[t], distModules[t], compareOptions.format);
63
+ }
64
+ return result;
65
+ }
66
+ exports.compareFile = compareFile;
67
+ function compareModules(modules, sourceModules, distModules, formatOptions) {
68
+ const compareResults = [];
69
+ for (let name of modules) {
70
+ const renamed = (0, replace_runtime_module_name_1.replaceRuntimeModuleName)(name);
71
+ const sourceContent = sourceModules.has(renamed) &&
72
+ (0, format_code_1.formatCode)(sourceModules.get(renamed), formatOptions);
73
+ const distContent = distModules.has(renamed) &&
74
+ (0, format_code_1.formatCode)(distModules.get(renamed), formatOptions);
75
+ compareResults.push({
76
+ ...compareContent(sourceContent, distContent),
77
+ name
78
+ });
79
+ }
80
+ return compareResults;
81
+ }
82
+ exports.compareModules = compareModules;
83
+ function compareContent(sourceContent, distContent) {
84
+ if (sourceContent) {
85
+ if (distContent) {
86
+ if (sourceContent === distContent) {
87
+ const lines = sourceContent.trim().split("\n").length;
88
+ return {
89
+ type: type_1.ECompareResultType.Same,
90
+ lines: {
91
+ source: lines,
92
+ common: lines,
93
+ dist: lines
94
+ }
95
+ };
96
+ }
97
+ else {
98
+ const difference = (0, jest_diff_1.diffStringsUnified)(sourceContent.trim(), distContent.trim());
99
+ const diffLines = (0, jest_diff_1.diffLinesRaw)(sourceContent.trim().split("\n"), distContent.trim().split("\n"));
100
+ return {
101
+ type: type_1.ECompareResultType.Different,
102
+ detail: difference,
103
+ lines: {
104
+ source: diffLines.filter(l => l[0] < 0).length,
105
+ common: diffLines.filter(l => l[0] === 0).length,
106
+ dist: diffLines.filter(l => l[0] > 0).length
107
+ }
108
+ };
109
+ }
110
+ }
111
+ else {
112
+ return {
113
+ type: type_1.ECompareResultType.OnlySource,
114
+ lines: {
115
+ source: sourceContent.trim().split("\n").length,
116
+ common: 0,
117
+ dist: 0
118
+ }
119
+ };
120
+ }
121
+ }
122
+ else {
123
+ if (distContent) {
124
+ return {
125
+ type: type_1.ECompareResultType.OnlyDist,
126
+ lines: {
127
+ source: 0,
128
+ common: 0,
129
+ dist: distContent.trim().split("\n").length
130
+ }
131
+ };
132
+ }
133
+ else {
134
+ return {
135
+ type: type_1.ECompareResultType.Missing,
136
+ lines: {
137
+ source: 0,
138
+ common: 0,
139
+ dist: 0
140
+ }
141
+ };
142
+ }
143
+ }
144
+ }
145
+ exports.compareContent = compareContent;
@@ -0,0 +1,7 @@
1
+ export interface IFormatCodeOptions {
2
+ replacements?: Record<string, string>;
3
+ ignorePropertyQuotationMark: boolean;
4
+ ignoreModuleId: boolean;
5
+ ignoreModuleArugments: boolean;
6
+ }
7
+ export declare function formatCode(raw: string, options: IFormatCodeOptions): string;
@@ -0,0 +1,69 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.formatCode = void 0;
30
+ const parser_1 = require("@babel/parser");
31
+ const generator_1 = __importDefault(require("@babel/generator"));
32
+ const traverse_1 = __importDefault(require("@babel/traverse"));
33
+ const T = __importStar(require("@babel/types"));
34
+ const replace_module_argument_1 = require("./replace-module-argument");
35
+ function formatCode(raw, options) {
36
+ const ast = (0, parser_1.parse)(raw, {
37
+ sourceType: "unambiguous"
38
+ });
39
+ (0, traverse_1.default)(ast, {
40
+ ObjectProperty(path) {
41
+ if (options.ignorePropertyQuotationMark) {
42
+ const keyPath = path.get("key");
43
+ if (keyPath.isIdentifier()) {
44
+ keyPath.replaceWith(T.stringLiteral(keyPath.node.name));
45
+ }
46
+ }
47
+ },
48
+ Identifier(path) {
49
+ if (options.ignoreModuleId) {
50
+ path.node.name = path.node.name.replace(/__WEBPACK_IMPORTED_MODULE_\d+__/g, "__WEBPACK_IMPORTED_MODULE_xxx__");
51
+ }
52
+ }
53
+ });
54
+ let result = (0, generator_1.default)(ast, {
55
+ comments: false,
56
+ compact: false,
57
+ concise: false
58
+ }).code;
59
+ if (options.ignoreModuleArugments) {
60
+ result = (0, replace_module_argument_1.replaceModuleArgument)(result);
61
+ }
62
+ if (options.replacements) {
63
+ for (let [key, value] of Object.entries(options.replacements)) {
64
+ result = result.split(key).join(value);
65
+ }
66
+ }
67
+ return result.trim();
68
+ }
69
+ exports.formatCode = formatCode;
@@ -0,0 +1,4 @@
1
+ export * from "./replace-module-argument";
2
+ export * from "./replace-runtime-module-name";
3
+ export * from "./format-code";
4
+ export * from "./compare";
@@ -0,0 +1,20 @@
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("./replace-module-argument"), exports);
18
+ __exportStar(require("./replace-runtime-module-name"), exports);
19
+ __exportStar(require("./format-code"), exports);
20
+ __exportStar(require("./compare"), exports);
@@ -0,0 +1 @@
1
+ export declare function replaceModuleArgument(raw: string): string;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.replaceModuleArgument = void 0;
4
+ function replaceModuleArgument(raw) {
5
+ return raw.trim().replace(/^\(function \([\w_,\s]+\) {/, "(function () {");
6
+ }
7
+ exports.replaceModuleArgument = replaceModuleArgument;
@@ -0,0 +1 @@
1
+ export declare function replaceRuntimeModuleName(raw: string): string;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.replaceRuntimeModuleName = void 0;
4
+ const RUNTIME_MODULE_NAME_MAPPING = {
5
+ "webpack/runtime/auto_public_path": "webpack/runtime/publicPath",
6
+ "webpack/runtime/public_path": "webpack/runtime/publicPath",
7
+ "webpack/runtime/async_module": "webpack/runtime/async module",
8
+ "webpack/runtime/base_uri": "webpack/runtime/base uri",
9
+ "webpack/runtime/chunk_name": "webpack/runtime/chunkName",
10
+ "webpack/runtime/compat_get_default_export": "webpack/runtime/compat get default export",
11
+ "webpack/runtime/compat": "webpack/runtime/compat",
12
+ "webpack/runtime/create_fake_namespace_object": "webpack/runtime/create fake namespace object",
13
+ "webpack/runtime/create_script": "webpack/runtime/trusted types script",
14
+ "webpack/runtime/define_property_getters": "webpack/runtime/define property getters",
15
+ "webpack/runtime/ensure_chunk": "webpack/runtime/ensure chunk",
16
+ "webpack/runtime/get_full_hash": "webpack/runtime/getFullHash",
17
+ "webpack/runtime/get_trusted_types_policy": "webpack/runtime/trusted types policy",
18
+ "webpack/runtime/global": "webpack/runtime/global",
19
+ "webpack/runtime/has_own_property": "webpack/runtime/hasOwnProperty shorthand",
20
+ "webpack/runtime/load_script": "webpack/runtime/load script",
21
+ "webpack/runtime/make_namespace_object": "webpack/runtime/make namespace object",
22
+ "webpack/runtime/nonce": "webpack/runtime/nonce",
23
+ "webpack/runtime/on_chunk_loaded": "webpack/runtime/chunk loaded",
24
+ "webpack/runtime/relative_url": "webpack/runtime/relative url",
25
+ "webpack/runtime/runtime_id": "webpack/runtime/runtimeId",
26
+ "webpack/runtime/startup_chunk_dependencies": "webpack/runtime/startup chunk dependencies",
27
+ "webpack/runtime/startup_entrypoint": "webpack/runtime/startup entrypoint",
28
+ "webpack/runtime/system_context": "webpack/runtime/__system_context__",
29
+ "webpack/runtime/chunk_prefetch_startup": "webpack/runtime/startup prefetch",
30
+ "webpack/runtime/chunk_prefetch_trigger": "webpack/runtime/chunk prefetch trigger",
31
+ "webpack/runtime/chunk_preload_trigger": "webpack/runtime/chunk preload trigger",
32
+ "webpack/runtime/css_loading": "webpack/runtime/css loading",
33
+ "webpack/runtime/async_wasm_loading": "webpack/runtime/wasm loading",
34
+ "webpack/runtime/hot_module_replacement": "webpack/runtime/hot module replacement",
35
+ "webpack/runtime/readfile_chunk_loading": "webpack/runtime/readFile chunk loading",
36
+ "webpack/runtime/require_chunk_loading": "webpack/runtime/require chunk loading",
37
+ "webpack/runtime/import_scripts_chunk_loading": "webpack/runtime/importScripts chunk loading",
38
+ "webpack/runtime/module_chunk_loading": "webpack/runtime/import chunk loading",
39
+ "webpack/runtime/export_webpack_runtime": "webpack/runtime/export webpack runtime",
40
+ "webpack/runtime/jsonp_chunk_loading": "webpack/runtime/jsonp chunk loading",
41
+ "webpack/runtime/remote": "webpack/runtime/remotes loading",
42
+ "webpack/runtime/share": "webpack/runtime/sharing",
43
+ "webpack/runtime/consume_shared": "webpack/runtime/consumes",
44
+ "webpack/runtime/harmony_module_decorator": "webpack/runtime/harmony module decorator",
45
+ "webpack/runtime/node_module_decorator": "webpack/runtime/node module decorator",
46
+ // module name with parameters
47
+ "webpack/runtime/get_chunk_filename": "webpack/runtime/get $1 chunk filename",
48
+ "webpack/runtime/get_main_filename": "webpack/runtime/get $1 filename",
49
+ "webpack/runtime/chunk_prefetch_function": "webpack/runtime/chunk $1 function"
50
+ };
51
+ const RUNTIME_MODULE_PARAM_REGEX = {
52
+ "webpack/runtime/get_chunk_filename": /webpack\/runtime\/get_chunk_filename\/([\w.\-_\s]+)(\*\/)?/g,
53
+ "webpack/runtime/get_main_filename": /webpack\/runtime\/get_main_filename\/([\w.\-_\s]+)(\*\/)?/g,
54
+ "webpack/runtime/chunk_prefetch_function": /webpack\/runtime\/chunk_prefetch_function\/([\w.\-_\s]+)(\*\/)?/g
55
+ };
56
+ function replaceRuntimeModuleName(raw) {
57
+ for (let [rspackName, webpackName] of Object.entries(RUNTIME_MODULE_NAME_MAPPING)) {
58
+ if (RUNTIME_MODULE_PARAM_REGEX[rspackName]) {
59
+ raw = raw.replace(RUNTIME_MODULE_PARAM_REGEX[rspackName], (full, $1, $2) => {
60
+ return webpackName.replace("$1", $1.trim()) + ($2 ? " */" : "");
61
+ });
62
+ }
63
+ else {
64
+ raw = raw.split(rspackName).join(webpackName);
65
+ }
66
+ }
67
+ return raw;
68
+ }
69
+ exports.replaceRuntimeModuleName = replaceRuntimeModuleName;
@@ -0,0 +1,12 @@
1
+ import { ECompilerType, ITestCompilerManager, ITestContext, TCompiler, TCompilerOptions, TCompilerStats } from "./type";
2
+ export declare class TestCompilerManager<T extends ECompilerType> implements ITestCompilerManager<T> {
3
+ private compilerOptions;
4
+ private compilerInstance;
5
+ private compilerStats;
6
+ private runResult;
7
+ options(context: ITestContext, fn: (options: TCompilerOptions<T>) => TCompilerOptions<T>): void;
8
+ compiler(context: ITestContext, fn: (options: TCompilerOptions<T>, compiler: TCompiler<T> | null) => TCompiler<T> | null): void;
9
+ stats(context: ITestContext, fn: (compiler: TCompiler<T> | null, stats: TCompilerStats<T> | null) => TCompilerStats<T> | null): void;
10
+ result(context: ITestContext, fn: <R>(compiler: TCompiler<T> | null, result: R) => R): void;
11
+ build(context: ITestContext, fn: (compiler: TCompiler<T>) => Promise<void>): Promise<void>;
12
+ }
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TestCompilerManager = void 0;
4
+ class TestCompilerManager {
5
+ constructor() {
6
+ this.compilerOptions = {};
7
+ this.compilerInstance = null;
8
+ this.compilerStats = null;
9
+ }
10
+ options(context, fn) {
11
+ try {
12
+ const newOptions = fn(this.compilerOptions);
13
+ if (newOptions) {
14
+ this.compilerOptions = newOptions;
15
+ }
16
+ }
17
+ catch (e) {
18
+ context.emitError(e);
19
+ }
20
+ }
21
+ compiler(context, fn) {
22
+ try {
23
+ const newCompiler = fn(this.compilerOptions, this.compilerInstance);
24
+ if (newCompiler) {
25
+ this.compilerInstance = newCompiler;
26
+ }
27
+ }
28
+ catch (e) {
29
+ context.emitError(e);
30
+ }
31
+ }
32
+ stats(context, fn) {
33
+ try {
34
+ const newStats = fn(this.compilerInstance, this.compilerStats);
35
+ if (newStats) {
36
+ this.compilerStats = newStats;
37
+ }
38
+ }
39
+ catch (e) {
40
+ context.emitError(e);
41
+ }
42
+ }
43
+ result(context, fn) {
44
+ try {
45
+ const newResult = fn(this.compilerInstance, this.runResult);
46
+ if (newResult) {
47
+ this.runResult = newResult;
48
+ }
49
+ }
50
+ catch (e) {
51
+ context.emitError(e);
52
+ }
53
+ }
54
+ async build(context, fn) {
55
+ if (!this.compilerInstance) {
56
+ context.emitError(new Error("Build failed: compiler not exists"));
57
+ }
58
+ try {
59
+ await fn(this.compilerInstance);
60
+ }
61
+ catch (e) {
62
+ context.emitError(e);
63
+ }
64
+ }
65
+ }
66
+ exports.TestCompilerManager = TestCompilerManager;
@@ -0,0 +1,3 @@
1
+ export * from "./read-config-file";
2
+ export * from "./run-build";
3
+ export * from "./parse-modules";
@@ -0,0 +1,19 @@
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("./read-config-file"), exports);
18
+ __exportStar(require("./run-build"), exports);
19
+ __exportStar(require("./parse-modules"), exports);
@@ -0,0 +1,4 @@
1
+ export declare function parseModules(content: string): {
2
+ modules: Map<string, string>;
3
+ runtimeModules: Map<string, string>;
4
+ };
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseModules = void 0;
4
+ const BOOTSTRAP_SPLIT_LINE = "/************************************************************************/";
5
+ const MODULE_START_FLAG = "/* start::";
6
+ const MODULE_END_FLAG = "/* end::";
7
+ const MODULE_FLAG_END = " */";
8
+ function getStringBetween(raw, position, start, end) {
9
+ const startFlagIndex = raw.indexOf(start, position);
10
+ if (startFlagIndex === -1) {
11
+ return {
12
+ result: null,
13
+ remain: position
14
+ };
15
+ }
16
+ const endFlagIndex = raw.indexOf(end, startFlagIndex + start.length);
17
+ if (endFlagIndex === -1) {
18
+ return {
19
+ result: null,
20
+ remain: position
21
+ };
22
+ }
23
+ return {
24
+ result: raw.slice(startFlagIndex + start.length, endFlagIndex),
25
+ remain: endFlagIndex + end.length
26
+ };
27
+ }
28
+ function parseModules(content) {
29
+ const modules = new Map();
30
+ const runtimeModules = new Map();
31
+ let currentPosition = 0;
32
+ // parse bootstrap code
33
+ const bootstrap = getStringBetween(content, 0, BOOTSTRAP_SPLIT_LINE, BOOTSTRAP_SPLIT_LINE);
34
+ if (bootstrap.result) {
35
+ runtimeModules.set("webpack/bootstrap", bootstrap.result);
36
+ }
37
+ // parse module & runtime module code
38
+ let moduleName = getStringBetween(content, currentPosition, MODULE_START_FLAG, MODULE_FLAG_END).result;
39
+ let totalLength = 0;
40
+ while (moduleName) {
41
+ const moduleContent = getStringBetween(content, currentPosition, `${MODULE_START_FLAG}${moduleName}${MODULE_FLAG_END}`, `${MODULE_END_FLAG}${moduleName}${MODULE_FLAG_END}`);
42
+ if (!moduleContent.result) {
43
+ throw new Error(`Module code parsed error: ${moduleName}`);
44
+ }
45
+ if (moduleName.startsWith("webpack/runtime")) {
46
+ totalLength += moduleContent.result.length;
47
+ runtimeModules.set(moduleName, moduleContent.result);
48
+ }
49
+ else {
50
+ totalLength += moduleContent.result.length;
51
+ modules.set(moduleName, moduleContent.result);
52
+ }
53
+ currentPosition = moduleContent.remain;
54
+ moduleName = getStringBetween(content, currentPosition, MODULE_START_FLAG, MODULE_FLAG_END).result;
55
+ }
56
+ return {
57
+ modules,
58
+ runtimeModules
59
+ };
60
+ }
61
+ exports.parseModules = parseModules;
@@ -0,0 +1,2 @@
1
+ import { ECompilerType, TCompilerOptions } from "../type";
2
+ export declare function readConfigFile<T extends ECompilerType>(root: string, files: string[], options: TCompilerOptions<T>): TCompilerOptions<T>;
@@ -0,0 +1,19 @@
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.readConfigFile = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const fs_extra_1 = __importDefault(require("fs-extra"));
9
+ const deepmerge_1 = __importDefault(require("deepmerge"));
10
+ function readConfigFile(root, files, options) {
11
+ const existsFile = files
12
+ .map(i => path_1.default.resolve(root, i))
13
+ .find(i => fs_extra_1.default.existsSync(i));
14
+ const fileConfig = existsFile ? require(existsFile) : {};
15
+ return (0, deepmerge_1.default)(options, fileConfig, {
16
+ arrayMerge: (a, b) => [...a, ...b]
17
+ });
18
+ }
19
+ exports.readConfigFile = readConfigFile;
@@ -0,0 +1,2 @@
1
+ import { ECompilerType, ITestContext, TCompilerStats } from "../type";
2
+ export declare function runBuild<T extends ECompilerType>(context: ITestContext, name?: string): Promise<TCompilerStats<T> | null>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runBuild = void 0;
4
+ async function runBuild(context, name) {
5
+ let stats = null;
6
+ await context.build(compiler => new Promise((resolve, reject) => {
7
+ compiler.run((error, newStats) => {
8
+ if (error)
9
+ return reject(error);
10
+ context.stats(() => newStats);
11
+ stats = newStats;
12
+ resolve();
13
+ });
14
+ }), name);
15
+ return stats;
16
+ }
17
+ exports.runBuild = runBuild;
@@ -0,0 +1,3 @@
1
+ export * from "./type";
2
+ export * from "./test/tester";
3
+ export * from "./processor/diff";
package/dist/index.js ADDED
@@ -0,0 +1,19 @@
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("./type"), exports);
18
+ __exportStar(require("./test/tester"), exports);
19
+ __exportStar(require("./processor/diff"), exports);
@@ -0,0 +1,24 @@
1
+ import { ITestContext, ITestProcessor, TCompareModules, TFileCompareResult, TModuleCompareResult } from "../type";
2
+ import { IFormatCodeOptions } from "../compare";
3
+ export interface IDiffProcessorOptions extends IFormatCodeOptions {
4
+ webpackPath: string;
5
+ rspackPath: string;
6
+ files?: string[];
7
+ modules?: TCompareModules;
8
+ runtimeModules?: TCompareModules;
9
+ onCompareFile?: (file: string, result: TFileCompareResult) => void;
10
+ onCompareModules?: (file: string, results: TModuleCompareResult[]) => void;
11
+ onCompareRuntimeModules?: (file: string, results: TModuleCompareResult[]) => void;
12
+ }
13
+ export declare class DiffProcessor implements ITestProcessor {
14
+ private options;
15
+ private hashes;
16
+ constructor(options: IDiffProcessorOptions);
17
+ config(context: ITestContext): Promise<void>;
18
+ compiler(context: ITestContext): Promise<void>;
19
+ build(context: ITestContext): Promise<void>;
20
+ check(context: ITestContext): Promise<void>;
21
+ private setCompilerOptions;
22
+ private setDefaultOptions;
23
+ private createFormatOptions;
24
+ }
@@ -0,0 +1,131 @@
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.DiffProcessor = void 0;
7
+ const type_1 = require("../type");
8
+ const path_1 = __importDefault(require("path"));
9
+ const module_placeholder_plugin_1 = require("../webpack/module-placeholder-plugin");
10
+ const compare_1 = require("../compare");
11
+ const helper_1 = require("../helper");
12
+ const deepmerge_1 = __importDefault(require("deepmerge"));
13
+ class DiffProcessor {
14
+ constructor(options) {
15
+ this.options = options;
16
+ this.hashes = [];
17
+ }
18
+ async config(context) {
19
+ this.setCompilerOptions(type_1.ECompilerType.Rspack, ["rspack.config.js", "webpack.config.js"], context);
20
+ this.setCompilerOptions(type_1.ECompilerType.Webpack, ["webpack.config.js", "rspack.config.js"], context);
21
+ }
22
+ async compiler(context) {
23
+ const rspack = require(this.options.rspackPath).rspack;
24
+ context.compiler(options => rspack({ ...options }), type_1.ECompilerType.Rspack);
25
+ const webpack = require(this.options.webpackPath).webpack;
26
+ context.compiler(options => webpack({ ...options }), type_1.ECompilerType.Webpack);
27
+ }
28
+ async build(context) {
29
+ const rspackStats = await (0, helper_1.runBuild)(context, type_1.ECompilerType.Rspack);
30
+ const webpackStats = await (0, helper_1.runBuild)(context, type_1.ECompilerType.Webpack);
31
+ //TODO: handle chunk hash and content hash
32
+ (rspackStats === null || rspackStats === void 0 ? void 0 : rspackStats.hash) && this.hashes.push(rspackStats === null || rspackStats === void 0 ? void 0 : rspackStats.hash);
33
+ (webpackStats === null || webpackStats === void 0 ? void 0 : webpackStats.hash) && this.hashes.push(webpackStats === null || webpackStats === void 0 ? void 0 : webpackStats.hash);
34
+ }
35
+ async check(context) {
36
+ const dist = context.getDist();
37
+ for (let file of this.options.files) {
38
+ const rspackDist = path_1.default.join(dist, type_1.ECompilerType.Rspack, file);
39
+ const webpackDist = path_1.default.join(dist, type_1.ECompilerType.Webpack, file);
40
+ const result = (0, compare_1.compareFile)(rspackDist, webpackDist, {
41
+ modules: this.options.modules,
42
+ runtimeModules: this.options.runtimeModules,
43
+ format: this.createFormatOptions(),
44
+ renameModule: compare_1.replaceRuntimeModuleName
45
+ });
46
+ if (typeof this.options.onCompareFile === "function") {
47
+ this.options.onCompareFile(file, result);
48
+ }
49
+ if (typeof this.options.onCompareModules === "function" &&
50
+ result.modules["modules"]) {
51
+ this.options.onCompareModules(file, result.modules["modules"]);
52
+ }
53
+ if (typeof this.options.onCompareRuntimeModules === "function" &&
54
+ result.modules["runtimeModules"]) {
55
+ this.options.onCompareRuntimeModules(file, result.modules["runtimeModules"]);
56
+ }
57
+ }
58
+ }
59
+ setCompilerOptions(type, configFiles, context) {
60
+ const source = context.getSource();
61
+ const dist = context.getDist();
62
+ context.options(options => this.setDefaultOptions(options, type, source, path_1.default.join(dist, type)), type);
63
+ context.options(options => (0, helper_1.readConfigFile)(source, configFiles, options), type);
64
+ }
65
+ setDefaultOptions(options, type, src, dist) {
66
+ let result = (0, deepmerge_1.default)(options, {
67
+ entry: path_1.default.join(src, "./src/index.js"),
68
+ context: src,
69
+ output: {
70
+ filename: "bundle.js",
71
+ chunkFilename: "[name].chunk.js"
72
+ },
73
+ mode: "development",
74
+ devtool: false,
75
+ optimization: {
76
+ chunkIds: "named",
77
+ moduleIds: "named"
78
+ }
79
+ });
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
+ plugins: [(0, module_placeholder_plugin_1.createModulePlaceholderPlugin)(this.options.webpackPath)]
100
+ }, {
101
+ arrayMerge: (a, b) => [...a, ...b]
102
+ });
103
+ }
104
+ if (type === type_1.ECompilerType.Rspack) {
105
+ result = (0, deepmerge_1.default)(result, {
106
+ output: {
107
+ path: dist
108
+ },
109
+ experiments: {
110
+ rspackFuture: {
111
+ disableTransformByDefault: true
112
+ }
113
+ }
114
+ });
115
+ }
116
+ return result;
117
+ }
118
+ createFormatOptions() {
119
+ const formatOptions = {
120
+ ignoreModuleArugments: this.options.ignoreModuleArugments,
121
+ ignoreModuleId: this.options.ignoreModuleId,
122
+ ignorePropertyQuotationMark: this.options.ignorePropertyQuotationMark,
123
+ replacements: this.options.replacements || {}
124
+ };
125
+ for (let hash of this.hashes) {
126
+ formatOptions.replacements[hash] = "fullhash";
127
+ }
128
+ return formatOptions;
129
+ }
130
+ }
131
+ exports.DiffProcessor = DiffProcessor;
@@ -0,0 +1,17 @@
1
+ import { ECompilerType, ITestContext, ITesterConfig, TCompiler, TCompilerOptions, TCompilerStats } from "../type";
2
+ export declare class TestContext implements ITestContext {
3
+ private config;
4
+ errors: Error[];
5
+ private compilers;
6
+ constructor(config: ITesterConfig);
7
+ getSource(sub?: string): string;
8
+ getDist(sub?: string): string;
9
+ build<T extends ECompilerType>(fn: (compiler: TCompiler<T>) => Promise<void>, name?: string): Promise<void>;
10
+ options<T extends ECompilerType>(fn: (options: TCompilerOptions<T>) => TCompilerOptions<T>, name?: string): void;
11
+ compiler<T extends ECompilerType>(fn: (options: TCompilerOptions<T>, compiler: TCompiler<T> | null) => TCompiler<T> | null, name?: string): void;
12
+ stats<T extends ECompilerType>(fn: (compiler: TCompiler<T> | null, stats: TCompilerStats<T> | null) => TCompilerStats<T> | null, name?: string): void;
13
+ result<T extends ECompilerType>(fn: <R>(compiler: TCompiler<T> | null, result: R) => R, name?: string): void;
14
+ emitError(err: Error | string): void;
15
+ hasError(): boolean;
16
+ private getCompilerManage;
17
+ }
@@ -0,0 +1,61 @@
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.TestContext = void 0;
7
+ const compiler_1 = require("../compiler");
8
+ const path_1 = __importDefault(require("path"));
9
+ const DEFAULT_COMPILER_NAME = "__default__";
10
+ class TestContext {
11
+ constructor(config) {
12
+ this.config = config;
13
+ this.errors = [];
14
+ this.compilers = new Map();
15
+ }
16
+ getSource(sub) {
17
+ if (sub) {
18
+ return path_1.default.resolve(this.config.src, sub);
19
+ }
20
+ return this.config.src;
21
+ }
22
+ getDist(sub) {
23
+ if (sub) {
24
+ return path_1.default.resolve(this.config.dist, sub);
25
+ }
26
+ return this.config.dist;
27
+ }
28
+ async build(fn, name = DEFAULT_COMPILER_NAME) {
29
+ const compiler = this.getCompilerManage(name);
30
+ await compiler.build(this, fn);
31
+ }
32
+ options(fn, name = DEFAULT_COMPILER_NAME) {
33
+ const compiler = this.getCompilerManage(name);
34
+ compiler.options(this, fn);
35
+ }
36
+ compiler(fn, name = DEFAULT_COMPILER_NAME) {
37
+ const compiler = this.getCompilerManage(name);
38
+ compiler.compiler(this, fn);
39
+ }
40
+ stats(fn, name = DEFAULT_COMPILER_NAME) {
41
+ const compiler = this.getCompilerManage(name);
42
+ compiler.stats(this, fn);
43
+ }
44
+ result(fn, name = DEFAULT_COMPILER_NAME) {
45
+ const compiler = this.getCompilerManage(name);
46
+ compiler.result(this, fn);
47
+ }
48
+ emitError(err) {
49
+ this.errors.push(typeof err === "string" ? new Error(err) : err);
50
+ }
51
+ hasError() {
52
+ return !!this.errors.length;
53
+ }
54
+ getCompilerManage(name) {
55
+ if (!this.compilers.has(name)) {
56
+ this.compilers.set(name, new compiler_1.TestCompilerManager());
57
+ }
58
+ return this.compilers.get(name);
59
+ }
60
+ }
61
+ exports.TestContext = TestContext;
@@ -0,0 +1,14 @@
1
+ import { ITester, ITesterConfig } from "../type";
2
+ export declare class Tester implements ITester {
3
+ private context;
4
+ private steps;
5
+ step: number;
6
+ constructor(config: ITesterConfig);
7
+ prepare(): Promise<void>;
8
+ compile(): Promise<void>;
9
+ check(): Promise<void>;
10
+ next(): boolean;
11
+ resume(): Promise<void>;
12
+ private runStepMethods;
13
+ private outputErrors;
14
+ }
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Tester = void 0;
4
+ const context_1 = require("./context");
5
+ class Tester {
6
+ constructor(config) {
7
+ this.steps = [];
8
+ this.step = 0;
9
+ this.context = new context_1.TestContext(config);
10
+ this.steps = config.steps || [];
11
+ this.step = 0;
12
+ }
13
+ async prepare() {
14
+ for (let i of this.steps) {
15
+ if (typeof i.beforeAll === "function") {
16
+ await i.beforeAll(this.context);
17
+ }
18
+ }
19
+ }
20
+ async compile() {
21
+ const currentStep = this.steps[this.step];
22
+ if (!currentStep)
23
+ return;
24
+ await this.runStepMethods(currentStep, [
25
+ "before",
26
+ "config",
27
+ "compiler",
28
+ "build"
29
+ ]);
30
+ if (this.context.hasError()) {
31
+ this.outputErrors();
32
+ throw new Error("build failed");
33
+ }
34
+ }
35
+ async check() {
36
+ const currentStep = this.steps[this.step];
37
+ if (!currentStep)
38
+ return;
39
+ await this.runStepMethods(currentStep, ["run", "check", "after"]);
40
+ if (this.context.hasError()) {
41
+ this.outputErrors();
42
+ throw new Error("check failed");
43
+ }
44
+ }
45
+ next() {
46
+ if (this.context.hasError()) {
47
+ this.outputErrors();
48
+ return false;
49
+ }
50
+ if (this.steps[this.step + 1]) {
51
+ this.step++;
52
+ return true;
53
+ }
54
+ else {
55
+ return false;
56
+ }
57
+ }
58
+ async resume() {
59
+ for (let i of this.steps) {
60
+ if (typeof i.afterAll === "function") {
61
+ await i.afterAll(this.context);
62
+ }
63
+ }
64
+ }
65
+ async runStepMethods(step, methods) {
66
+ for (let i of methods) {
67
+ if (this.context.hasError())
68
+ return;
69
+ if (typeof step[i] === "function") {
70
+ await step[i](this.context);
71
+ }
72
+ }
73
+ }
74
+ outputErrors() {
75
+ console.error(`Errors occur in step ${this.step + 1}:`);
76
+ for (let error of this.context.errors) {
77
+ console.error(error);
78
+ }
79
+ }
80
+ }
81
+ exports.Tester = Tester;
package/dist/type.d.ts ADDED
@@ -0,0 +1,83 @@
1
+ import { RspackOptions, Compiler as RspackCompiler, Stats as RspackStats } from "@rspack/core";
2
+ import type { Configuration as WebpackOptions, Compiler as WebpackCompiler, Stats as WebpackStats } from "webpack";
3
+ export interface ITestContext {
4
+ errors: Error[];
5
+ getSource(sub?: string): string;
6
+ getDist(sub?: string): string;
7
+ options<T extends ECompilerType>(fn: (options: TCompilerOptions<T>) => TCompilerOptions<T>, name?: string): void;
8
+ compiler<T extends ECompilerType>(fn: (options: TCompilerOptions<T>, compiler: TCompiler<T> | null) => TCompiler<T> | null, name?: string): void;
9
+ stats<T extends ECompilerType>(fn: (compiler: TCompiler<T> | null, stats: TCompilerStats<T> | null) => TCompilerStats<T> | null, name?: string): void;
10
+ result<T extends ECompilerType>(fn: <R>(compiler: TCompiler<T> | null, result: R) => R, name?: string): void;
11
+ build<T extends ECompilerType>(fn: (compiler: TCompiler<T>) => Promise<void>, name?: string): Promise<void>;
12
+ emitError(err: Error | string): void;
13
+ hasError(): boolean;
14
+ }
15
+ export declare enum ECompilerType {
16
+ Rspack = "rspack",
17
+ Webpack = "webpack"
18
+ }
19
+ export type TCompilerOptions<T> = T extends ECompilerType.Rspack ? RspackOptions : WebpackOptions;
20
+ export type TCompiler<T> = T extends ECompilerType.Rspack ? RspackCompiler : WebpackCompiler;
21
+ export type TCompilerStats<T> = T extends ECompilerType.Rspack ? RspackStats : WebpackStats;
22
+ export interface ITestCompilerManager<T extends ECompilerType> {
23
+ options(context: ITestContext, fn: (options: TCompilerOptions<T>) => TCompilerOptions<T>): void;
24
+ compiler(context: ITestContext, fn: (options: TCompilerOptions<T>, compiler: TCompiler<T> | null) => TCompiler<T> | null): void;
25
+ stats(context: ITestContext, fn: (compiler: TCompiler<T> | null, stats: TCompilerStats<T> | null) => TCompilerStats<T> | null): void;
26
+ result(context: ITestContext, fn: <R>(compiler: TCompiler<T> | null, result: R) => R): void;
27
+ build(context: ITestContext, fn: (compiler: TCompiler<T>) => Promise<void>): Promise<void>;
28
+ }
29
+ export interface ITestLoader {
30
+ walk(): Promise<void>;
31
+ }
32
+ export interface ITesterConfig {
33
+ name: string;
34
+ src: string;
35
+ dist: string;
36
+ steps?: ITestProcessor[];
37
+ }
38
+ export interface ITester {
39
+ step: number;
40
+ prepare(): Promise<void>;
41
+ compile(): Promise<void>;
42
+ check(): Promise<void>;
43
+ next(): boolean;
44
+ resume(): Promise<void>;
45
+ }
46
+ export interface ITestProcessor {
47
+ beforeAll?(context: ITestContext): Promise<void>;
48
+ afterAll?(context: ITestContext): Promise<void>;
49
+ before?(context: ITestContext): Promise<void>;
50
+ after?(context: ITestContext): Promise<void>;
51
+ config?(context: ITestContext): Promise<void>;
52
+ compiler?(context: ITestContext): Promise<void>;
53
+ build?(context: ITestContext): Promise<void>;
54
+ run?(context: ITestContext): Promise<void>;
55
+ check?(context: ITestContext): Promise<unknown>;
56
+ }
57
+ export declare enum ECompareResultType {
58
+ Same = "same",
59
+ Missing = "missing",
60
+ OnlyDist = "only-dist",
61
+ OnlySource = "only-source",
62
+ Different = "different"
63
+ }
64
+ export type TCompareModules = string[] | true;
65
+ export type TCompareResult = {
66
+ type: ECompareResultType;
67
+ detail?: unknown;
68
+ lines?: {
69
+ common: number;
70
+ source: number;
71
+ dist: number;
72
+ };
73
+ };
74
+ export type TModuleCompareResult = TCompareResult & {
75
+ name: string;
76
+ };
77
+ export type TFileCompareResult = TCompareResult & {
78
+ file: {
79
+ source: string;
80
+ dist: string;
81
+ };
82
+ modules: Partial<Record<"modules" | "runtimeModules", TModuleCompareResult[]>>;
83
+ };
package/dist/type.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ECompareResultType = exports.ECompilerType = void 0;
4
+ var ECompilerType;
5
+ (function (ECompilerType) {
6
+ ECompilerType["Rspack"] = "rspack";
7
+ ECompilerType["Webpack"] = "webpack";
8
+ })(ECompilerType = exports.ECompilerType || (exports.ECompilerType = {}));
9
+ var ECompareResultType;
10
+ (function (ECompareResultType) {
11
+ ECompareResultType["Same"] = "same";
12
+ ECompareResultType["Missing"] = "missing";
13
+ ECompareResultType["OnlyDist"] = "only-dist";
14
+ ECompareResultType["OnlySource"] = "only-source";
15
+ ECompareResultType["Different"] = "different";
16
+ })(ECompareResultType = exports.ECompareResultType || (exports.ECompareResultType = {}));
@@ -0,0 +1,3 @@
1
+ export declare function createModulePlaceholderPlugin(webpackPath: any): {
2
+ apply(compiler: any): void;
3
+ };
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createModulePlaceholderPlugin = 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
+ source.add(Template.toNormalComment(`start::${module.identifier()}`) + "\n");
31
+ if (!module.shouldIsolate()) {
32
+ source.add(runtimeSource);
33
+ source.add("\n\n");
34
+ }
35
+ else if (renderContext.runtimeTemplate.supportsArrowFunction()) {
36
+ source.add("(() => {\n");
37
+ source.add(new PrefixSource("\t", runtimeSource));
38
+ source.add("\n})();\n\n");
39
+ }
40
+ else {
41
+ source.add("!function() {\n");
42
+ source.add(new PrefixSource("\t", runtimeSource));
43
+ source.add("\n}();\n\n");
44
+ }
45
+ source.add(Template.toNormalComment(`end::${module.identifier()}`) + "\n");
46
+ }
47
+ }
48
+ return source;
49
+ };
50
+ }
51
+ const caches = new WeakMap();
52
+ function createModulePlaceholderPlugin(webpackPath) {
53
+ const JavascriptModulesPlugin = require(path.join(path.dirname(webpackPath), "javascript/JavascriptModulesPlugin.js"));
54
+ const Template = require(path.join(path.dirname(webpackPath), "Template.js"));
55
+ Template.renderRuntimeModules = createRenderRuntimeModulesFn(Template);
56
+ return {
57
+ apply(compiler) {
58
+ compiler.hooks.compilation.tap("RuntimeDiffPlugin", compilation => {
59
+ const hooks = JavascriptModulesPlugin.getCompilationHooks(compilation);
60
+ hooks.inlineInRuntimeBailout.tap("RuntimeDiffPlugin", () => "not allow inline startup");
61
+ hooks.renderModulePackage.tap("RuntimeDiffPlugin", (moduleSource, module, { chunk, chunkGraph, moduleGraph, runtimeTemplate }) => {
62
+ const { requestShortener } = runtimeTemplate;
63
+ let cacheEntry;
64
+ let cache = caches.get(requestShortener);
65
+ if (cache === undefined) {
66
+ caches.set(requestShortener, (cache = new WeakMap()));
67
+ cache.set(module, (cacheEntry = {
68
+ header: undefined,
69
+ footer: undefined,
70
+ full: new WeakMap()
71
+ }));
72
+ }
73
+ else {
74
+ cacheEntry = cache.get(module);
75
+ if (cacheEntry === undefined) {
76
+ cache.set(module, (cacheEntry = {
77
+ header: undefined,
78
+ footer: undefined,
79
+ full: new WeakMap()
80
+ }));
81
+ }
82
+ else {
83
+ const cachedSource = cacheEntry.full.get(moduleSource);
84
+ if (cachedSource !== undefined)
85
+ return cachedSource;
86
+ }
87
+ }
88
+ const source = new ConcatSource();
89
+ let header = cacheEntry.header;
90
+ let footer = cacheEntry.footer;
91
+ if (header === undefined) {
92
+ const req = module.readableIdentifier(requestShortener);
93
+ const reqStr = req.replace(/\*\//g, "*_/");
94
+ header = new RawSource(`\n/* start::${reqStr} */\n`);
95
+ footer = new RawSource(`\n/* end::${reqStr} */\n`);
96
+ cacheEntry.header = header;
97
+ cacheEntry.footer = footer;
98
+ }
99
+ source.add(header);
100
+ source.add(moduleSource);
101
+ source.add(footer);
102
+ const cachedSource = new CachedSource(source);
103
+ cacheEntry.full.set(moduleSource, cachedSource);
104
+ return cachedSource;
105
+ });
106
+ });
107
+ }
108
+ };
109
+ }
110
+ exports.createModulePlaceholderPlugin = createModulePlaceholderPlugin;
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "name": "@rspack/test-tools",
3
+ "version": "0.3.14-canary-020060b-20231122074120",
4
+ "license": "MIT",
5
+ "description": "Test tools for rspack",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "default": "./dist/index.js"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "files": [
15
+ "client",
16
+ "dist"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "homepage": "https://rspack.dev",
22
+ "bugs": "https://github.com/web-infra-dev/rspack/issues",
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/web-infra-dev/rspack",
26
+ "directory": "packages/rspack-test-tools"
27
+ },
28
+ "dependencies": {
29
+ "@babel/generator": "7.23.0",
30
+ "@babel/helpers": "7.23.2",
31
+ "@babel/parser": "7.23.0",
32
+ "@babel/template": "7.22.15",
33
+ "@babel/traverse": "7.23.2",
34
+ "@babel/types": "7.23.0",
35
+ "deepmerge": "^4.3.1",
36
+ "fs-extra": "^11.1.1",
37
+ "jest-diff": "^29.7.0",
38
+ "webpack-sources": "3.2.3",
39
+ "@rspack/core": "0.3.14-canary-020060b-20231122074120"
40
+ },
41
+ "devDependencies": {
42
+ "@types/webpack": "5.28.3",
43
+ "@types/webpack-sources": "3.2.0",
44
+ "webpack": "5.89.0"
45
+ },
46
+ "peerDependenciesMeta": {},
47
+ "jest": {
48
+ "watchPathIgnorePatterns": [
49
+ "<rootDir>/dist",
50
+ "<rootDir>/tests/dist"
51
+ ],
52
+ "testEnvironment": "../../scripts/test/patch-node-env.cjs"
53
+ },
54
+ "scripts": {
55
+ "build": "rimraf dist/ && tsc -b ./tsconfig.build.json --force",
56
+ "dev": "tsc -b -w"
57
+ }
58
+ }