[Fresh] Hash signatures (#16738)
Dan Abramov committed
Sep 10, 2019 at 20:28 UTC
ba6bb0fccf21d6b2c6eeb7a6e1036b0071711c2c
2 files changed
+23
-3
packages/react-refresh/src/ReactFreshBabelPlugin.js
+15
-2
@@ -7,7 +7,7 @@
7
8
'use strict';
9
10
-export default function(babel, opts) {
10
+export default function(babel, opts = {}) {
11
if (typeof babel.getEnv === 'function') {
12
// Only available in Babel 7.
13
const env = babel.getEnv();
@@ -300,7 +300,20 @@ export default function(babel, opts) {
300
}
301
});
302
303
- const args = [node, t.stringLiteral(key)];
303
+ let finalKey = key;
304
+ if (typeof require === 'function' && !opts.emitFullSignatures) {
305
+ // Prefer to hash when we can (e.g. outside of ASTExplorer).
306
+ // This makes it deterministically compact, even if there's
307
+ // e.g. a useState ininitalizer with some code inside.
308
+ // We also need it for www that has transforms like cx()
309
+ // that don't understand if something is part of a string.
310
+ finalKey = require('crypto')
311
+ .createHash('sha1')
312
+ .update(key)
313
+ .digest('base64');
314
+ }
315
+
316
+ const args = [node, t.stringLiteral(finalKey)];
317
if (forceReset || customHooksInScope.length > 0) {
318
args.push(t.booleanLiteral(forceReset));
319
}
packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js
+8
-1
@@ -19,7 +19,14 @@ function transform(input, options = {}) {
19
plugins: [
20
'@babel/syntax-jsx',
21
'@babel/syntax-dynamic-import',
22
- [freshPlugin, {skipEnvCheck: true}],
22
+ [
23
+ freshPlugin,
24
+ {
25
+ skipEnvCheck: true,
26
+ // To simplify debugging tests:
27
+ emitFullSignatures: true,
28
+ },
29
+ ],
30
...(options.plugins || []),
31
],
32
}).code,