Skip to content

Commit

Permalink
fix(gatsby-plugin-sharp): sort nested options (#15459)
Browse files Browse the repository at this point in the history
* fixed arg digest sorting

* reveresed key order

* fixed test
  • Loading branch information
Annnnnieee authored and pieh committed Jul 6, 2019
1 parent e94ca3e commit 6ea8c29
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
22 changes: 21 additions & 1 deletion packages/gatsby-plugin-sharp/src/__tests__/process-file.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createArgsDigest } = require(`../process-file`)
const { createArgsDigest, sortKeys } = require(`../process-file`)

describe(`createArgsDigest`, () => {
const defaultArgsBaseline = {
Expand Down Expand Up @@ -94,5 +94,25 @@ describe(`createArgsDigest`, () => {
testHashEqual(`maxWidth`, { maxWidth: 500 })
testHashEqual(`base64`, { base64: true })
})

describe(`argument sorting`, () => {
it(`sorts nested arguments`, () => {
const args = {
duotone: {
shadow: `#10c5f8`,
highlight: `#32CD32`,
},
cropFocus: 17,
}
const actual = sortKeys(args)
expect(actual).toEqual({
cropFocus: 17,
duotone: {
highlight: `#32CD32`,
shadow: `#10c5f8`,
},
})
})
})
})
})
21 changes: 20 additions & 1 deletion packages/gatsby-plugin-sharp/src/process-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,29 @@ exports.createArgsDigest = args => {

const argsDigest = crypto
.createHash(`md5`)
.update(JSON.stringify(filtered, Object.keys(filtered).sort()))
.update(JSON.stringify(sortKeys(filtered)))
.digest(`hex`)

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

return argsDigestShort
}

const sortKeys = object => {
var sortedObj = {},
keys = _.keys(object)

keys = _.sortBy(keys, key => key)

_.each(keys, key => {
if (typeof object[key] == `object` && !(object[key] instanceof Array)) {
sortedObj[key] = sortKeys(object[key])
} else {
sortedObj[key] = object[key]
}
})

return sortedObj
}

exports.sortKeys = sortKeys

0 comments on commit 6ea8c29

Please sign in to comment.