Skip to content

Commit ba6bb0f

Browse files
authored
[Fresh] Hash signatures (#16738)
1 parent fd3e8cb commit ba6bb0f

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

packages/react-refresh/src/ReactFreshBabelPlugin.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
'use strict';
99

10-
export default function(babel, opts) {
10+
export default function(babel, opts = {}) {
1111
if (typeof babel.getEnv === 'function') {
1212
// Only available in Babel 7.
1313
const env = babel.getEnv();
@@ -300,7 +300,20 @@ export default function(babel, opts) {
300300
}
301301
});
302302

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)];
304317
if (forceReset || customHooksInScope.length > 0) {
305318
args.push(t.booleanLiteral(forceReset));
306319
}

packages/react-refresh/src/__tests__/ReactFreshBabelPlugin-test.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ function transform(input, options = {}) {
1919
plugins: [
2020
'@babel/syntax-jsx',
2121
'@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+
],
2330
...(options.plugins || []),
2431
],
2532
}).code,

0 commit comments

Comments
 (0)