Skip to content

Commit 6ea8c29

Browse files
Annnnnieeepieh
authored andcommitted
fix(gatsby-plugin-sharp): sort nested options (#15459)
* fixed arg digest sorting * reveresed key order * fixed test
1 parent e94ca3e commit 6ea8c29

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

packages/gatsby-plugin-sharp/src/__tests__/process-file.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { createArgsDigest } = require(`../process-file`)
1+
const { createArgsDigest, sortKeys } = require(`../process-file`)
22

33
describe(`createArgsDigest`, () => {
44
const defaultArgsBaseline = {
@@ -94,5 +94,25 @@ describe(`createArgsDigest`, () => {
9494
testHashEqual(`maxWidth`, { maxWidth: 500 })
9595
testHashEqual(`base64`, { base64: true })
9696
})
97+
98+
describe(`argument sorting`, () => {
99+
it(`sorts nested arguments`, () => {
100+
const args = {
101+
duotone: {
102+
shadow: `#10c5f8`,
103+
highlight: `#32CD32`,
104+
},
105+
cropFocus: 17,
106+
}
107+
const actual = sortKeys(args)
108+
expect(actual).toEqual({
109+
cropFocus: 17,
110+
duotone: {
111+
highlight: `#32CD32`,
112+
shadow: `#10c5f8`,
113+
},
114+
})
115+
})
116+
})
97117
})
98118
})

packages/gatsby-plugin-sharp/src/process-file.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,29 @@ exports.createArgsDigest = args => {
241241

242242
const argsDigest = crypto
243243
.createHash(`md5`)
244-
.update(JSON.stringify(filtered, Object.keys(filtered).sort()))
244+
.update(JSON.stringify(sortKeys(filtered)))
245245
.digest(`hex`)
246246

247247
const argsDigestShort = argsDigest.substr(argsDigest.length - 5)
248248

249249
return argsDigestShort
250250
}
251+
252+
const sortKeys = object => {
253+
var sortedObj = {},
254+
keys = _.keys(object)
255+
256+
keys = _.sortBy(keys, key => key)
257+
258+
_.each(keys, key => {
259+
if (typeof object[key] == `object` && !(object[key] instanceof Array)) {
260+
sortedObj[key] = sortKeys(object[key])
261+
} else {
262+
sortedObj[key] = object[key]
263+
}
264+
})
265+
266+
return sortedObj
267+
}
268+
269+
exports.sortKeys = sortKeys

0 commit comments

Comments
 (0)