path-serializer 0.4.0 → 0.5.0

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.
@@ -2,58 +2,73 @@ export declare function createSnapshotSerializer(options?: SnapshotSerializerOpt
2
2
 
3
3
  export declare interface Features {
4
4
  /**
5
- * /foo/node_modules/.pnpm -> <ROOT>/node_modules/.pnpm
5
+ * `file:///foo/rspack/packages/core/src` -> `<WORKSPACE>/src`
6
6
  * @default true
7
7
  */
8
- replaceRoot?: boolean;
8
+ replaceWorkspaceWithFileProtocol?: boolean;
9
+ /**
10
+ * `file:///foo/node_modules/.pnpm` -> `<ROOT>/node_modules/.pnpm`
11
+ * @default true
12
+ */
13
+ replaceRootWithFileProtocol?: boolean;
14
+ /**
15
+ * `D:\\foo\\node_modules\\<PNPM_INNER>\\css-loader\\utils.ts`
16
+ *
17
+ * ->
18
+ *
19
+ * `/d/foo/node_modules/<PNPM_INNER>/css-loader/utils.ts`
20
+ * @default true
21
+ */
22
+ transformWin32Path?: boolean;
9
23
  /**
10
- * /foo/rspack/packages/core/src -> <WORKSPACE>/src
24
+ * `/foo/rspack/packages/core/src` -> `<WORKSPACE>/src`
11
25
  * @default true
12
26
  */
13
27
  replaceWorkspace?: boolean;
14
28
  /**
15
- * /foo/node_modules/.pnpm/@swc+helpers@0.5.11/node_modules/@swc/helpers/esm/_class_private_method_get.js
16
- * -> /foo/node_modules/<PNPM_INNER>/@swc/helpers/esm/_class_private_method_get.js
29
+ * `/foo/node_modules/.pnpm` -> `<ROOT>/node_modules/.pnpm`
17
30
  * @default true
18
31
  */
19
- replacePnpmInner?: boolean;
32
+ replaceRoot?: boolean;
20
33
  /**
21
- * <TEMP>
34
+ * `/foo/node_modules/.pnpm/@swc+helpers@0.5.11/node_modules/@swc/helpers/esm/_class_private_method_get.js`
35
+ *
36
+ * ->
37
+ *
38
+ * `/foo/node_modules/<PNPM_INNER>/@swc/helpers/esm/_class_private_method_get.js`
22
39
  * @default true
23
40
  */
24
- replaceTmpDir?: boolean;
41
+ replacePnpmInner?: boolean;
25
42
  /**
26
- * <HOME>
43
+ * `${os.tmpdir()}/src/index.ts` -> `<TEMP>/src/index.ts`
27
44
  * @default true
28
45
  */
29
- replaceHomeDir?: boolean;
46
+ replaceTmpDir?: boolean;
30
47
  /**
31
- * foo -> "foo"
48
+ * `${os.homedir()}/src/index.ts` -> `<HOME>/src/index.ts`
32
49
  * @default true
33
50
  */
34
- addDoubleQuotes?: boolean;
51
+ replaceHomeDir?: boolean;
35
52
  /**
36
- * D:\\foo\\node_modules\\<PNPM_INNER>\\css-loader\\utils.ts
37
- * -> /d/foo/node_modules/<PNPM_INNER>/css-loader/utils.ts
53
+ * `\u001b[1mBold Text\u001b[0m` -> `<CLR=BOLD>Bold Text<CLR=0>`
38
54
  * @default true
39
55
  */
40
- transformWin32Path?: boolean;
56
+ transformCLR?: boolean;
41
57
  /**
42
- * "" -> \"\"
58
+ * `""` -> `\"\"`
43
59
  * @default true
44
60
  */
45
61
  escapeDoubleQuotes?: boolean;
46
62
  /**
47
- * \r\n -> \n
63
+ * `\r\n` -> `\n`
48
64
  * @default true
49
65
  */
50
66
  escapeEOL?: boolean;
51
67
  /**
52
- * \u001b[1mBold Text\u001b[0m
53
- * -> <CLR=BOLD>Bold Text<CLR=0>
68
+ * `foo` -> `"foo"`
54
69
  * @default true
55
70
  */
56
- transformCLR?: boolean;
71
+ addDoubleQuotes?: boolean;
57
72
  }
58
73
 
59
74
  export declare interface PathMatcher {
@@ -89,6 +104,10 @@ export declare interface SnapshotSerializerOptions {
89
104
  * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
90
105
  */
91
106
  replace?: PathMatcher[];
107
+ /**
108
+ * @description all the features is executed in order
109
+ */
110
+ features?: Features;
92
111
  /**
93
112
  * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
94
113
  */
@@ -97,7 +116,6 @@ export declare interface SnapshotSerializerOptions {
97
116
  * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
98
117
  */
99
118
  afterSerialize?: (val: string) => string;
100
- features?: Features;
101
119
  }
102
120
 
103
121
  export { }
package/dist/cjs/index.js CHANGED
@@ -189,6 +189,7 @@ function __webpack_require__(moduleId) {
189
189
  __webpack_require__.d(__webpack_exports__, {
190
190
  createSnapshotSerializer: ()=>/* reexport */ createSnapshotSerializer
191
191
  });
192
+ const external_node_url_namespaceObject = require("node:url");
192
193
  /**
193
194
  * A specialized version of `_.reduce` for arrays without support for
194
195
  * iteratee shorthands.
@@ -869,14 +870,14 @@ function __webpack_require__(moduleId) {
869
870
  const normalizePathToPosix = (p)=>upath_default().normalizeSafe(external_node_path_default().normalize(p || '')).replace(/^([a-zA-Z]+):/, (_, m)=>`/${m.toLowerCase()}`);
870
871
  // find the path in code and replace it with normalizePathToPosix
871
872
  const normalizeCodeToPosix = (code)=>code.replace(// ignore http, https, file
872
- /(?<![a-zA-Z])([a-zA-Z]:[\\/])([-\u4e00-\u9fa5\w\s.()~!@#$%^&()\[\]{}+=]+[\\/])*/g, (match, _diskName)=>normalizePathToPosix(match));
873
+ /(?<![a-zA-Z])([a-zA-Z]:[\\/]+)([-\u4e00-\u9fa5\w\s.()~!@#$%^&()\[\]{}+=]+[\\/]+)*/g, (match, _diskName)=>normalizePathToPosix(match.replace(/[\\]{2,}/g, '\\')));
873
874
  const normalizeCLR = (str)=>str.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, '<CLR=$1,BOLD>').replace(/\u001b\[1m/g, '<CLR=BOLD>').replace(/\u001b\[39m\u001b\[22m/g, '</CLR>').replace(/\u001b\[([0-9;]*)m/g, '<CLR=$1>') // CHANGE: The time unit display in Rspack is second
874
875
  // CHANGE2: avoid a bad case "./react/assets.svg" -> "./react/assetsXsvg"
875
876
  // modified based on https://github.com/webpack/webpack/blob/001cab14692eb9a833c6b56709edbab547e291a1/test/StatsTestCases.basictest.js#L199
876
877
  .replace(/[0-9]+(\.[0-9]+)*(<\/CLR>)?(\s?s)/g, 'X$2$3');
877
878
  function createSnapshotSerializer(options) {
878
879
  const { root = process.cwd(), workspace = '', replace: customMatchers = [], replacePost: customPostMatchers = [], features = {}, beforeSerialize, afterSerialize } = options || {};
879
- const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, replaceHomeDir = true, addDoubleQuotes = true, transformWin32Path = true, escapeDoubleQuotes = true, escapeEOL = true, transformCLR = true } = features;
880
+ const { replaceWorkspaceWithFileProtocol = true, replaceWorkspace = true, replaceRootWithFileProtocol = true, replaceRoot = true, replacePnpmInner = true, replaceTmpDir = true, replaceHomeDir = true, addDoubleQuotes = true, transformWin32Path = true, escapeDoubleQuotes = true, escapeEOL = true, transformCLR = true } = features;
880
881
  function createPathMatchers() {
881
882
  const pathMatchers = [];
882
883
  pathMatchers.push(...customMatchers);
@@ -904,6 +905,10 @@ function __webpack_require__(moduleId) {
904
905
  serialize (val) {
905
906
  let replaced = val;
906
907
  if (beforeSerialize) replaced = beforeSerialize(replaced);
908
+ if (replaceWorkspaceWithFileProtocol && workspace) // this is a polyfill for .replaceAll(string, string)
909
+ replaced = replaced.split((0, external_node_url_namespaceObject.pathToFileURL)(workspace).toString()).join('<WORKSPACE>');
910
+ if (replaceRootWithFileProtocol && root) // this is a polyfill for .replaceAll(string, string)
911
+ replaced = replaced.split((0, external_node_url_namespaceObject.pathToFileURL)(root).toString()).join('<ROOT>');
907
912
  if (transformWin32Path) replaced = normalizeCodeToPosix(replaced);
908
913
  replaced = applyMatcherReplacement(pathMatchers, replaced);
909
914
  if (transformCLR) replaced = normalizeCLR(replaced);
@@ -2,58 +2,73 @@ export declare function createSnapshotSerializer(options?: SnapshotSerializerOpt
2
2
 
3
3
  export declare interface Features {
4
4
  /**
5
- * /foo/node_modules/.pnpm -> <ROOT>/node_modules/.pnpm
5
+ * `file:///foo/rspack/packages/core/src` -> `<WORKSPACE>/src`
6
6
  * @default true
7
7
  */
8
- replaceRoot?: boolean;
8
+ replaceWorkspaceWithFileProtocol?: boolean;
9
+ /**
10
+ * `file:///foo/node_modules/.pnpm` -> `<ROOT>/node_modules/.pnpm`
11
+ * @default true
12
+ */
13
+ replaceRootWithFileProtocol?: boolean;
14
+ /**
15
+ * `D:\\foo\\node_modules\\<PNPM_INNER>\\css-loader\\utils.ts`
16
+ *
17
+ * ->
18
+ *
19
+ * `/d/foo/node_modules/<PNPM_INNER>/css-loader/utils.ts`
20
+ * @default true
21
+ */
22
+ transformWin32Path?: boolean;
9
23
  /**
10
- * /foo/rspack/packages/core/src -> <WORKSPACE>/src
24
+ * `/foo/rspack/packages/core/src` -> `<WORKSPACE>/src`
11
25
  * @default true
12
26
  */
13
27
  replaceWorkspace?: boolean;
14
28
  /**
15
- * /foo/node_modules/.pnpm/@swc+helpers@0.5.11/node_modules/@swc/helpers/esm/_class_private_method_get.js
16
- * -> /foo/node_modules/<PNPM_INNER>/@swc/helpers/esm/_class_private_method_get.js
29
+ * `/foo/node_modules/.pnpm` -> `<ROOT>/node_modules/.pnpm`
17
30
  * @default true
18
31
  */
19
- replacePnpmInner?: boolean;
32
+ replaceRoot?: boolean;
20
33
  /**
21
- * <TEMP>
34
+ * `/foo/node_modules/.pnpm/@swc+helpers@0.5.11/node_modules/@swc/helpers/esm/_class_private_method_get.js`
35
+ *
36
+ * ->
37
+ *
38
+ * `/foo/node_modules/<PNPM_INNER>/@swc/helpers/esm/_class_private_method_get.js`
22
39
  * @default true
23
40
  */
24
- replaceTmpDir?: boolean;
41
+ replacePnpmInner?: boolean;
25
42
  /**
26
- * <HOME>
43
+ * `${os.tmpdir()}/src/index.ts` -> `<TEMP>/src/index.ts`
27
44
  * @default true
28
45
  */
29
- replaceHomeDir?: boolean;
46
+ replaceTmpDir?: boolean;
30
47
  /**
31
- * foo -> "foo"
48
+ * `${os.homedir()}/src/index.ts` -> `<HOME>/src/index.ts`
32
49
  * @default true
33
50
  */
34
- addDoubleQuotes?: boolean;
51
+ replaceHomeDir?: boolean;
35
52
  /**
36
- * D:\\foo\\node_modules\\<PNPM_INNER>\\css-loader\\utils.ts
37
- * -> /d/foo/node_modules/<PNPM_INNER>/css-loader/utils.ts
53
+ * `\u001b[1mBold Text\u001b[0m` -> `<CLR=BOLD>Bold Text<CLR=0>`
38
54
  * @default true
39
55
  */
40
- transformWin32Path?: boolean;
56
+ transformCLR?: boolean;
41
57
  /**
42
- * "" -> \"\"
58
+ * `""` -> `\"\"`
43
59
  * @default true
44
60
  */
45
61
  escapeDoubleQuotes?: boolean;
46
62
  /**
47
- * \r\n -> \n
63
+ * `\r\n` -> `\n`
48
64
  * @default true
49
65
  */
50
66
  escapeEOL?: boolean;
51
67
  /**
52
- * \u001b[1mBold Text\u001b[0m
53
- * -> <CLR=BOLD>Bold Text<CLR=0>
68
+ * `foo` -> `"foo"`
54
69
  * @default true
55
70
  */
56
- transformCLR?: boolean;
71
+ addDoubleQuotes?: boolean;
57
72
  }
58
73
 
59
74
  export declare interface PathMatcher {
@@ -89,6 +104,10 @@ export declare interface SnapshotSerializerOptions {
89
104
  * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
90
105
  */
91
106
  replace?: PathMatcher[];
107
+ /**
108
+ * @description all the features is executed in order
109
+ */
110
+ features?: Features;
92
111
  /**
93
112
  * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
94
113
  */
@@ -97,7 +116,6 @@ export declare interface SnapshotSerializerOptions {
97
116
  * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
98
117
  */
99
118
  afterSerialize?: (val: string) => string;
100
- features?: Features;
101
119
  }
102
120
 
103
121
  export { }
@@ -1,4 +1,5 @@
1
1
  import * as __WEBPACK_EXTERNAL_MODULE_path__ from "path";
2
+ import * as __WEBPACK_EXTERNAL_MODULE_node_url__ from "node:url";
2
3
  import * as __WEBPACK_EXTERNAL_MODULE_node_fs__ from "node:fs";
3
4
  import * as __WEBPACK_EXTERNAL_MODULE_node_os__ from "node:os";
4
5
  import * as __WEBPACK_EXTERNAL_MODULE_node_path__ from "node:path";
@@ -844,14 +845,14 @@ var upath_default = /*#__PURE__*/ __webpack_require__.n(upath);
844
845
  const normalizePathToPosix = (p)=>upath_default().normalizeSafe(__WEBPACK_EXTERNAL_MODULE_node_path__["default"].normalize(p || '')).replace(/^([a-zA-Z]+):/, (_, m)=>`/${m.toLowerCase()}`);
845
846
  // find the path in code and replace it with normalizePathToPosix
846
847
  const normalizeCodeToPosix = (code)=>code.replace(// ignore http, https, file
847
- /(?<![a-zA-Z])([a-zA-Z]:[\\/])([-\u4e00-\u9fa5\w\s.()~!@#$%^&()\[\]{}+=]+[\\/])*/g, (match, _diskName)=>normalizePathToPosix(match));
848
+ /(?<![a-zA-Z])([a-zA-Z]:[\\/]+)([-\u4e00-\u9fa5\w\s.()~!@#$%^&()\[\]{}+=]+[\\/]+)*/g, (match, _diskName)=>normalizePathToPosix(match.replace(/[\\]{2,}/g, '\\')));
848
849
  const normalizeCLR = (str)=>str.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, '<CLR=$1,BOLD>').replace(/\u001b\[1m/g, '<CLR=BOLD>').replace(/\u001b\[39m\u001b\[22m/g, '</CLR>').replace(/\u001b\[([0-9;]*)m/g, '<CLR=$1>') // CHANGE: The time unit display in Rspack is second
849
850
  // CHANGE2: avoid a bad case "./react/assets.svg" -> "./react/assetsXsvg"
850
851
  // modified based on https://github.com/webpack/webpack/blob/001cab14692eb9a833c6b56709edbab547e291a1/test/StatsTestCases.basictest.js#L199
851
852
  .replace(/[0-9]+(\.[0-9]+)*(<\/CLR>)?(\s?s)/g, 'X$2$3');
852
853
  function createSnapshotSerializer(options) {
853
854
  const { root = process.cwd(), workspace = '', replace: customMatchers = [], replacePost: customPostMatchers = [], features = {}, beforeSerialize, afterSerialize } = options || {};
854
- const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, replaceHomeDir = true, addDoubleQuotes = true, transformWin32Path = true, escapeDoubleQuotes = true, escapeEOL = true, transformCLR = true } = features;
855
+ const { replaceWorkspaceWithFileProtocol = true, replaceWorkspace = true, replaceRootWithFileProtocol = true, replaceRoot = true, replacePnpmInner = true, replaceTmpDir = true, replaceHomeDir = true, addDoubleQuotes = true, transformWin32Path = true, escapeDoubleQuotes = true, escapeEOL = true, transformCLR = true } = features;
855
856
  function createPathMatchers() {
856
857
  const pathMatchers = [];
857
858
  pathMatchers.push(...customMatchers);
@@ -879,6 +880,10 @@ function createSnapshotSerializer(options) {
879
880
  serialize (val) {
880
881
  let replaced = val;
881
882
  if (beforeSerialize) replaced = beforeSerialize(replaced);
883
+ if (replaceWorkspaceWithFileProtocol && workspace) // this is a polyfill for .replaceAll(string, string)
884
+ replaced = replaced.split((0, __WEBPACK_EXTERNAL_MODULE_node_url__.pathToFileURL)(workspace).toString()).join('<WORKSPACE>');
885
+ if (replaceRootWithFileProtocol && root) // this is a polyfill for .replaceAll(string, string)
886
+ replaced = replaced.split((0, __WEBPACK_EXTERNAL_MODULE_node_url__.pathToFileURL)(root).toString()).join('<ROOT>');
882
887
  if (transformWin32Path) replaced = normalizeCodeToPosix(replaced);
883
888
  replaced = applyMatcherReplacement(pathMatchers, replaced);
884
889
  if (transformCLR) replaced = normalizeCLR(replaced);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "path-serializer",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "path-serializer",
5
5
  "keywords": [
6
6
  "snapshot",