path-serializer 0.3.4 → 0.4.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.
package/README.md CHANGED
@@ -2,14 +2,20 @@
2
2
 
3
3
  [![npm latest version](https://img.shields.io/npm/v/path-serializer?style=flat-square&color=98c379)](https://www.npmjs.com/package/path-serializer)
4
4
 
5
- 1. stabilize pnpm dependencies path and update dependencies smoothly
5
+ 1. stabilize pnpm dependencies path in snapshot
6
6
  2. transform win32 path to posix path
7
- and more...
7
+ 3. escapeEOL \r\n -> \n
8
+ and more...
8
9
 
9
10
  ```ts
10
11
  // __snapshots__/index.test.ts.snap
12
+ // 😭 bad
11
13
  {
12
- "loader" : "<ROOT>/node_modules/<PNPM_INNER>/@rspack/core/dist/builtin-plugin/css-extract/loader-js"
14
+ "loader" : "D:\\user\\rspack\\node_modules\\.pnpm\\css-loader@6.11.0_@rspack+core@packages+rspack_webpack@5.94.0_@swc+core@1.4.0_@swc+helpers@0._jlcdgjlw2ezzhg43ml3d627wdu\\node_modules\\css-loader\\utils.ts"
15
+ }
16
+ //😎👍🏻 good
17
+ {
18
+ "loader" : "<ROOT>/node_modules/<PNPM_INNER>/css-loader/utils.ts"
13
19
  }
14
20
  ```
15
21
 
@@ -21,14 +27,17 @@ import { createSnapshotSerializer } from 'path-serializer';
21
27
 
22
28
  expect.addSnapshotSerializer(
23
29
  createSnapshotSerializer({
24
- workspace: path.join(__dirname, '..'),
30
+ root: path.join(__dirname, '..'),
25
31
  }),
26
32
  );
27
33
  ```
28
34
 
29
- The specific usage can be found in [./src/types.ts](https://github.com/rspack-contrib/path-serializer/blob/main/src/types.ts)
35
+ More features can be found in [./src/types.ts](https://github.com/rspack-contrib/path-serializer/blob/main/src/types.ts)
30
36
 
31
37
  ## Showcases
32
38
 
33
39
  [Rslib](https://github.com/web-infra-dev/rslib/blob/3ff6859eb38171c731e447a1364afc021f8c501a/tests/setupVitestTests.ts)
40
+
34
41
  [Rsbuild](https://github.com/web-infra-dev/rsbuild/blob/a50eafa3519caaa66ecd6b0ccb2897a8194781ff/scripts/test-helper/vitest.setup.ts)
42
+
43
+ [Rspack](https://github.com/web-infra-dev/rspack/blob/5a6162c/packages/rspack-test-tools/src/helper/expect/placeholder.ts)
@@ -1,6 +1,6 @@
1
1
  export declare function createSnapshotSerializer(options?: SnapshotSerializerOptions): SnapshotSerializer;
2
2
 
3
- declare interface Features {
3
+ export declare interface Features {
4
4
  /**
5
5
  * /foo/node_modules/.pnpm -> <ROOT>/node_modules/.pnpm
6
6
  * @default true
@@ -18,10 +18,12 @@ declare interface Features {
18
18
  */
19
19
  replacePnpmInner?: boolean;
20
20
  /**
21
+ * <TEMP>
21
22
  * @default true
22
23
  */
23
24
  replaceTmpDir?: boolean;
24
25
  /**
26
+ * <HOME>
25
27
  * @default true
26
28
  */
27
29
  replaceHomeDir?: boolean;
@@ -37,6 +39,7 @@ declare interface Features {
37
39
  */
38
40
  transformWin32Path?: boolean;
39
41
  /**
42
+ * "" -> \"\"
40
43
  * @default true
41
44
  */
42
45
  escapeDoubleQuotes?: boolean;
@@ -53,19 +56,19 @@ declare interface Features {
53
56
  transformCLR?: boolean;
54
57
  }
55
58
 
56
- declare interface PathMatcher {
59
+ export declare interface PathMatcher {
57
60
  match: PathMatchExpression;
58
61
  mark: string | ((substring: string, ...args: any[]) => string);
59
62
  }
60
63
 
61
64
  declare type PathMatchExpression = string | RegExp;
62
65
 
63
- declare interface SnapshotSerializer {
66
+ export declare interface SnapshotSerializer {
64
67
  serialize: (val: any) => string;
65
68
  test: (arg0: any) => boolean;
66
69
  }
67
70
 
68
- declare interface SnapshotSerializerOptions {
71
+ export declare interface SnapshotSerializerOptions {
69
72
  /**
70
73
  * repository root path
71
74
  * @example '/Users/foo/codes/rsbuild/node_modules/.pnpm' -> '<ROOT>/node_modules/.pnpm'
@@ -79,13 +82,21 @@ declare interface SnapshotSerializerOptions {
79
82
  */
80
83
  workspace?: string;
81
84
  /**
82
- * @description replace -> workspace root pnpmInner temp home -> replacePost
85
+ * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
86
+ */
87
+ beforeSerialize?: (val: string) => string;
88
+ /**
89
+ * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
83
90
  */
84
91
  replace?: PathMatcher[];
85
92
  /**
86
- * @description replace -> workspace root pnpmInner temp home -> replacePost
93
+ * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
87
94
  */
88
95
  replacePost?: PathMatcher[];
96
+ /**
97
+ * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
98
+ */
99
+ afterSerialize?: (val: string) => string;
89
100
  features?: Features;
90
101
  }
91
102
 
package/dist/cjs/index.js CHANGED
@@ -139,7 +139,7 @@ function __webpack_require__(moduleId) {
139
139
  }
140
140
  /************************************************************************/ // webpack/runtime/compat_get_default_export
141
141
  (()=>{
142
- // getDefaultExport function for compatibility with non-harmony modules
142
+ // getDefaultExport function for compatibility with non-ESM modules
143
143
  __webpack_require__.n = function(module) {
144
144
  var getter = module && module.__esModule ? function() {
145
145
  return module['default'];
@@ -206,7 +206,7 @@ function __webpack_require__(moduleId) {
206
206
  while(++index < length)accumulator = iteratee(accumulator, array[index], index, array);
207
207
  return accumulator;
208
208
  }
209
- /* harmony default export */ const _arrayReduce = arrayReduce;
209
+ /* ESM default export */ const _arrayReduce = arrayReduce;
210
210
  /**
211
211
  * The base implementation of `_.propertyOf` without support for deep paths.
212
212
  *
@@ -218,7 +218,7 @@ function __webpack_require__(moduleId) {
218
218
  return null == object ? void 0 : object[key];
219
219
  };
220
220
  }
221
- /* harmony default export */ const _basePropertyOf = basePropertyOf;
221
+ /* ESM default export */ const _basePropertyOf = basePropertyOf;
222
222
  /** Used to map Latin Unicode letters to basic Latin letters. */ var deburredLetters = {
223
223
  // Latin-1 Supplement block.
224
224
  À: 'A',
@@ -421,14 +421,14 @@ function __webpack_require__(moduleId) {
421
421
  * @param {string} letter The matched letter to deburr.
422
422
  * @returns {string} Returns the deburred letter.
423
423
  */ var deburrLetter = _basePropertyOf(deburredLetters);
424
- /* harmony default export */ const _deburrLetter = deburrLetter;
424
+ /* ESM default export */ const _deburrLetter = deburrLetter;
425
425
  /** Detect free variable `global` from Node.js. */ var freeGlobal = 'object' == typeof global && global && global.Object === Object && global;
426
- /* harmony default export */ const _freeGlobal = freeGlobal;
426
+ /* ESM default export */ const _freeGlobal = freeGlobal;
427
427
  /** Detect free variable `self`. */ var freeSelf = 'object' == typeof self && self && self.Object === Object && self;
428
428
  /** Used as a reference to the global object. */ var _root_root = _freeGlobal || freeSelf || Function('return this')();
429
- /* harmony default export */ const _root = _root_root;
429
+ /* ESM default export */ const _root = _root_root;
430
430
  /** Built-in value references. */ var Symbol1 = _root.Symbol;
431
- /* harmony default export */ const _Symbol = Symbol1;
431
+ /* ESM default export */ const _Symbol = Symbol1;
432
432
  /**
433
433
  * A specialized version of `_.map` for arrays without support for iteratee
434
434
  * shorthands.
@@ -442,7 +442,7 @@ function __webpack_require__(moduleId) {
442
442
  while(++index < length)result[index] = iteratee(array[index], index, array);
443
443
  return result;
444
444
  }
445
- /* harmony default export */ const _arrayMap = arrayMap;
445
+ /* ESM default export */ const _arrayMap = arrayMap;
446
446
  /**
447
447
  * Checks if `value` is classified as an `Array` object.
448
448
  *
@@ -466,7 +466,7 @@ function __webpack_require__(moduleId) {
466
466
  * _.isArray(_.noop);
467
467
  * // => false
468
468
  */ var isArray_isArray = Array.isArray;
469
- /* harmony default export */ const isArray = isArray_isArray;
469
+ /* ESM default export */ const isArray = isArray_isArray;
470
470
  /** Used for built-in method references. */ var objectProto = Object.prototype;
471
471
  /** Used to check objects for own properties. */ var _getRawTag_hasOwnProperty = objectProto.hasOwnProperty;
472
472
  /**
@@ -494,7 +494,7 @@ function __webpack_require__(moduleId) {
494
494
  }
495
495
  return result;
496
496
  }
497
- /* harmony default export */ const _getRawTag = getRawTag;
497
+ /* ESM default export */ const _getRawTag = getRawTag;
498
498
  /** Used for built-in method references. */ var _objectToString_objectProto = Object.prototype;
499
499
  /**
500
500
  * Used to resolve the
@@ -510,7 +510,7 @@ function __webpack_require__(moduleId) {
510
510
  */ function objectToString(value) {
511
511
  return _objectToString_nativeObjectToString.call(value);
512
512
  }
513
- /* harmony default export */ const _objectToString = objectToString;
513
+ /* ESM default export */ const _objectToString = objectToString;
514
514
  /** `Object#toString` result references. */ var nullTag = '[object Null]', undefinedTag = '[object Undefined]';
515
515
  /** Built-in value references. */ var _baseGetTag_symToStringTag = _Symbol ? _Symbol.toStringTag : void 0;
516
516
  /**
@@ -523,7 +523,7 @@ function __webpack_require__(moduleId) {
523
523
  if (null == value) return void 0 === value ? undefinedTag : nullTag;
524
524
  return _baseGetTag_symToStringTag && _baseGetTag_symToStringTag in Object(value) ? _getRawTag(value) : _objectToString(value);
525
525
  }
526
- /* harmony default export */ const _baseGetTag = baseGetTag;
526
+ /* ESM default export */ const _baseGetTag = baseGetTag;
527
527
  /**
528
528
  * Checks if `value` is object-like. A value is object-like if it's not `null`
529
529
  * and has a `typeof` result of "object".
@@ -550,7 +550,7 @@ function __webpack_require__(moduleId) {
550
550
  */ function isObjectLike(value) {
551
551
  return null != value && 'object' == typeof value;
552
552
  }
553
- /* harmony default export */ const lodash_es_isObjectLike = isObjectLike;
553
+ /* ESM default export */ const lodash_es_isObjectLike = isObjectLike;
554
554
  /** `Object#toString` result references. */ var symbolTag = '[object Symbol]';
555
555
  /**
556
556
  * Checks if `value` is classified as a `Symbol` primitive or object.
@@ -571,7 +571,7 @@ function __webpack_require__(moduleId) {
571
571
  */ function isSymbol_isSymbol(value) {
572
572
  return 'symbol' == typeof value || lodash_es_isObjectLike(value) && _baseGetTag(value) == symbolTag;
573
573
  }
574
- /* harmony default export */ const isSymbol = isSymbol_isSymbol;
574
+ /* ESM default export */ const isSymbol = isSymbol_isSymbol;
575
575
  /** Used as references for various `Number` constants. */ var INFINITY = 1 / 0;
576
576
  /** Used to convert symbols to primitives and strings. */ var symbolProto = _Symbol ? _Symbol.prototype : void 0, symbolToString = symbolProto ? symbolProto.toString : void 0;
577
577
  /**
@@ -590,7 +590,7 @@ function __webpack_require__(moduleId) {
590
590
  var result = value + '';
591
591
  return '0' == result && 1 / value == -INFINITY ? '-0' : result;
592
592
  }
593
- /* harmony default export */ const _baseToString = baseToString;
593
+ /* ESM default export */ const _baseToString = baseToString;
594
594
  /**
595
595
  * Converts `value` to a string. An empty string is returned for `null`
596
596
  * and `undefined` values. The sign of `-0` is preserved.
@@ -614,7 +614,7 @@ function __webpack_require__(moduleId) {
614
614
  */ function toString_toString(value) {
615
615
  return null == value ? '' : _baseToString(value);
616
616
  }
617
- /* harmony default export */ const lodash_es_toString = toString_toString;
617
+ /* ESM default export */ const lodash_es_toString = toString_toString;
618
618
  /** Used to match Latin Unicode letters (excluding mathematical operators). */ var reLatin = /[\xc0-\xd6\xd8-\xf6\xf8-\xff\u0100-\u017f]/g;
619
619
  /** Used to compose unicode character classes. */ var rsComboMarksRange = '\\u0300-\\u036f', reComboHalfMarksRange = '\\ufe20-\\ufe2f', rsComboSymbolsRange = '\\u20d0-\\u20ff', rsComboRange = rsComboMarksRange + reComboHalfMarksRange + rsComboSymbolsRange;
620
620
  /** Used to compose unicode capture groups. */ var rsCombo = '[' + rsComboRange + ']';
@@ -643,7 +643,7 @@ function __webpack_require__(moduleId) {
643
643
  string = lodash_es_toString(string);
644
644
  return string && string.replace(reLatin, _deburrLetter).replace(reComboMark, '');
645
645
  }
646
- /* harmony default export */ const deburr = deburr_deburr;
646
+ /* ESM default export */ const deburr = deburr_deburr;
647
647
  /** Used to match words composed of alphanumeric characters. */ var reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;
648
648
  /**
649
649
  * Splits an ASCII `string` into an array of its words.
@@ -654,7 +654,7 @@ function __webpack_require__(moduleId) {
654
654
  */ function asciiWords(string) {
655
655
  return string.match(reAsciiWord) || [];
656
656
  }
657
- /* harmony default export */ const _asciiWords = asciiWords;
657
+ /* ESM default export */ const _asciiWords = asciiWords;
658
658
  /** Used to detect strings that need a more robust regexp to match words. */ var reHasUnicodeWord = /[a-z][A-Z]|[A-Z]{2}[a-z]|[0-9][a-zA-Z]|[a-zA-Z][0-9]|[^a-zA-Z0-9 ]/;
659
659
  /**
660
660
  * Checks if `string` contains a word composed of Unicode symbols.
@@ -665,7 +665,7 @@ function __webpack_require__(moduleId) {
665
665
  */ function hasUnicodeWord(string) {
666
666
  return reHasUnicodeWord.test(string);
667
667
  }
668
- /* harmony default export */ const _hasUnicodeWord = hasUnicodeWord;
668
+ /* ESM default export */ const _hasUnicodeWord = hasUnicodeWord;
669
669
  /** Used to compose unicode character classes. */ var rsAstralRange = '\\ud800-\\udfff', _unicodeWords_rsComboMarksRange = '\\u0300-\\u036f', _unicodeWords_reComboHalfMarksRange = '\\ufe20-\\ufe2f', _unicodeWords_rsComboSymbolsRange = '\\u20d0-\\u20ff', _unicodeWords_rsComboRange = _unicodeWords_rsComboMarksRange + _unicodeWords_reComboHalfMarksRange + _unicodeWords_rsComboSymbolsRange, rsDingbatRange = '\\u2700-\\u27bf', rsLowerRange = 'a-z\\xdf-\\xf6\\xf8-\\xff', rsMathOpRange = '\\xac\\xb1\\xd7\\xf7', rsNonCharRange = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf', rsPunctuationRange = '\\u2000-\\u206f', rsSpaceRange = ' \\t\\x0b\\f\\xa0\\ufeff\\n\\r\\u2028\\u2029\\u1680\\u180e\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200a\\u202f\\u205f\\u3000', rsUpperRange = 'A-Z\\xc0-\\xd6\\xd8-\\xde', rsVarRange = '\\ufe0e\\ufe0f', rsBreakRange = rsMathOpRange + rsNonCharRange + rsPunctuationRange + rsSpaceRange;
670
670
  /** Used to compose unicode capture groups. */ var rsApos = "['\u2019]", rsBreak = '[' + rsBreakRange + ']', _unicodeWords_rsCombo = '[' + _unicodeWords_rsComboRange + ']', rsDigits = '\\d+', rsDingbat = '[' + rsDingbatRange + ']', rsLower = '[' + rsLowerRange + ']', rsMisc = '[^' + rsAstralRange + rsBreakRange + rsDigits + rsDingbatRange + rsLowerRange + rsUpperRange + ']', rsFitz = '\\ud83c[\\udffb-\\udfff]', rsModifier = '(?:' + _unicodeWords_rsCombo + '|' + rsFitz + ')', rsNonAstral = '[^' + rsAstralRange + ']', rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}', rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]', rsUpper = '[' + rsUpperRange + ']', rsZWJ = '\\u200d';
671
671
  /** Used to compose unicode regexes. */ var rsMiscLower = '(?:' + rsLower + '|' + rsMisc + ')', rsMiscUpper = '(?:' + rsUpper + '|' + rsMisc + ')', rsOptContrLower = '(?:' + rsApos + '(?:d|ll|m|re|s|t|ve))?', rsOptContrUpper = '(?:' + rsApos + '(?:D|LL|M|RE|S|T|VE))?', reOptMod = rsModifier + '?', rsOptVar = '[' + rsVarRange + ']?', rsOptJoin = '(?:' + rsZWJ + '(?:' + [
@@ -704,7 +704,7 @@ function __webpack_require__(moduleId) {
704
704
  */ function unicodeWords(string) {
705
705
  return string.match(reUnicodeWord) || [];
706
706
  }
707
- /* harmony default export */ const _unicodeWords = unicodeWords;
707
+ /* ESM default export */ const _unicodeWords = unicodeWords;
708
708
  /**
709
709
  * Splits `string` into an array of its words.
710
710
  *
@@ -729,7 +729,7 @@ function __webpack_require__(moduleId) {
729
729
  if (void 0 === pattern) return _hasUnicodeWord(string) ? _unicodeWords(string) : _asciiWords(string);
730
730
  return string.match(pattern) || [];
731
731
  }
732
- /* harmony default export */ const words = words_words;
732
+ /* ESM default export */ const words = words_words;
733
733
  /** Used to compose unicode capture groups. */ var _createCompounder_rsApos = "['\u2019]";
734
734
  /** Used to match apostrophes. */ var reApos = RegExp(_createCompounder_rsApos, 'g');
735
735
  /**
@@ -743,7 +743,7 @@ function __webpack_require__(moduleId) {
743
743
  return _arrayReduce(words(deburr(string).replace(reApos, '')), callback, '');
744
744
  };
745
745
  }
746
- /* harmony default export */ const _createCompounder = createCompounder;
746
+ /* ESM default export */ const _createCompounder = createCompounder;
747
747
  /**
748
748
  * Converts `string` to
749
749
  * [snake case](https://en.wikipedia.org/wiki/Snake_case).
@@ -767,7 +767,7 @@ function __webpack_require__(moduleId) {
767
767
  */ var snakeCase_snakeCase = _createCompounder(function(result, word, index) {
768
768
  return result + (index ? '_' : '') + word.toLowerCase();
769
769
  });
770
- /* harmony default export */ const snakeCase = snakeCase_snakeCase;
770
+ /* ESM default export */ const snakeCase = snakeCase_snakeCase;
771
771
  const external_node_fs_namespaceObject = require("node:fs");
772
772
  var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
773
773
  const external_node_os_namespaceObject = require("node:os");
@@ -794,7 +794,7 @@ function __webpack_require__(moduleId) {
794
794
  string = lodash_es_toString(string);
795
795
  return string && reHasRegExpChar.test(string) ? string.replace(reRegExpChar, '\\$&') : string;
796
796
  }
797
- /* harmony default export */ const lodash_es_escapeRegExp = escapeRegExp;
797
+ /* ESM default export */ const lodash_es_escapeRegExp = escapeRegExp;
798
798
  function getRealTemporaryDirectory() {
799
799
  let ret = null;
800
800
  try {
@@ -875,7 +875,7 @@ function __webpack_require__(moduleId) {
875
875
  // modified based on https://github.com/webpack/webpack/blob/001cab14692eb9a833c6b56709edbab547e291a1/test/StatsTestCases.basictest.js#L199
876
876
  .replace(/[0-9]+(\.[0-9]+)*(<\/CLR>)?(\s?s)/g, 'X$2$3');
877
877
  function createSnapshotSerializer(options) {
878
- const { root = process.cwd(), workspace = '', replace: customMatchers = [], replacePost: customPostMatchers = [], features = {} } = options || {};
878
+ const { root = process.cwd(), workspace = '', replace: customMatchers = [], replacePost: customPostMatchers = [], features = {}, beforeSerialize, afterSerialize } = options || {};
879
879
  const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, replaceHomeDir = true, addDoubleQuotes = true, transformWin32Path = true, escapeDoubleQuotes = true, escapeEOL = true, transformCLR = true } = features;
880
880
  function createPathMatchers() {
881
881
  const pathMatchers = [];
@@ -903,20 +903,23 @@ function __webpack_require__(moduleId) {
903
903
  },
904
904
  serialize (val) {
905
905
  let replaced = val;
906
+ if (beforeSerialize) replaced = beforeSerialize(replaced);
906
907
  if (transformWin32Path) replaced = normalizeCodeToPosix(replaced);
907
908
  replaced = applyMatcherReplacement(pathMatchers, replaced);
908
909
  if (transformCLR) replaced = normalizeCLR(replaced);
909
910
  if (escapeDoubleQuotes) replaced = replaced.replace(/"/g, '\\"');
910
911
  if (escapeEOL) replaced = replaced.replace(/\\r\\n/g, '\\n');
911
912
  if (addDoubleQuotes) replaced = `"${replaced}"`;
913
+ if (afterSerialize) replaced = afterSerialize(replaced);
912
914
  return replaced;
913
915
  }
914
916
  };
915
917
  return serializer;
916
918
  }
919
+ // export types related to `createSnapshotSerializer`
917
920
  })();
918
921
  var __webpack_export_target__ = exports;
919
- for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
922
+ for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
920
923
  if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
921
924
  value: true
922
925
  });
@@ -1,6 +1,6 @@
1
1
  export declare function createSnapshotSerializer(options?: SnapshotSerializerOptions): SnapshotSerializer;
2
2
 
3
- declare interface Features {
3
+ export declare interface Features {
4
4
  /**
5
5
  * /foo/node_modules/.pnpm -> <ROOT>/node_modules/.pnpm
6
6
  * @default true
@@ -18,10 +18,12 @@ declare interface Features {
18
18
  */
19
19
  replacePnpmInner?: boolean;
20
20
  /**
21
+ * <TEMP>
21
22
  * @default true
22
23
  */
23
24
  replaceTmpDir?: boolean;
24
25
  /**
26
+ * <HOME>
25
27
  * @default true
26
28
  */
27
29
  replaceHomeDir?: boolean;
@@ -37,6 +39,7 @@ declare interface Features {
37
39
  */
38
40
  transformWin32Path?: boolean;
39
41
  /**
42
+ * "" -> \"\"
40
43
  * @default true
41
44
  */
42
45
  escapeDoubleQuotes?: boolean;
@@ -53,19 +56,19 @@ declare interface Features {
53
56
  transformCLR?: boolean;
54
57
  }
55
58
 
56
- declare interface PathMatcher {
59
+ export declare interface PathMatcher {
57
60
  match: PathMatchExpression;
58
61
  mark: string | ((substring: string, ...args: any[]) => string);
59
62
  }
60
63
 
61
64
  declare type PathMatchExpression = string | RegExp;
62
65
 
63
- declare interface SnapshotSerializer {
66
+ export declare interface SnapshotSerializer {
64
67
  serialize: (val: any) => string;
65
68
  test: (arg0: any) => boolean;
66
69
  }
67
70
 
68
- declare interface SnapshotSerializerOptions {
71
+ export declare interface SnapshotSerializerOptions {
69
72
  /**
70
73
  * repository root path
71
74
  * @example '/Users/foo/codes/rsbuild/node_modules/.pnpm' -> '<ROOT>/node_modules/.pnpm'
@@ -79,13 +82,21 @@ declare interface SnapshotSerializerOptions {
79
82
  */
80
83
  workspace?: string;
81
84
  /**
82
- * @description replace -> workspace root pnpmInner temp home -> replacePost
85
+ * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
86
+ */
87
+ beforeSerialize?: (val: string) => string;
88
+ /**
89
+ * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
83
90
  */
84
91
  replace?: PathMatcher[];
85
92
  /**
86
- * @description replace -> workspace root pnpmInner temp home -> replacePost
93
+ * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
87
94
  */
88
95
  replacePost?: PathMatcher[];
96
+ /**
97
+ * @description beforeSerialize -> replace -> workspace root pnpmInner temp home -> replacePost -> afterSerialize
98
+ */
99
+ afterSerialize?: (val: string) => string;
89
100
  features?: Features;
90
101
  }
91
102
 
@@ -142,7 +142,7 @@ function __webpack_require__(moduleId) {
142
142
  }
143
143
  /************************************************************************/ // webpack/runtime/compat_get_default_export
144
144
  (()=>{
145
- // getDefaultExport function for compatibility with non-harmony modules
145
+ // getDefaultExport function for compatibility with non-ESM modules
146
146
  __webpack_require__.n = function(module) {
147
147
  var getter = module && module.__esModule ? function() {
148
148
  return module['default'];
@@ -187,7 +187,7 @@ function __webpack_require__(moduleId) {
187
187
  while(++index < length)accumulator = iteratee(accumulator, array[index], index, array);
188
188
  return accumulator;
189
189
  }
190
- /* harmony default export */ const _arrayReduce = arrayReduce;
190
+ /* ESM default export */ const _arrayReduce = arrayReduce;
191
191
  /**
192
192
  * The base implementation of `_.propertyOf` without support for deep paths.
193
193
  *
@@ -199,7 +199,7 @@ function __webpack_require__(moduleId) {
199
199
  return null == object ? void 0 : object[key];
200
200
  };
201
201
  }
202
- /* harmony default export */ const _basePropertyOf = basePropertyOf;
202
+ /* ESM default export */ const _basePropertyOf = basePropertyOf;
203
203
  /** Used to map Latin Unicode letters to basic Latin letters. */ var deburredLetters = {
204
204
  // Latin-1 Supplement block.
205
205
  À: 'A',
@@ -402,14 +402,14 @@ function __webpack_require__(moduleId) {
402
402
  * @param {string} letter The matched letter to deburr.
403
403
  * @returns {string} Returns the deburred letter.
404
404
  */ var deburrLetter = _basePropertyOf(deburredLetters);
405
- /* harmony default export */ const _deburrLetter = deburrLetter;
405
+ /* ESM default export */ const _deburrLetter = deburrLetter;
406
406
  /** Detect free variable `global` from Node.js. */ var freeGlobal = 'object' == typeof global && global && global.Object === Object && global;
407
- /* harmony default export */ const _freeGlobal = freeGlobal;
407
+ /* ESM default export */ const _freeGlobal = freeGlobal;
408
408
  /** Detect free variable `self`. */ var freeSelf = 'object' == typeof self && self && self.Object === Object && self;
409
409
  /** Used as a reference to the global object. */ var _root_root = _freeGlobal || freeSelf || Function('return this')();
410
- /* harmony default export */ const _root = _root_root;
410
+ /* ESM default export */ const _root = _root_root;
411
411
  /** Built-in value references. */ var Symbol = _root.Symbol;
412
- /* harmony default export */ const _Symbol = Symbol;
412
+ /* ESM default export */ const _Symbol = Symbol;
413
413
  /**
414
414
  * A specialized version of `_.map` for arrays without support for iteratee
415
415
  * shorthands.
@@ -423,7 +423,7 @@ function __webpack_require__(moduleId) {
423
423
  while(++index < length)result[index] = iteratee(array[index], index, array);
424
424
  return result;
425
425
  }
426
- /* harmony default export */ const _arrayMap = arrayMap;
426
+ /* ESM default export */ const _arrayMap = arrayMap;
427
427
  /**
428
428
  * Checks if `value` is classified as an `Array` object.
429
429
  *
@@ -447,7 +447,7 @@ function __webpack_require__(moduleId) {
447
447
  * _.isArray(_.noop);
448
448
  * // => false
449
449
  */ var isArray_isArray = Array.isArray;
450
- /* harmony default export */ const isArray = isArray_isArray;
450
+ /* ESM default export */ const isArray = isArray_isArray;
451
451
  /** Used for built-in method references. */ var objectProto = Object.prototype;
452
452
  /** Used to check objects for own properties. */ var _getRawTag_hasOwnProperty = objectProto.hasOwnProperty;
453
453
  /**
@@ -475,7 +475,7 @@ function __webpack_require__(moduleId) {
475
475
  }
476
476
  return result;
477
477
  }
478
- /* harmony default export */ const _getRawTag = getRawTag;
478
+ /* ESM default export */ const _getRawTag = getRawTag;
479
479
  /** Used for built-in method references. */ var _objectToString_objectProto = Object.prototype;
480
480
  /**
481
481
  * Used to resolve the
@@ -491,7 +491,7 @@ function __webpack_require__(moduleId) {
491
491
  */ function objectToString(value) {
492
492
  return _objectToString_nativeObjectToString.call(value);
493
493
  }
494
- /* harmony default export */ const _objectToString = objectToString;
494
+ /* ESM default export */ const _objectToString = objectToString;
495
495
  /** `Object#toString` result references. */ var nullTag = '[object Null]', undefinedTag = '[object Undefined]';
496
496
  /** Built-in value references. */ var _baseGetTag_symToStringTag = _Symbol ? _Symbol.toStringTag : void 0;
497
497
  /**
@@ -504,7 +504,7 @@ function __webpack_require__(moduleId) {
504
504
  if (null == value) return void 0 === value ? undefinedTag : nullTag;
505
505
  return _baseGetTag_symToStringTag && _baseGetTag_symToStringTag in Object(value) ? _getRawTag(value) : _objectToString(value);
506
506
  }
507
- /* harmony default export */ const _baseGetTag = baseGetTag;
507
+ /* ESM default export */ const _baseGetTag = baseGetTag;
508
508
  /**
509
509
  * Checks if `value` is object-like. A value is object-like if it's not `null`
510
510
  * and has a `typeof` result of "object".
@@ -531,7 +531,7 @@ function __webpack_require__(moduleId) {
531
531
  */ function isObjectLike(value) {
532
532
  return null != value && 'object' == typeof value;
533
533
  }
534
- /* harmony default export */ const lodash_es_isObjectLike = isObjectLike;
534
+ /* ESM default export */ const lodash_es_isObjectLike = isObjectLike;
535
535
  /** `Object#toString` result references. */ var symbolTag = '[object Symbol]';
536
536
  /**
537
537
  * Checks if `value` is classified as a `Symbol` primitive or object.
@@ -552,7 +552,7 @@ function __webpack_require__(moduleId) {
552
552
  */ function isSymbol_isSymbol(value) {
553
553
  return 'symbol' == typeof value || lodash_es_isObjectLike(value) && _baseGetTag(value) == symbolTag;
554
554
  }
555
- /* harmony default export */ const isSymbol = isSymbol_isSymbol;
555
+ /* ESM default export */ const isSymbol = isSymbol_isSymbol;
556
556
  /** Used as references for various `Number` constants. */ var INFINITY = 1 / 0;
557
557
  /** Used to convert symbols to primitives and strings. */ var symbolProto = _Symbol ? _Symbol.prototype : void 0, symbolToString = symbolProto ? symbolProto.toString : void 0;
558
558
  /**
@@ -571,7 +571,7 @@ function __webpack_require__(moduleId) {
571
571
  var result = value + '';
572
572
  return '0' == result && 1 / value == -INFINITY ? '-0' : result;
573
573
  }
574
- /* harmony default export */ const _baseToString = baseToString;
574
+ /* ESM default export */ const _baseToString = baseToString;
575
575
  /**
576
576
  * Converts `value` to a string. An empty string is returned for `null`
577
577
  * and `undefined` values. The sign of `-0` is preserved.
@@ -595,7 +595,7 @@ function __webpack_require__(moduleId) {
595
595
  */ function toString_toString(value) {
596
596
  return null == value ? '' : _baseToString(value);
597
597
  }
598
- /* harmony default export */ const lodash_es_toString = toString_toString;
598
+ /* ESM default export */ const lodash_es_toString = toString_toString;
599
599
  /** Used to match Latin Unicode letters (excluding mathematical operators). */ var reLatin = /[\xc0-\xd6\xd8-\xf6\xf8-\xff\u0100-\u017f]/g;
600
600
  /** Used to compose unicode character classes. */ var rsComboMarksRange = '\\u0300-\\u036f', reComboHalfMarksRange = '\\ufe20-\\ufe2f', rsComboSymbolsRange = '\\u20d0-\\u20ff', rsComboRange = rsComboMarksRange + reComboHalfMarksRange + rsComboSymbolsRange;
601
601
  /** Used to compose unicode capture groups. */ var rsCombo = '[' + rsComboRange + ']';
@@ -624,7 +624,7 @@ function __webpack_require__(moduleId) {
624
624
  string = lodash_es_toString(string);
625
625
  return string && string.replace(reLatin, _deburrLetter).replace(reComboMark, '');
626
626
  }
627
- /* harmony default export */ const deburr = deburr_deburr;
627
+ /* ESM default export */ const deburr = deburr_deburr;
628
628
  /** Used to match words composed of alphanumeric characters. */ var reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;
629
629
  /**
630
630
  * Splits an ASCII `string` into an array of its words.
@@ -635,7 +635,7 @@ function __webpack_require__(moduleId) {
635
635
  */ function asciiWords(string) {
636
636
  return string.match(reAsciiWord) || [];
637
637
  }
638
- /* harmony default export */ const _asciiWords = asciiWords;
638
+ /* ESM default export */ const _asciiWords = asciiWords;
639
639
  /** Used to detect strings that need a more robust regexp to match words. */ var reHasUnicodeWord = /[a-z][A-Z]|[A-Z]{2}[a-z]|[0-9][a-zA-Z]|[a-zA-Z][0-9]|[^a-zA-Z0-9 ]/;
640
640
  /**
641
641
  * Checks if `string` contains a word composed of Unicode symbols.
@@ -646,7 +646,7 @@ function __webpack_require__(moduleId) {
646
646
  */ function hasUnicodeWord(string) {
647
647
  return reHasUnicodeWord.test(string);
648
648
  }
649
- /* harmony default export */ const _hasUnicodeWord = hasUnicodeWord;
649
+ /* ESM default export */ const _hasUnicodeWord = hasUnicodeWord;
650
650
  /** Used to compose unicode character classes. */ var rsAstralRange = '\\ud800-\\udfff', _unicodeWords_rsComboMarksRange = '\\u0300-\\u036f', _unicodeWords_reComboHalfMarksRange = '\\ufe20-\\ufe2f', _unicodeWords_rsComboSymbolsRange = '\\u20d0-\\u20ff', _unicodeWords_rsComboRange = _unicodeWords_rsComboMarksRange + _unicodeWords_reComboHalfMarksRange + _unicodeWords_rsComboSymbolsRange, rsDingbatRange = '\\u2700-\\u27bf', rsLowerRange = 'a-z\\xdf-\\xf6\\xf8-\\xff', rsMathOpRange = '\\xac\\xb1\\xd7\\xf7', rsNonCharRange = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf', rsPunctuationRange = '\\u2000-\\u206f', rsSpaceRange = ' \\t\\x0b\\f\\xa0\\ufeff\\n\\r\\u2028\\u2029\\u1680\\u180e\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200a\\u202f\\u205f\\u3000', rsUpperRange = 'A-Z\\xc0-\\xd6\\xd8-\\xde', rsVarRange = '\\ufe0e\\ufe0f', rsBreakRange = rsMathOpRange + rsNonCharRange + rsPunctuationRange + rsSpaceRange;
651
651
  /** Used to compose unicode capture groups. */ var rsApos = "['\u2019]", rsBreak = '[' + rsBreakRange + ']', _unicodeWords_rsCombo = '[' + _unicodeWords_rsComboRange + ']', rsDigits = '\\d+', rsDingbat = '[' + rsDingbatRange + ']', rsLower = '[' + rsLowerRange + ']', rsMisc = '[^' + rsAstralRange + rsBreakRange + rsDigits + rsDingbatRange + rsLowerRange + rsUpperRange + ']', rsFitz = '\\ud83c[\\udffb-\\udfff]', rsModifier = '(?:' + _unicodeWords_rsCombo + '|' + rsFitz + ')', rsNonAstral = '[^' + rsAstralRange + ']', rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}', rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]', rsUpper = '[' + rsUpperRange + ']', rsZWJ = '\\u200d';
652
652
  /** Used to compose unicode regexes. */ var rsMiscLower = '(?:' + rsLower + '|' + rsMisc + ')', rsMiscUpper = '(?:' + rsUpper + '|' + rsMisc + ')', rsOptContrLower = '(?:' + rsApos + '(?:d|ll|m|re|s|t|ve))?', rsOptContrUpper = '(?:' + rsApos + '(?:D|LL|M|RE|S|T|VE))?', reOptMod = rsModifier + '?', rsOptVar = '[' + rsVarRange + ']?', rsOptJoin = '(?:' + rsZWJ + '(?:' + [
@@ -685,7 +685,7 @@ function __webpack_require__(moduleId) {
685
685
  */ function unicodeWords(string) {
686
686
  return string.match(reUnicodeWord) || [];
687
687
  }
688
- /* harmony default export */ const _unicodeWords = unicodeWords;
688
+ /* ESM default export */ const _unicodeWords = unicodeWords;
689
689
  /**
690
690
  * Splits `string` into an array of its words.
691
691
  *
@@ -710,7 +710,7 @@ function __webpack_require__(moduleId) {
710
710
  if (void 0 === pattern) return _hasUnicodeWord(string) ? _unicodeWords(string) : _asciiWords(string);
711
711
  return string.match(pattern) || [];
712
712
  }
713
- /* harmony default export */ const words = words_words;
713
+ /* ESM default export */ const words = words_words;
714
714
  /** Used to compose unicode capture groups. */ var _createCompounder_rsApos = "['\u2019]";
715
715
  /** Used to match apostrophes. */ var reApos = RegExp(_createCompounder_rsApos, 'g');
716
716
  /**
@@ -724,7 +724,7 @@ function __webpack_require__(moduleId) {
724
724
  return _arrayReduce(words(deburr(string).replace(reApos, '')), callback, '');
725
725
  };
726
726
  }
727
- /* harmony default export */ const _createCompounder = createCompounder;
727
+ /* ESM default export */ const _createCompounder = createCompounder;
728
728
  /**
729
729
  * Converts `string` to
730
730
  * [snake case](https://en.wikipedia.org/wiki/Snake_case).
@@ -748,7 +748,7 @@ function __webpack_require__(moduleId) {
748
748
  */ var snakeCase_snakeCase = _createCompounder(function(result, word, index) {
749
749
  return result + (index ? '_' : '') + word.toLowerCase();
750
750
  });
751
- /* harmony default export */ const snakeCase = snakeCase_snakeCase;
751
+ /* ESM default export */ const snakeCase = snakeCase_snakeCase;
752
752
  /**
753
753
  * Used to match `RegExp`
754
754
  * [syntax characters](http://ecma-international.org/ecma-262/7.0/#sec-patterns).
@@ -771,7 +771,7 @@ function __webpack_require__(moduleId) {
771
771
  string = lodash_es_toString(string);
772
772
  return string && reHasRegExpChar.test(string) ? string.replace(reRegExpChar, '\\$&') : string;
773
773
  }
774
- /* harmony default export */ const lodash_es_escapeRegExp = escapeRegExp;
774
+ /* ESM default export */ const lodash_es_escapeRegExp = escapeRegExp;
775
775
  function getRealTemporaryDirectory() {
776
776
  let ret = null;
777
777
  try {
@@ -850,7 +850,7 @@ const normalizeCLR = (str)=>str.replace(/\u001b\[1m\u001b\[([0-9;]*)m/g, '<CLR=$
850
850
  // modified based on https://github.com/webpack/webpack/blob/001cab14692eb9a833c6b56709edbab547e291a1/test/StatsTestCases.basictest.js#L199
851
851
  .replace(/[0-9]+(\.[0-9]+)*(<\/CLR>)?(\s?s)/g, 'X$2$3');
852
852
  function createSnapshotSerializer(options) {
853
- const { root = process.cwd(), workspace = '', replace: customMatchers = [], replacePost: customPostMatchers = [], features = {} } = options || {};
853
+ const { root = process.cwd(), workspace = '', replace: customMatchers = [], replacePost: customPostMatchers = [], features = {}, beforeSerialize, afterSerialize } = options || {};
854
854
  const { replaceRoot = true, replaceWorkspace = true, replacePnpmInner = true, replaceTmpDir = true, replaceHomeDir = true, addDoubleQuotes = true, transformWin32Path = true, escapeDoubleQuotes = true, escapeEOL = true, transformCLR = true } = features;
855
855
  function createPathMatchers() {
856
856
  const pathMatchers = [];
@@ -878,12 +878,14 @@ function createSnapshotSerializer(options) {
878
878
  },
879
879
  serialize (val) {
880
880
  let replaced = val;
881
+ if (beforeSerialize) replaced = beforeSerialize(replaced);
881
882
  if (transformWin32Path) replaced = normalizeCodeToPosix(replaced);
882
883
  replaced = applyMatcherReplacement(pathMatchers, replaced);
883
884
  if (transformCLR) replaced = normalizeCLR(replaced);
884
885
  if (escapeDoubleQuotes) replaced = replaced.replace(/"/g, '\\"');
885
886
  if (escapeEOL) replaced = replaced.replace(/\\r\\n/g, '\\n');
886
887
  if (addDoubleQuotes) replaced = `"${replaced}"`;
888
+ if (afterSerialize) replaced = afterSerialize(replaced);
887
889
  return replaced;
888
890
  }
889
891
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "path-serializer",
3
- "version": "0.3.4",
3
+ "version": "0.4.0",
4
4
  "description": "path-serializer",
5
5
  "keywords": [
6
6
  "snapshot",
@@ -35,19 +35,19 @@
35
35
  "pre-commit": "npx nano-staged"
36
36
  },
37
37
  "devDependencies": {
38
- "vitest": "^2.0.5",
39
- "@biomejs/biome": "^1.8.3",
40
- "@microsoft/api-extractor": "^7.47.5",
41
- "@rslib/core": "0.0.12",
38
+ "vitest": "^2.1.6",
39
+ "@biomejs/biome": "^1.9.4",
40
+ "@microsoft/api-extractor": "^7.48.0",
41
+ "@rslib/core": "0.1.1",
42
42
  "@types/lodash-es": "^4.17.12",
43
- "@types/node": "^22.1.0",
43
+ "@types/node": "^22.10.1",
44
44
  "lodash-es": "^4.17.21",
45
45
  "nano-staged": "^0.8.0",
46
46
  "simple-git-hooks": "^2.11.1",
47
47
  "slash": "5.1.0",
48
- "typescript": "^5.6.3",
48
+ "typescript": "^5.7.2",
49
49
  "upath": "^2.0.1",
50
- "execa": "9.3.1",
50
+ "execa": "9.5.1",
51
51
  "fs-extra": "11.2.0",
52
52
  "semver": "7.6.3"
53
53
  },