@rspack/test-tools 1.5.6 → 1.5.8

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.
Files changed (125) hide show
  1. package/dist/case/builtin.d.ts +2 -0
  2. package/dist/case/builtin.js +173 -13
  3. package/dist/case/cache.d.ts +1 -1
  4. package/dist/case/cache.js +210 -11
  5. package/dist/case/common.d.ts +10 -0
  6. package/dist/case/common.js +239 -0
  7. package/dist/case/compiler.d.ts +7 -4
  8. package/dist/case/compiler.js +144 -109
  9. package/dist/case/config.d.ts +5 -1
  10. package/dist/case/config.js +102 -16
  11. package/dist/case/defaults.d.ts +12 -4
  12. package/dist/case/defaults.js +67 -12
  13. package/dist/case/diagnostic.d.ts +6 -0
  14. package/dist/case/diagnostic.js +123 -14
  15. package/dist/case/diff.d.ts +17 -1
  16. package/dist/case/diff.js +170 -22
  17. package/dist/case/error.d.ts +14 -4
  18. package/dist/case/error.js +117 -10
  19. package/dist/case/esm-output.d.ts +1 -0
  20. package/dist/case/esm-output.js +96 -0
  21. package/dist/case/hash.d.ts +1 -1
  22. package/dist/case/hash.js +70 -9
  23. package/dist/case/hook.d.ts +35 -4
  24. package/dist/case/hook.js +210 -21
  25. package/dist/case/hot-step.js +307 -9
  26. package/dist/case/hot.d.ts +8 -1
  27. package/dist/case/hot.js +235 -8
  28. package/dist/case/incremental.d.ts +1 -1
  29. package/dist/case/incremental.js +39 -41
  30. package/dist/case/index.d.ts +21 -20
  31. package/dist/case/index.js +46 -34
  32. package/dist/case/native-watcher.js +10 -23
  33. package/dist/case/normal.js +174 -12
  34. package/dist/case/runner.d.ts +18 -0
  35. package/dist/case/runner.js +108 -0
  36. package/dist/case/serial.d.ts +1 -1
  37. package/dist/case/serial.js +8 -12
  38. package/dist/case/stats-api.d.ts +0 -5
  39. package/dist/case/stats-api.js +71 -10
  40. package/dist/case/stats-output.d.ts +10 -0
  41. package/dist/case/stats-output.js +198 -9
  42. package/dist/case/treeshaking.js +34 -7
  43. package/dist/case/watch.d.ts +27 -0
  44. package/dist/case/watch.js +321 -21
  45. package/dist/helper/plugins/hot-update.d.ts +2 -2
  46. package/dist/index.d.ts +0 -2
  47. package/dist/index.js +0 -2
  48. package/dist/runner/index.d.ts +2 -7
  49. package/dist/runner/index.js +2 -7
  50. package/dist/runner/{runner/node → node}/index.d.ts +1 -2
  51. package/dist/runner/{runner/node → node}/index.js +13 -9
  52. package/dist/runner/{runner/web → web}/fake.d.ts +1 -2
  53. package/dist/runner/{runner/web → web}/fake.js +7 -7
  54. package/dist/runner/{runner/web → web}/index.d.ts +2 -2
  55. package/dist/runner/{runner/web → web}/index.js +1 -1
  56. package/dist/runner/{runner/web → web}/jsdom.d.ts +1 -2
  57. package/dist/runner/{runner/web → web}/jsdom.js +4 -4
  58. package/dist/test/context.d.ts +3 -5
  59. package/dist/test/context.js +22 -12
  60. package/dist/test/creator.d.ts +13 -12
  61. package/dist/test/creator.js +52 -43
  62. package/dist/test/tester.js +4 -1
  63. package/dist/type.d.ts +42 -11
  64. package/dist/type.js +7 -1
  65. package/package.json +6 -6
  66. package/dist/processor/basic.d.ts +0 -24
  67. package/dist/processor/basic.js +0 -147
  68. package/dist/processor/builtin.d.ts +0 -9
  69. package/dist/processor/builtin.js +0 -171
  70. package/dist/processor/cache.d.ts +0 -20
  71. package/dist/processor/cache.js +0 -131
  72. package/dist/processor/config.d.ts +0 -11
  73. package/dist/processor/config.js +0 -88
  74. package/dist/processor/defaults.d.ts +0 -30
  75. package/dist/processor/defaults.js +0 -72
  76. package/dist/processor/diagnostic.d.ts +0 -15
  77. package/dist/processor/diagnostic.js +0 -104
  78. package/dist/processor/diff.d.ts +0 -30
  79. package/dist/processor/diff.js +0 -140
  80. package/dist/processor/error.d.ts +0 -23
  81. package/dist/processor/error.js +0 -95
  82. package/dist/processor/hash.d.ts +0 -10
  83. package/dist/processor/hash.js +0 -65
  84. package/dist/processor/hook.d.ts +0 -44
  85. package/dist/processor/hook.js +0 -206
  86. package/dist/processor/hot-incremental.d.ts +0 -14
  87. package/dist/processor/hot-incremental.js +0 -43
  88. package/dist/processor/hot-step.d.ts +0 -18
  89. package/dist/processor/hot-step.js +0 -307
  90. package/dist/processor/hot.d.ts +0 -17
  91. package/dist/processor/hot.js +0 -147
  92. package/dist/processor/index.d.ts +0 -20
  93. package/dist/processor/index.js +0 -36
  94. package/dist/processor/multi.d.ts +0 -17
  95. package/dist/processor/multi.js +0 -73
  96. package/dist/processor/normal.d.ts +0 -12
  97. package/dist/processor/normal.js +0 -170
  98. package/dist/processor/simple.d.ts +0 -24
  99. package/dist/processor/simple.js +0 -51
  100. package/dist/processor/snapshot.d.ts +0 -12
  101. package/dist/processor/snapshot.js +0 -66
  102. package/dist/processor/stats-api.d.ts +0 -18
  103. package/dist/processor/stats-api.js +0 -48
  104. package/dist/processor/stats.d.ts +0 -18
  105. package/dist/processor/stats.js +0 -206
  106. package/dist/processor/treeshaking.d.ts +0 -10
  107. package/dist/processor/treeshaking.js +0 -33
  108. package/dist/processor/watch.d.ts +0 -30
  109. package/dist/processor/watch.js +0 -252
  110. package/dist/runner/basic.d.ts +0 -10
  111. package/dist/runner/basic.js +0 -64
  112. package/dist/runner/cache.d.ts +0 -5
  113. package/dist/runner/cache.js +0 -92
  114. package/dist/runner/hot.d.ts +0 -5
  115. package/dist/runner/hot.js +0 -91
  116. package/dist/runner/multiple.d.ts +0 -11
  117. package/dist/runner/multiple.js +0 -52
  118. package/dist/runner/runner/index.d.ts +0 -2
  119. package/dist/runner/runner/index.js +0 -18
  120. package/dist/runner/type.d.ts +0 -42
  121. package/dist/runner/type.js +0 -9
  122. package/dist/runner/watch.d.ts +0 -7
  123. package/dist/runner/watch.js +0 -71
  124. package/dist/test/simple.d.ts +0 -5
  125. package/dist/test/simple.js +0 -43
@@ -0,0 +1,239 @@
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.getCompiler = getCompiler;
7
+ exports.config = config;
8
+ exports.compiler = compiler;
9
+ exports.build = build;
10
+ exports.run = run;
11
+ exports.check = check;
12
+ exports.checkSnapshot = checkSnapshot;
13
+ exports.findMultiCompilerBundle = findMultiCompilerBundle;
14
+ exports.configMultiCompiler = configMultiCompiler;
15
+ const fs_extra_1 = __importDefault(require("fs-extra"));
16
+ const path_1 = __importDefault(require("path"));
17
+ const webpack_merge_1 = __importDefault(require("webpack-merge"));
18
+ const helper_1 = require("../helper");
19
+ const placeholder_1 = require("../helper/expect/placeholder");
20
+ const checkArrayExpectation_1 = __importDefault(require("../helper/legacy/checkArrayExpectation"));
21
+ const type_1 = require("../type");
22
+ function getCompiler(context, name) {
23
+ return context.getCompiler(name, type_1.ECompilerType.Rspack);
24
+ }
25
+ async function config(context, name, configFiles, defaultOptions = {}) {
26
+ const compiler = getCompiler(context, name);
27
+ compiler.setOptions(defaultOptions);
28
+ if (Array.isArray(configFiles)) {
29
+ const fileOptions = (0, helper_1.readConfigFile)(configFiles.map(i => context.getSource(i)))[0];
30
+ compiler.mergeOptions(fileOptions);
31
+ }
32
+ return compiler.getOptions();
33
+ }
34
+ async function compiler(context, name) {
35
+ const compiler = getCompiler(context, name);
36
+ compiler.createCompiler();
37
+ return compiler.getCompiler();
38
+ }
39
+ async function build(context, name) {
40
+ const compiler = getCompiler(context, name);
41
+ await compiler.build();
42
+ return compiler.getCompiler();
43
+ }
44
+ async function run(env, context, name, findBundle) {
45
+ const testConfig = context.getTestConfig();
46
+ if (testConfig.noTests)
47
+ return;
48
+ if (testConfig.documentType) {
49
+ context.setValue(name, "documentType", testConfig.documentType);
50
+ }
51
+ const compiler = getCompiler(context, name);
52
+ if (typeof testConfig.beforeExecute === "function") {
53
+ testConfig.beforeExecute(compiler.getOptions());
54
+ }
55
+ let bundles;
56
+ if (testConfig.bundlePath) {
57
+ bundles = testConfig.bundlePath;
58
+ }
59
+ else if (typeof findBundle === "function") {
60
+ bundles = findBundle(context, compiler.getOptions());
61
+ }
62
+ else {
63
+ bundles = [];
64
+ }
65
+ if (typeof bundles === "string") {
66
+ bundles = [bundles];
67
+ }
68
+ if (!bundles || !bundles.length) {
69
+ return;
70
+ }
71
+ for (const bundle of bundles) {
72
+ if (!bundle) {
73
+ continue;
74
+ }
75
+ const runner = context.getRunner(name, bundle, env);
76
+ const mod = runner.run(bundle);
77
+ const result = context.getValue(name, "modules") || [];
78
+ result.push(mod);
79
+ context.setValue(name, "modules", result);
80
+ }
81
+ const results = context.getValue(name, "modules") || [];
82
+ await Promise.all(results);
83
+ if (typeof testConfig.afterExecute === "function") {
84
+ testConfig.afterExecute(compiler.getOptions());
85
+ }
86
+ }
87
+ async function check(env, context, name) {
88
+ const testConfig = context.getTestConfig();
89
+ if (testConfig.noTests)
90
+ return;
91
+ const compiler = getCompiler(context, name);
92
+ const errors = (context.getError(name) || []).map(e => ({
93
+ message: e.message,
94
+ stack: e.stack
95
+ }));
96
+ const warnings = [];
97
+ const stats = compiler.getStats();
98
+ const options = compiler.getOptions();
99
+ if (stats) {
100
+ if (testConfig.writeStatsOuptut) {
101
+ fs_extra_1.default.writeFileSync(path_1.default.join(context.getDist(), "stats.txt"), stats.toString({
102
+ preset: "verbose",
103
+ colors: false
104
+ }), "utf-8");
105
+ }
106
+ if (testConfig.writeStatsJson) {
107
+ const jsonStats = stats.toJson({
108
+ errorDetails: true
109
+ });
110
+ fs_extra_1.default.writeFileSync(path_1.default.join(context.getDist(), "stats.json"), JSON.stringify(jsonStats, null, 2), "utf-8");
111
+ }
112
+ if (fs_extra_1.default.existsSync(context.getSource("errors.js")) ||
113
+ fs_extra_1.default.existsSync(context.getSource("warnings.js")) ||
114
+ stats.hasErrors() ||
115
+ stats.hasWarnings()) {
116
+ const statsJson = stats.toJson({
117
+ errorDetails: true
118
+ });
119
+ if (statsJson.errors) {
120
+ errors.push(...statsJson.errors);
121
+ }
122
+ if (statsJson.warnings) {
123
+ warnings.push(...statsJson.warnings);
124
+ }
125
+ }
126
+ }
127
+ await (0, checkArrayExpectation_1.default)(context.getSource(), { errors }, "error", "errors", "Error", options);
128
+ await (0, checkArrayExpectation_1.default)(context.getSource(), { warnings }, "warning", "warnings", "Warning", options);
129
+ // clear error if checked
130
+ if (fs_extra_1.default.existsSync(context.getSource("errors.js"))) {
131
+ context.clearError(name);
132
+ }
133
+ }
134
+ async function checkSnapshot(env, context, name, snapshot, filter) {
135
+ if (path_1.default.extname(snapshot) === ".snap") {
136
+ throw new Error("Snapshot with `.snap` will be managed by jest, please use `.snap.txt` instead");
137
+ }
138
+ const compilerManager = getCompiler(context, name);
139
+ const stats = compilerManager.getStats();
140
+ const compiler = compilerManager.getCompiler();
141
+ if (!stats || !compiler)
142
+ return;
143
+ const compilers = "compilers" in compiler
144
+ ? compiler.compilers
145
+ : [compiler];
146
+ const totalStats = "stats" in stats
147
+ ? stats.stats
148
+ : [stats];
149
+ const total = compilers.length;
150
+ for (let i = 0; i < compilers.length; i++) {
151
+ const c = compilers[i];
152
+ const stats = totalStats[i];
153
+ if (stats.hasErrors()) {
154
+ const errors = [];
155
+ errors.push(...stats.compilation.errors);
156
+ throw new Error(`Failed to compile in fixture ${name}, Errors: ${errors
157
+ ?.map(i => `${i.message}\n${i.stack}`)
158
+ .join("\n\n")}`);
159
+ }
160
+ const compilation = c._lastCompilation ||
161
+ c._lastCompilation;
162
+ const snapshotFileFilter = filter ||
163
+ ((file) => (file.endsWith(".js") || file.endsWith(".mjs")) &&
164
+ !file.includes("runtime.js"));
165
+ const fileContents = Object.entries(compilation.assets)
166
+ .filter(([file]) => snapshotFileFilter(file))
167
+ .map(([file, source]) => {
168
+ const tag = path_1.default.extname(file).slice(1) || "txt";
169
+ let content = (0, placeholder_1.normalizePlaceholder)(source.source().toString());
170
+ const testConfig = context.getTestConfig();
171
+ if (testConfig.snapshotContent) {
172
+ content = testConfig.snapshotContent(content);
173
+ }
174
+ return `\`\`\`${tag} title=${file}\n${content}\n\`\`\``;
175
+ });
176
+ fileContents.sort();
177
+ const content = fileContents.join("\n\n");
178
+ const snapshotPath = path_1.default.isAbsolute(snapshot)
179
+ ? snapshot
180
+ : path_1.default.resolve(context.getSource(), `./__snapshots__/${snapshot}${total > 1 ? `-${i}` : ""}`);
181
+ env.expect(content).toMatchFileSnapshot(snapshotPath);
182
+ }
183
+ }
184
+ function findMultiCompilerBundle(context, name, multiFindBundle) {
185
+ if (typeof multiFindBundle !== "function") {
186
+ return [];
187
+ }
188
+ const multiCompilerOptions = (context.getValue(name, "multiCompilerOptions") || []);
189
+ const result = [];
190
+ const multiFileIndexMap = context.getValue(name, "multiFileIndexMap") || {};
191
+ for (const [index, compilerOptions] of multiCompilerOptions.entries()) {
192
+ const curBundles = multiFindBundle(index, context, compilerOptions);
193
+ const bundles = Array.isArray(curBundles)
194
+ ? curBundles
195
+ : curBundles
196
+ ? [curBundles]
197
+ : [];
198
+ for (const bundle of bundles) {
199
+ if (multiFileIndexMap[bundle]) {
200
+ multiFileIndexMap[bundle].push(index);
201
+ }
202
+ else {
203
+ multiFileIndexMap[bundle] = [index];
204
+ }
205
+ }
206
+ result.push(...bundles);
207
+ }
208
+ context.setValue(name, "multiFileIndexMap", multiFileIndexMap);
209
+ return result;
210
+ }
211
+ function configMultiCompiler(context, name, configFiles, defaultOptions, overrideOptions) {
212
+ const multiCompilerOptions = [];
213
+ const caseOptions = Array.isArray(configFiles)
214
+ ? (0, helper_1.readConfigFile)(configFiles.map(i => context.getSource(i)), configs => {
215
+ return configs.flatMap(c => {
216
+ if (typeof c === "function") {
217
+ const options = {
218
+ testPath: context.getDist(),
219
+ env: undefined
220
+ };
221
+ return c(options.env, options);
222
+ }
223
+ return c;
224
+ });
225
+ })
226
+ : [{}];
227
+ for (const [index, options] of caseOptions.entries()) {
228
+ const compilerOptions = (0, webpack_merge_1.default)(typeof defaultOptions === "function"
229
+ ? defaultOptions(index, context)
230
+ : {}, options);
231
+ if (typeof overrideOptions === "function") {
232
+ overrideOptions(index, context, compilerOptions);
233
+ }
234
+ multiCompilerOptions.push(compilerOptions);
235
+ }
236
+ const compiler = getCompiler(context, name);
237
+ compiler.setOptions(multiCompilerOptions);
238
+ context.setValue(name, "multiCompilerOptions", multiCompilerOptions);
239
+ }
@@ -1,9 +1,12 @@
1
- import { type ISimpleProcessorOptions } from "../processor";
2
- import { ECompilerType, type ITestContext, type TCompilation, type TCompiler, type TCompilerStatsCompilation } from "../type";
3
- export type TCompilerCaseConfig = Omit<ISimpleProcessorOptions<ECompilerType.Rspack>, "name" | "compilerType" | "check"> & {
1
+ import type { ECompilerType, ITestContext, TCompilation, TCompiler, TCompilerOptions, TCompilerStats, TCompilerStatsCompilation } from "../type";
2
+ export declare function createCompilerCase(name: string, src: string, dist: string, testConfig: string): void;
3
+ export type TCompilerCaseConfig = {
4
4
  description: string;
5
5
  error?: boolean;
6
6
  skip?: boolean;
7
+ options?: (context: ITestContext) => TCompilerOptions<ECompilerType.Rspack>;
8
+ compiler?: (context: ITestContext, compiler: TCompiler<ECompilerType.Rspack>) => Promise<void>;
9
+ build?: (context: ITestContext, compiler: TCompiler<ECompilerType.Rspack>) => Promise<void>;
7
10
  check?: ({ context, stats, files, compiler, compilation }: {
8
11
  context: ITestContext;
9
12
  stats?: TCompilerStatsCompilation<ECompilerType.Rspack>;
@@ -11,5 +14,5 @@ export type TCompilerCaseConfig = Omit<ISimpleProcessorOptions<ECompilerType.Rsp
11
14
  compiler: TCompiler<ECompilerType.Rspack>;
12
15
  compilation?: TCompilation<ECompilerType.Rspack>;
13
16
  }) => Promise<void>;
17
+ compilerCallback?: (error: Error | null, stats: TCompilerStats<ECompilerType.Rspack> | null) => void;
14
18
  };
15
- export declare function createCompilerCase(name: string, src: string, dist: string, testConfig: string): void;
@@ -1,120 +1,155 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createCompilerCase = createCompilerCase;
4
- const processor_1 = require("../processor");
5
- const simple_1 = require("../test/simple");
6
- const type_1 = require("../type");
4
+ const creator_1 = require("../test/creator");
5
+ const common_1 = require("./common");
6
+ function createCompilerProcessor(name, caseConfig) {
7
+ const logs = {
8
+ mkdir: [],
9
+ writeFile: []
10
+ };
11
+ const files = {};
12
+ return {
13
+ config: async (context) => {
14
+ const compiler = (0, common_1.getCompiler)(context, name);
15
+ const options = caseConfig.options?.(context) || {};
16
+ options.mode ??= "production";
17
+ options.context ??= context.getSource();
18
+ options.entry ??= "./a.js";
19
+ options.output ??= {};
20
+ options.output.path ??= "/";
21
+ options.output.pathinfo ??= true;
22
+ options.optimization ??= {};
23
+ options.optimization.minimize ??= false;
24
+ compiler.setOptions(options);
25
+ },
26
+ compiler: async (context) => {
27
+ const compiler = (0, common_1.getCompiler)(context, name);
28
+ if (caseConfig.compilerCallback) {
29
+ compiler.createCompilerWithCallback(caseConfig.compilerCallback);
30
+ }
31
+ else {
32
+ compiler.createCompiler();
33
+ }
34
+ const c = compiler.getCompiler();
35
+ c.outputFileSystem = {
36
+ // CHANGE: Added support for the `options` parameter to enable recursive directory creation,
37
+ // accommodating Rspack's requirement that differs from webpack's usage
38
+ mkdir(path, callback) {
39
+ const recursive = false;
40
+ // if (typeof options === "function") {
41
+ // callback = options;
42
+ // } else if (options) {
43
+ // if (options.recursive !== undefined) recursive = options.recursive;
44
+ // }
45
+ logs.mkdir.push(path);
46
+ if (recursive) {
47
+ callback();
48
+ }
49
+ else {
50
+ const err = new Error();
51
+ err.code = "EEXIST";
52
+ callback(err);
53
+ }
54
+ },
55
+ writeFile(name, content, callback) {
56
+ logs.writeFile.push(name, content);
57
+ files[name] = content.toString("utf-8");
58
+ callback();
59
+ },
60
+ stat(path, callback) {
61
+ callback(new Error("ENOENT"));
62
+ }
63
+ };
64
+ c.hooks.compilation.tap("CompilerTest", compilation => (compilation.bail = true));
65
+ await caseConfig.compiler?.(context, c);
66
+ },
67
+ build: async (context) => {
68
+ const compiler = (0, common_1.getCompiler)(context, name);
69
+ if (typeof caseConfig.build === "function") {
70
+ await caseConfig.build?.(context, compiler.getCompiler());
71
+ }
72
+ else {
73
+ await compiler.build();
74
+ }
75
+ },
76
+ run: async (env, context) => { },
77
+ check: async (env, context) => {
78
+ const compiler = (0, common_1.getCompiler)(context, name);
79
+ const c = compiler.getCompiler();
80
+ const stats = compiler.getStats();
81
+ if (caseConfig.error) {
82
+ const statsJson = stats?.toJson({
83
+ modules: true,
84
+ reasons: true
85
+ });
86
+ const compilation = stats?.compilation;
87
+ await caseConfig.check?.({
88
+ context,
89
+ compiler: c,
90
+ stats: statsJson,
91
+ compilation,
92
+ files
93
+ });
94
+ }
95
+ else if (stats) {
96
+ expect(typeof stats).toBe("object");
97
+ const compilation = stats.compilation;
98
+ const statsJson = stats.toJson({
99
+ modules: true,
100
+ reasons: true
101
+ });
102
+ expect(typeof statsJson).toBe("object");
103
+ expect(statsJson).toHaveProperty("errors");
104
+ expect(Array.isArray(statsJson.errors)).toBe(true);
105
+ if (statsJson.errors.length > 0) {
106
+ expect(statsJson.errors[0]).toBeInstanceOf(Object);
107
+ throw statsJson.errors[0];
108
+ }
109
+ statsJson.logs = logs;
110
+ await caseConfig.check?.({
111
+ context,
112
+ stats: statsJson,
113
+ files,
114
+ compiler: c,
115
+ compilation
116
+ });
117
+ }
118
+ else {
119
+ await caseConfig.check?.({
120
+ context,
121
+ files,
122
+ compiler: c
123
+ });
124
+ }
125
+ },
126
+ after: async (context) => {
127
+ await context.closeCompiler(name);
128
+ }
129
+ };
130
+ }
131
+ const creator = new creator_1.BasicCaseCreator({
132
+ clean: true,
133
+ describe: false,
134
+ steps: ({ name, caseConfig }) => {
135
+ return [createCompilerProcessor(name, caseConfig)];
136
+ },
137
+ concurrent: false
138
+ });
7
139
  function createCompilerCase(name, src, dist, testConfig) {
8
140
  let caseConfigList = require(testConfig);
9
141
  if (!Array.isArray(caseConfigList)) {
10
142
  caseConfigList = [caseConfigList];
11
143
  }
12
- const runner = (0, simple_1.getSimpleProcessorRunner)(src, dist);
13
- for (const caseConfig of caseConfigList) {
14
- const testFn = caseConfig.skip ? it.skip : it;
15
- testFn(caseConfig.description, async () => {
16
- const logs = {
17
- mkdir: [],
18
- writeFile: []
19
- };
20
- const files = {};
21
- await runner(name, new processor_1.SimpleTaskProcessor({
22
- name: name,
23
- compilerType: type_1.ECompilerType.Rspack,
24
- compilerCallback: caseConfig.compilerCallback,
25
- build: caseConfig.build,
26
- options: context => {
27
- const options = caseConfig.options?.(context) || {};
28
- options.mode ??= "production";
29
- options.context ??= context.getSource();
30
- options.entry ??= "./a.js";
31
- options.output ??= {};
32
- options.output.path ??= "/";
33
- options.output.pathinfo ??= true;
34
- options.optimization ??= {};
35
- options.optimization.minimize ??= false;
36
- return options;
37
- },
38
- async compiler(context, compiler) {
39
- compiler.outputFileSystem = {
40
- // CHANGE: Added support for the `options` parameter to enable recursive directory creation,
41
- // accommodating Rspack's requirement that differs from webpack's usage
42
- mkdir(path, callback) {
43
- const recursive = false;
44
- // if (typeof options === "function") {
45
- // callback = options;
46
- // } else if (options) {
47
- // if (options.recursive !== undefined) recursive = options.recursive;
48
- // }
49
- logs.mkdir.push(path);
50
- if (recursive) {
51
- callback();
52
- }
53
- else {
54
- const err = new Error();
55
- err.code = "EEXIST";
56
- callback(err);
57
- }
58
- },
59
- writeFile(name, content, callback) {
60
- logs.writeFile.push(name, content);
61
- files[name] = content.toString("utf-8");
62
- callback();
63
- },
64
- stat(path, callback) {
65
- callback(new Error("ENOENT"));
66
- }
67
- };
68
- compiler.hooks.compilation.tap("CompilerTest", compilation => (compilation.bail = true));
69
- await caseConfig.compiler?.(context, compiler);
70
- },
71
- async check(context, compiler, stats) {
72
- if (caseConfig.error) {
73
- const statsJson = stats?.toJson({
74
- modules: true,
75
- reasons: true
76
- });
77
- const compilation = stats?.compilation;
78
- await caseConfig.check?.({
79
- context,
80
- compiler,
81
- stats: statsJson,
82
- compilation,
83
- files
84
- });
85
- }
86
- else if (stats) {
87
- expect(typeof stats).toBe("object");
88
- const compilation = stats.compilation;
89
- const statsJson = stats.toJson({
90
- modules: true,
91
- reasons: true
92
- });
93
- expect(typeof statsJson).toBe("object");
94
- expect(statsJson).toHaveProperty("errors");
95
- expect(Array.isArray(statsJson.errors)).toBe(true);
96
- if (statsJson.errors.length > 0) {
97
- expect(statsJson.errors[0]).toBeInstanceOf(Object);
98
- throw statsJson.errors[0];
99
- }
100
- statsJson.logs = logs;
101
- await caseConfig.check?.({
102
- context,
103
- stats: statsJson,
104
- files,
105
- compiler,
106
- compilation
107
- });
108
- }
109
- else {
110
- await caseConfig.check?.({
111
- context,
112
- files,
113
- compiler
114
- });
115
- }
116
- }
117
- }));
144
+ for (let i = 0; i < caseConfigList.length; i++) {
145
+ const caseConfig = caseConfigList[i];
146
+ if (caseConfig.skip) {
147
+ it.skip(`${name}[${i}]`, () => { });
148
+ continue;
149
+ }
150
+ creator.create(`${name}[${i}]`, src, dist, undefined, {
151
+ caseConfig,
152
+ description: () => caseConfig.description
118
153
  });
119
154
  }
120
155
  }
@@ -1,3 +1,7 @@
1
- import { ECompilerType, type TTestConfig } from "../type";
1
+ import type { ECompilerType, ITestContext, ITestProcessor, TCompilerOptions, TTestConfig } from "../type";
2
2
  export type TConfigCaseConfig = Omit<TTestConfig<ECompilerType.Rspack>, "validate">;
3
+ export declare function createConfigProcessor(name: string): ITestProcessor;
3
4
  export declare function createConfigCase(name: string, src: string, dist: string): void;
5
+ export declare function defaultOptions(index: number, context: ITestContext): TCompilerOptions<ECompilerType.Rspack>;
6
+ export declare function overrideOptions(index: number, context: ITestContext, options: TCompilerOptions<ECompilerType.Rspack>): void;
7
+ export declare function findBundle(index: number, context: ITestContext, options: TCompilerOptions<ECompilerType.Rspack>): string | string[];
@@ -1,10 +1,38 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createConfigProcessor = createConfigProcessor;
3
7
  exports.createConfigCase = createConfigCase;
4
- const config_1 = require("../processor/config");
5
- const runner_1 = require("../runner");
8
+ exports.defaultOptions = defaultOptions;
9
+ exports.overrideOptions = overrideOptions;
10
+ exports.findBundle = findBundle;
11
+ const fs_extra_1 = __importDefault(require("fs-extra"));
12
+ const path_1 = __importDefault(require("path"));
13
+ const parseResource_1 = require("../helper/legacy/parseResource");
6
14
  const creator_1 = require("../test/creator");
7
- const type_1 = require("../type");
15
+ const common_1 = require("./common");
16
+ const runner_1 = require("./runner");
17
+ function createConfigProcessor(name) {
18
+ return {
19
+ config: async (context) => {
20
+ (0, common_1.configMultiCompiler)(context, name, ["rspack.config.cjs", "rspack.config.js", "webpack.config.js"], defaultOptions, overrideOptions);
21
+ },
22
+ compiler: async (context) => {
23
+ await (0, common_1.compiler)(context, name);
24
+ },
25
+ build: async (context) => {
26
+ await (0, common_1.build)(context, name);
27
+ },
28
+ run: async (env, context) => {
29
+ await (0, common_1.run)(env, context, name, (context) => (0, common_1.findMultiCompilerBundle)(context, name, findBundle));
30
+ },
31
+ check: async (env, context) => {
32
+ await (0, common_1.check)(env, context, name);
33
+ }
34
+ };
35
+ }
8
36
  const creator = new creator_1.BasicCaseCreator({
9
37
  clean: true,
10
38
  describe: false,
@@ -19,21 +47,79 @@ const creator = new creator_1.BasicCaseCreator({
19
47
  return res;
20
48
  };
21
49
  },
22
- steps: ({ name }) => [
23
- new config_1.ConfigProcessor({
24
- name,
25
- runable: true,
26
- compilerType: type_1.ECompilerType.Rspack,
27
- configFiles: [
28
- "rspack.config.cjs",
29
- "rspack.config.js",
30
- "webpack.config.js"
31
- ]
32
- })
33
- ],
34
- runner: runner_1.MultipleRunnerFactory,
50
+ steps: ({ name }) => [createConfigProcessor(name)],
51
+ runner: {
52
+ key: runner_1.getMultiCompilerRunnerKey,
53
+ runner: runner_1.createMultiCompilerRunner
54
+ },
35
55
  concurrent: true
36
56
  });
37
57
  function createConfigCase(name, src, dist) {
38
58
  creator.create(name, src, dist);
39
59
  }
60
+ function defaultOptions(index, context) {
61
+ return {
62
+ context: context.getSource(),
63
+ mode: "production",
64
+ target: "async-node",
65
+ devtool: false,
66
+ cache: false,
67
+ output: {
68
+ path: context.getDist()
69
+ },
70
+ optimization: {
71
+ minimize: false
72
+ },
73
+ experiments: {
74
+ css: true,
75
+ rspackFuture: {
76
+ bundlerInfo: {
77
+ force: false
78
+ }
79
+ },
80
+ inlineConst: true,
81
+ lazyBarrel: true
82
+ }
83
+ };
84
+ }
85
+ function overrideOptions(index, context, options) {
86
+ if (!options.entry) {
87
+ options.entry = "./index.js";
88
+ }
89
+ if (options.amd === undefined) {
90
+ options.amd = {};
91
+ }
92
+ if (!options.output?.filename) {
93
+ const outputModule = options.experiments?.outputModule;
94
+ options.output ??= {};
95
+ options.output.filename = `bundle${index}${outputModule ? ".mjs" : ".js"}`;
96
+ }
97
+ if (options.cache === undefined)
98
+ options.cache = false;
99
+ if (!global.printLogger) {
100
+ options.infrastructureLogging = {
101
+ level: "error"
102
+ };
103
+ }
104
+ }
105
+ function findBundle(index, context, options) {
106
+ const testConfig = context.getTestConfig();
107
+ if (typeof testConfig.findBundle === "function") {
108
+ return testConfig.findBundle(index, options);
109
+ }
110
+ const ext = path_1.default.extname((0, parseResource_1.parseResource)(options.output?.filename).path);
111
+ const bundlePath = [];
112
+ if (options.output?.path &&
113
+ fs_extra_1.default.existsSync(path_1.default.join(options.output.path, `bundle${index}${ext}`))) {
114
+ if (options.experiments?.css) {
115
+ const cssOutputPath = path_1.default.join(options.output.path, (typeof options.output?.cssFilename === "string" &&
116
+ options.output?.cssFilename) ||
117
+ `bundle${index}.css`);
118
+ if (fs_extra_1.default.existsSync(cssOutputPath)) {
119
+ bundlePath.push(`./bundle${index}.css`);
120
+ }
121
+ }
122
+ bundlePath.push(`./bundle${index}${ext}`);
123
+ }
124
+ return bundlePath;
125
+ }