Skip to content

Commit 86b5859

Browse files
authored
Merge pull request webpack#8680 from wtgtybhertgeghgtwtg/fast-stable-stringify
Replace `json-stable-stringify` with `fast-json-stable-stringify`.
2 parents 49c5400 + 1da2c42 commit 86b5859

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

lib/Compiler.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"use strict";
77

88
const parseJson = require("json-parse-better-errors");
9-
const stringify = require("json-stable-stringify");
109
const asyncLib = require("neo-async");
1110
const path = require("path");
1211
const {
@@ -58,6 +57,29 @@ const { makePathsRelative } = require("./util/identifier");
5857
* @param {Compilation=} compilation
5958
*/
6059

60+
/**
61+
* @param {string[]} array an array
62+
* @returns {boolean} true, if the array is sorted
63+
*/
64+
const isSorted = array => {
65+
for (let i = 1; i < array.length; i++) {
66+
if (array[i - 1] > array[i]) return false;
67+
}
68+
return true;
69+
};
70+
71+
/**
72+
* @param {Object} obj an object
73+
* @param {string[]} keys the keys of the object
74+
* @returns {Object} the object with properties sorted by property name
75+
*/
76+
const sortObject = (obj, keys) => {
77+
return keys.sort().reduce((o, k) => {
78+
o[k] = obj[k];
79+
return o;
80+
}, {});
81+
};
82+
6183
class Compiler {
6284
/**
6385
* @param {string} context the compilation path
@@ -448,7 +470,23 @@ class Compiler {
448470
const writeFile = () => {
449471
this.outputFileSystem.writeFile(
450472
this.recordsOutputPath,
451-
stringify(this.records, { space: 2 }),
473+
JSON.stringify(
474+
this.records,
475+
(n, value) => {
476+
if (
477+
typeof value === "object" &&
478+
value !== null &&
479+
!Array.isArray(value)
480+
) {
481+
const keys = Object.keys(value);
482+
if (!isSorted(keys)) {
483+
return sortObject(value, keys);
484+
}
485+
}
486+
return value;
487+
},
488+
2
489+
),
452490
callback
453491
);
454492
};

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"eslint-scope": "^4.0.0",
1919
"events": "^3.0.0",
2020
"json-parse-better-errors": "^1.0.2",
21-
"json-stable-stringify": "^1.0.1",
2221
"loader-runner": "3.0.0",
2322
"loader-utils": "^1.1.0",
2423
"memory-fs": "~0.4.1",

test/__snapshots__/ConfigTestCases.test.js.snap

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ exports[`ConfigTestCases records issue-2991 exported tests should write relative
66
\\"byName\\": {
77
\\"main\\": 404
88
},
9-
\\"bySource\\": {
10-
},
9+
\\"bySource\\": {},
1110
\\"usedIds\\": [
1211
404
1312
]
@@ -35,8 +34,7 @@ exports[`ConfigTestCases records issue-7339 exported tests should write relative
3534
\\"byName\\": {
3635
\\"main\\": 404
3736
},
38-
\\"bySource\\": {
39-
},
37+
\\"bySource\\": {},
4038
\\"usedIds\\": [
4139
404
4240
]

0 commit comments

Comments
 (0)