diff --git a/react-packager/src/DependencyResolver/Module.js b/react-packager/src/DependencyResolver/Module.js index c75473ac..6b68a620 100644 --- a/react-packager/src/DependencyResolver/Module.js +++ b/react-packager/src/DependencyResolver/Module.js @@ -11,6 +11,7 @@ const crypto = require('crypto'); const docblock = require('./DependencyGraph/docblock'); const isAbsolutePath = require('absolute-path'); +const jsonStableStringify = require('json-stable-stringify'); const path = require('path'); const extractRequires = require('./lib/extractRequires'); @@ -206,29 +207,15 @@ function whileInDocBlock(chunk, i, result) { const knownHashes = new WeakMap(); function stableObjectHash(object) { let digest = knownHashes.get(object); - if (!digest) { - const hash = crypto.createHash('md5'); - stableObjectHash.addTo(object, hash); - digest = hash.digest('base64'); + digest = crypto.createHash('md5') + .update(jsonStableStringify(object)) + .digest('base64'); knownHashes.set(object, digest); } return digest; } -stableObjectHash.addTo = function addTo(value, hash) { - if (value === null || typeof value !== 'object') { - hash.update(JSON.stringify(value)); - } else { - Object.keys(value).sort().forEach(key => { - const valueForKey = value[key]; - if (valueForKey !== undefined) { - hash.update(key); - addTo(valueForKey, hash); - } - }); - } -}; function cacheKey(field, transformOptions) { return transformOptions !== undefined diff --git a/react-packager/src/DependencyResolver/__tests__/Module-test.js b/react-packager/src/DependencyResolver/__tests__/Module-test.js index 71c3a43d..bb4fa75e 100644 --- a/react-packager/src/DependencyResolver/__tests__/Module-test.js +++ b/react-packager/src/DependencyResolver/__tests__/Module-test.js @@ -10,6 +10,7 @@ jest .dontMock('absolute-path') + .dontMock('json-stable-stringify') .dontMock('../fastfs') .dontMock('../lib/extractRequires') .dontMock('../lib/replacePatterns')