MCPcopy
hub / github.com/webpack/webpack / mapAndDeduplicateSourceBuffers

Function mapAndDeduplicateSourceBuffers

lib/optimize/RealContentHashPlugin.js:90–123  ·  lib/optimize/RealContentHashPlugin.js::mapAndDeduplicateSourceBuffers
(input, fn)

Source from the content-addressed store, hash-verified

88 * @returns {Buffer[][]} unique chunk arrays
89 */
90const mapAndDeduplicateSourceBuffers = (input, fn) => {
91 /** @type {Map<number, Buffer[][]>} */
92 const bySize = new Map();
93 /** @type {Buffer[][]} */
94 const result = [];
95 for (const value of input) {
96 const source = fn(value);
97 class="cm">// TODO webpack 6: drop the `buffers` check, require webpack-sources >= 3.4
98 class="cm">// and call `source.buffers()` unconditionally.
99 const chunks =
100 class="cm">// TODO remove in webpack 6, this is protection against authors who directly use `webpack-sources` outdated version
101 typeof source.buffers === class="st">"function"
102 ? source.buffers()
103 : [source.buffer()];
104 let total = 0;
105 for (const c of chunks) total += c.length;
106 const sameSize = bySize.get(total);
107 if (sameSize) {
108 let duplicate = false;
109 for (const other of sameSize) {
110 if (bufferArraysEqual(chunks, other)) {
111 duplicate = true;
112 break;
113 }
114 }
115 if (duplicate) continue;
116 sameSize.push(chunks);
117 } else {
118 bySize.set(total, [chunks]);
119 }
120 result.push(chunks);
121 }
122 return result;
123};
124
125/**
126 * Escapes regular expression metacharacters

Callers 1

applyMethod · 0.85

Calls 5

bufferArraysEqualFunction · 0.85
fnFunction · 0.70
getMethod · 0.45
pushMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected