-
-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Breaking] Ensure stability of filename cache-keys (#909)
* [Bugfix] Ensure stability of filename cache-keys `JSON.stringify(structure)` isn’t inherently stable as it relies on various internal details of how `structure` was created. As written, if a given babel configuration is create in an dynamic manner, it is possible for babel-loader to have spurious cache misses. To address this, we can use one of the many stable stringify alternatives. For this PR I have selected [fast-stable-stringify](https://www.npmjs.com/package/fast-stable-stringify) for that task, as it appears both popular and it’s benchmarks look promising. This PR does not explicitly include tests, as testing this is both tricky to test in this context, and the important tests are contained within fast-stable-stringify itself. * update yarn.lock * perf: avoid serialize options twice The options will be serialized in the cache#filename function with the cache identifier, so we don't have to include options in the cache identifier. * polish: use our own config serializer * update cacheIdentifier docs --------- Co-authored-by: Huáng Jùnliàng <[email protected]>
- Loading branch information
1 parent
e449287
commit 3c63b4e
Showing
5 changed files
with
91 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
var objToString = Object.prototype.toString; | ||
var objKeys = Object.getOwnPropertyNames; | ||
|
||
/** | ||
* A custom Babel options serializer | ||
* | ||
* Intentional deviation from JSON.stringify: | ||
* 1. Object properties are sorted before seralizing | ||
* 2. The output is NOT a valid JSON: e.g. | ||
* The output does not enquote strings, which means a JSON-like string '{"a":1}' | ||
* will share the same result with an JS object { a: 1 }. This is not an issue | ||
* for Babel options, but it can not be used for general serialization purpose | ||
* 3. Only 20% slower than the native JSON.stringify on V8 | ||
* | ||
* This function is a fork from https://github.com/nickyout/fast-stable-stringify | ||
* @param {*} val Babel options | ||
* @param {*} isArrayProp | ||
* @returns serialized Babel options | ||
*/ | ||
function serialize(val, isArrayProp) { | ||
var i, max, str, keys, key, propVal, toStr; | ||
if (val === true) { | ||
return "!0"; | ||
} | ||
if (val === false) { | ||
return "!1"; | ||
} | ||
switch (typeof val) { | ||
case "object": | ||
if (val === null) { | ||
return null; | ||
} else if (val.toJSON && typeof val.toJSON === "function") { | ||
return serialize(val.toJSON(), isArrayProp); | ||
} else { | ||
toStr = objToString.call(val); | ||
if (toStr === "[object Array]") { | ||
str = "["; | ||
max = val.length - 1; | ||
for (i = 0; i < max; i++) { | ||
str += serialize(val[i], true) + ","; | ||
} | ||
if (max > -1) { | ||
str += serialize(val[i], true); | ||
} | ||
return str + "]"; | ||
} else if (toStr === "[object Object]") { | ||
// only object is left | ||
keys = objKeys(val).sort(); | ||
max = keys.length; | ||
str = "{"; | ||
i = 0; | ||
while (i < max) { | ||
key = keys[i]; | ||
propVal = serialize(val[key], false); | ||
if (propVal !== undefined) { | ||
if (str) { | ||
str += ","; | ||
} | ||
str += '"' + key + '":' + propVal; | ||
} | ||
i++; | ||
} | ||
return str + "}"; | ||
} else { | ||
return JSON.stringify(val); | ||
} | ||
} | ||
case "function": | ||
case "undefined": | ||
return isArrayProp ? null : undefined; | ||
case "string": | ||
return val; | ||
default: | ||
return isFinite(val) ? val : null; | ||
} | ||
} | ||
|
||
module.exports = function (val) { | ||
var returnVal = serialize(val, false); | ||
if (returnVal !== undefined) { | ||
return "" + returnVal; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters