1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
9 |
|
---|
10 | /** @type {WeakMap<Source, WeakMap<Source, boolean>>} */
|
---|
11 | const equalityCache = new WeakMap();
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * @param {Source} a a source
|
---|
15 | * @param {Source} b another source
|
---|
16 | * @returns {boolean} true, when both sources are equal
|
---|
17 | */
|
---|
18 | const _isSourceEqual = (a, b) => {
|
---|
19 | // prefer .buffer(), it's called anyway during emit
|
---|
20 | /** @type {Buffer|string} */
|
---|
21 | let aSource = typeof a.buffer === "function" ? a.buffer() : a.source();
|
---|
22 | /** @type {Buffer|string} */
|
---|
23 | let bSource = typeof b.buffer === "function" ? b.buffer() : b.source();
|
---|
24 | if (aSource === bSource) return true;
|
---|
25 | if (typeof aSource === "string" && typeof bSource === "string") return false;
|
---|
26 | if (!Buffer.isBuffer(aSource)) aSource = Buffer.from(aSource, "utf-8");
|
---|
27 | if (!Buffer.isBuffer(bSource)) bSource = Buffer.from(bSource, "utf-8");
|
---|
28 | return aSource.equals(bSource);
|
---|
29 | };
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * @param {Source} a a source
|
---|
33 | * @param {Source} b another source
|
---|
34 | * @returns {boolean} true, when both sources are equal
|
---|
35 | */
|
---|
36 | const isSourceEqual = (a, b) => {
|
---|
37 | if (a === b) return true;
|
---|
38 | const cache1 = equalityCache.get(a);
|
---|
39 | if (cache1 !== undefined) {
|
---|
40 | const result = cache1.get(b);
|
---|
41 | if (result !== undefined) return result;
|
---|
42 | }
|
---|
43 | const result = _isSourceEqual(a, b);
|
---|
44 | if (cache1 !== undefined) {
|
---|
45 | cache1.set(b, result);
|
---|
46 | } else {
|
---|
47 | const map = new WeakMap();
|
---|
48 | map.set(b, result);
|
---|
49 | equalityCache.set(a, map);
|
---|
50 | }
|
---|
51 | const cache2 = equalityCache.get(b);
|
---|
52 | if (cache2 !== undefined) {
|
---|
53 | cache2.set(a, result);
|
---|
54 | } else {
|
---|
55 | const map = new WeakMap();
|
---|
56 | map.set(a, result);
|
---|
57 | equalityCache.set(b, map);
|
---|
58 | }
|
---|
59 | return result;
|
---|
60 | };
|
---|
61 | exports.isSourceEqual = isSourceEqual;
|
---|