@rspack/test-tools 0.4.2 → 0.4.3

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.
@@ -69,9 +69,9 @@ function compareModules(modules, sourceModules, distModules, formatOptions) {
69
69
  for (let name of modules) {
70
70
  const renamed = (0, replace_runtime_module_name_1.replaceRuntimeModuleName)(name);
71
71
  const sourceContent = sourceModules.has(renamed) &&
72
- (0, format_code_1.formatCode)(sourceModules.get(renamed), formatOptions);
72
+ (0, format_code_1.formatCode)(name, sourceModules.get(renamed), formatOptions);
73
73
  const distContent = distModules.has(renamed) &&
74
- (0, format_code_1.formatCode)(distModules.get(renamed), formatOptions);
74
+ (0, format_code_1.formatCode)(name, distModules.get(renamed), formatOptions);
75
75
  compareResults.push({
76
76
  ...compareContent(sourceContent, distContent),
77
77
  name
@@ -90,9 +90,9 @@ function compareContent(sourceContent, distContent) {
90
90
  source: sourceContent,
91
91
  dist: distContent,
92
92
  lines: {
93
- source: lines,
93
+ source: 0,
94
94
  common: lines,
95
- dist: lines
95
+ dist: 0
96
96
  }
97
97
  };
98
98
  }
@@ -3,5 +3,9 @@ export interface IFormatCodeOptions {
3
3
  ignorePropertyQuotationMark: boolean;
4
4
  ignoreModuleId: boolean;
5
5
  ignoreModuleArguments: boolean;
6
+ ignoreBlockOnlyStatement: boolean;
7
+ ignoreSwcHelpersPath: boolean;
8
+ ignoreObjectPropertySequence: boolean;
9
+ ignoreCssFilePath: boolean;
6
10
  }
7
- export declare function formatCode(raw: string, options: IFormatCodeOptions): string;
11
+ export declare function formatCode(name: string, raw: string, options: IFormatCodeOptions): string;
@@ -32,7 +32,10 @@ const generator_1 = __importDefault(require("@babel/generator"));
32
32
  const traverse_1 = __importDefault(require("@babel/traverse"));
33
33
  const T = __importStar(require("@babel/types"));
34
34
  const replace_module_argument_1 = require("./replace-module-argument");
35
- function formatCode(raw, options) {
35
+ const SWC_HELPER_PATH_REG = /^_swc_helpers_[a-zA-Z\d_-]+__WEBPACK_IMPORTED_MODULE_xxx__$/;
36
+ const CSS_FILE_EXT_REG = /(le|sa|c|sc)ss$/;
37
+ const INVALID_PATH_REG = /[<>:"/\\|?*.]/g;
38
+ function formatCode(name, raw, options) {
36
39
  const ast = (0, parser_1.parse)(raw, {
37
40
  sourceType: "unambiguous"
38
41
  });
@@ -44,17 +47,91 @@ function formatCode(raw, options) {
44
47
  keyPath.replaceWith(T.stringLiteral(keyPath.node.name));
45
48
  }
46
49
  }
50
+ if (options.ignoreCssFilePath && CSS_FILE_EXT_REG.test(name)) {
51
+ const valuePath = path.get("value");
52
+ if (valuePath.isStringLiteral()) {
53
+ valuePath.node.value = valuePath.node.value.replace(INVALID_PATH_REG, "-");
54
+ }
55
+ }
47
56
  },
48
57
  Identifier(path) {
49
58
  if (options.ignoreModuleId) {
50
59
  path.node.name = path.node.name.replace(/__WEBPACK_IMPORTED_MODULE_\d+__/g, "__WEBPACK_IMPORTED_MODULE_xxx__");
51
60
  }
61
+ if (options.ignoreSwcHelpersPath) {
62
+ if (SWC_HELPER_PATH_REG.test(path.node.name)) {
63
+ path.node.name = `$$SWC_HELPERS$$`;
64
+ }
65
+ }
66
+ },
67
+ IfStatement(path) {
68
+ if (options.ignoreBlockOnlyStatement) {
69
+ const consequent = path.get("consequent");
70
+ if (consequent.isBlockStatement() &&
71
+ consequent.node.body.length === 1) {
72
+ consequent.node.body[0].leadingComments =
73
+ consequent.node.leadingComments;
74
+ consequent.replaceWith(T.cloneNode(consequent.node.body[0], true, false));
75
+ }
76
+ const alternate = path.get("alternate");
77
+ if (alternate.isBlockStatement() && alternate.node.body.length === 1) {
78
+ alternate.node.body[0].leadingComments =
79
+ alternate.node.leadingComments;
80
+ alternate.replaceWith(T.cloneNode(alternate.node.body[0], true, false));
81
+ }
82
+ }
83
+ },
84
+ For(path) {
85
+ if (options.ignoreBlockOnlyStatement) {
86
+ const body = path.get("body");
87
+ if (body.isBlockStatement() && body.node.body.length === 1) {
88
+ body.node.body[0].leadingComments = body.node.leadingComments;
89
+ body.replaceWith(T.cloneNode(body.node.body[0], true, false));
90
+ }
91
+ }
92
+ },
93
+ ObjectExpression(path) {
94
+ if (options.ignoreObjectPropertySequence) {
95
+ let result = [];
96
+ let safe = [];
97
+ while (path.node.properties.length || safe.length) {
98
+ const cur = path.node.properties.shift();
99
+ if (cur && T.isObjectProperty(cur)) {
100
+ if (T.isIdentifier(cur.key)) {
101
+ safe.push({
102
+ name: cur.key.name,
103
+ node: cur
104
+ });
105
+ continue;
106
+ }
107
+ if (T.isStringLiteral(cur.key)) {
108
+ safe.push({
109
+ name: cur.key.value,
110
+ node: cur
111
+ });
112
+ continue;
113
+ }
114
+ }
115
+ if (safe.length) {
116
+ safe.sort((a, b) => (a.name > b.name ? 1 : -1));
117
+ result.push(...safe.map(n => n.node));
118
+ safe = [];
119
+ }
120
+ if (cur) {
121
+ result.push(cur);
122
+ }
123
+ }
124
+ path.node.properties = result;
125
+ }
52
126
  }
53
127
  });
54
128
  let result = (0, generator_1.default)(ast, {
55
129
  comments: false,
56
130
  compact: false,
57
- concise: false
131
+ concise: false,
132
+ jsescOption: {
133
+ quotes: "double"
134
+ }
58
135
  }).code;
59
136
  if (options.ignoreModuleArguments) {
60
137
  result = (0, replace_module_argument_1.replaceModuleArgument)(result);
@@ -2,6 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.replaceModuleArgument = void 0;
4
4
  function replaceModuleArgument(raw) {
5
- return raw.trim().replace(/^\(function \([\w_,\s]+\) {/, "(function () {");
5
+ return raw
6
+ .trim()
7
+ .replace(/^\(function\s?\([\w_,\s]+\)\s?{/, "(function () {");
6
8
  }
7
9
  exports.replaceModuleArgument = replaceModuleArgument;
@@ -96,6 +96,10 @@ class DiffProcessor {
96
96
  },
97
97
  path: dist
98
98
  },
99
+ optimization: {
100
+ mangleExports: false,
101
+ concatenateModules: false
102
+ },
99
103
  plugins: [(0, module_placeholder_plugin_1.createModulePlaceholderPlugin)(this.options.webpackPath)]
100
104
  }, {
101
105
  arrayMerge: (a, b) => [...a, ...b]
@@ -106,6 +110,9 @@ class DiffProcessor {
106
110
  output: {
107
111
  path: dist
108
112
  },
113
+ optimization: {
114
+ mangleExports: false
115
+ },
109
116
  experiments: {
110
117
  rspackFuture: {
111
118
  disableTransformByDefault: true
@@ -120,6 +127,10 @@ class DiffProcessor {
120
127
  ignoreModuleArguments: this.options.ignoreModuleArguments,
121
128
  ignoreModuleId: this.options.ignoreModuleId,
122
129
  ignorePropertyQuotationMark: this.options.ignorePropertyQuotationMark,
130
+ ignoreBlockOnlyStatement: this.options.ignoreBlockOnlyStatement,
131
+ ignoreSwcHelpersPath: this.options.ignoreSwcHelpersPath,
132
+ ignoreObjectPropertySequence: this.options.ignoreObjectPropertySequence,
133
+ ignoreCssFilePath: this.options.ignoreCssFilePath,
123
134
  replacements: this.options.replacements || {}
124
135
  };
125
136
  for (let hash of this.hashes) {
@@ -52,11 +52,12 @@ class DiffHtmlReporter {
52
52
  root: id,
53
53
  data: items
54
54
  };
55
- const content = template.replace(DIFF_STATS_PLACEHOLDER, JSON.stringify(data));
56
55
  const casename = path_1.default.basename(id);
57
56
  const extname = path_1.default.extname(viewerFile);
58
57
  const filename = path_1.default.basename(viewerFile, extname);
58
+ const content = template.replace(`<script id="${DIFF_STATS_PLACEHOLDER}"></script>`, `<script src="${filename}_${casename}.js"></script>`);
59
59
  fs_extra_1.default.writeFileSync(path_1.default.join(this.options.dist, `${filename}_${casename}${extname}`), content, "utf-8");
60
+ fs_extra_1.default.writeFileSync(path_1.default.join(this.options.dist, `${filename}_${casename}.js`), `window.$$diff_detail$$ = ${JSON.stringify(data)}`, "utf-8");
60
61
  }
61
62
  }
62
63
  else {
@@ -35,8 +35,8 @@ class DiffStatsReporter {
35
35
  if (item.type === type_1.ECompareResultType.Missing)
36
36
  continue;
37
37
  const moduleType = item.name.startsWith("webpack/runtime")
38
- ? "normal"
39
- : "runtime";
38
+ ? "runtime"
39
+ : "normal";
40
40
  // handle modules
41
41
  if (item.type === type_1.ECompareResultType.OnlySource) {
42
42
  current[`${type_1.ECompilerType.Rspack}|${moduleType}|${"modules"}`]++;
@@ -27,7 +27,8 @@ function createRenderRuntimeModulesFn(Template) {
27
27
  runtimeSource = codeGenResult.sources.get("runtime");
28
28
  }
29
29
  if (runtimeSource) {
30
- source.add(Template.toNormalComment(`start::${module.identifier()}`) + "\n");
30
+ let identifier = module.identifier();
31
+ source.add(Template.toNormalComment(`start::${identifier}`) + "\n");
31
32
  if (!module.shouldIsolate()) {
32
33
  source.add(runtimeSource);
33
34
  source.add("\n\n");
@@ -42,7 +43,7 @@ function createRenderRuntimeModulesFn(Template) {
42
43
  source.add(new PrefixSource("\t", runtimeSource));
43
44
  source.add("\n}();\n\n");
44
45
  }
45
- source.add(Template.toNormalComment(`end::${module.identifier()}`) + "\n");
46
+ source.add(Template.toNormalComment(`end::${identifier}`) + "\n");
46
47
  }
47
48
  }
48
49
  return source;
@@ -90,7 +91,11 @@ function createModulePlaceholderPlugin(webpackPath) {
90
91
  let footer = cacheEntry.footer;
91
92
  if (header === undefined) {
92
93
  const req = module.readableIdentifier(requestShortener);
93
- const reqStr = req.replace(/\*\//g, "*_/");
94
+ let reqStr = req.replace(/\*\//g, "*_/");
95
+ // handle css module identifier
96
+ if (reqStr.startsWith("css ")) {
97
+ reqStr = reqStr.replace(/^css[\s]+/, "").trim();
98
+ }
94
99
  header = new RawSource(`\n/* start::${reqStr} */\n`);
95
100
  footer = new RawSource(`\n/* end::${reqStr} */\n`);
96
101
  cacheEntry.header = header;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspack/test-tools",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "license": "MIT",
5
5
  "description": "Test tools for rspack",
6
6
  "main": "dist/index.js",
@@ -37,7 +37,7 @@
37
37
  "fs-extra": "^11.1.1",
38
38
  "jest-diff": "^29.7.0",
39
39
  "webpack-sources": "3.2.3",
40
- "@rspack/core": "0.4.2"
40
+ "@rspack/core": "0.4.3"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@arco-design/web-react": "^2.56.1",
@@ -56,7 +56,7 @@
56
56
  "react-refresh": "0.13.0",
57
57
  "typescript": "5.1.6",
58
58
  "webpack": "5.89.0",
59
- "@rspack/cli": "0.4.2"
59
+ "@rspack/cli": "0.4.3"
60
60
  },
61
61
  "peerDependenciesMeta": {},
62
62
  "jest": {