react-refresh 0.3.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,12 +11,13 @@
11
11
  c,b),!0;case "CallExpression":var f=b.get("arguments");if(void 0===f||0===f.length)break;var g=b.get("callee");switch(g.node.type){case "MemberExpression":case "Identifier":g=g.getSource();if(!m(a+"$"+g,f[0],d))return!1;d(a,c,b);return!0;default:return!1}case "VariableDeclarator":if(f=c.init,null!==f&&(g=c.id.name,t(g))){switch(f.type){case "ArrowFunctionExpression":case "FunctionExpression":break;case "CallExpression":c=f.callee;var e=c.type;if("Import"===e)return!1;if("Identifier"===e){if(0===c.name.indexOf("require")||
12
12
  0===c.name.indexOf("import"))return!1}else if("MemberExpression"!==e)return!1;break;case "TaggedTemplateExpression":break;default:return!1}c=b.get("init");if(m(a,c,d))return!0;g=b.scope.getBinding(g);if(void 0===g)return;b=!1;g=g.referencePaths;for(e=0;e<g.length;e++){var h=g[e];if("JSXIdentifier"===h.node.type||"Identifier"===h.node.type){h=h.parent;if("JSXOpeningElement"===h.type)b=!0;else if("CallExpression"===h.type){h=h.callee;var k=void 0;switch(h.type){case "Identifier":k=h.name;break;case "MemberExpression":k=
13
13
  h.property.name}switch(k){case "createElement":case "jsx":case "jsxDEV":case "jsxs":b=!0}}if(b)return d(a,f,c),!0}}}}return!1}function w(a){a=n.get(a);return void 0===a?null:{key:a.map(function(a){return a.name+"{"+a.key+"}"}).join("\n"),customHooks:a.filter(function(a){a:switch(a.name){case "useState":case "React.useState":case "useReducer":case "React.useReducer":case "useEffect":case "React.useEffect":case "useLayoutEffect":case "React.useLayoutEffect":case "useMemo":case "React.useMemo":case "useCallback":case "React.useCallback":case "useRef":case "React.useRef":case "useContext":case "React.useContext":case "useImperativeMethods":case "React.useImperativeMethods":case "useDebugValue":case "React.useDebugValue":a=
14
- !0;break a;default:a=!1}return!a}).map(function(a){return e.cloneDeep(a.callee)})}}function C(a){a=a.hub.file;var b=x.get(a);if(void 0!==b)return b;b=!1;for(var d=a.ast.comments,c=0;c<d.length;c++)if(-1!==d[c].value.indexOf("@refresh reset")){b=!0;break}x.set(a,b);return b}function u(a,b,d){var c=b.key;b=b.customHooks;var f=C(d.path),g=[];b.forEach(function(a){var b=void 0;switch(a.type){case "MemberExpression":"Identifier"===a.object.type&&(b=a.object.name);break;case "Identifier":b=a.name}d.hasBinding(b)?
15
- g.push(a):f=!0});a=[a,e.stringLiteral(c)];(f||0<g.length)&&a.push(e.booleanLiteral(f));0<g.length&&a.push(e.functionExpression(null,[],e.blockStatement([e.returnStatement(e.arrayExpression(g))])));return a}if("function"===typeof q.getEnv){var y=q.getEnv();if("development"!==y)throw Error('React Refresh Babel transform should only be enabled in development environment. Instead, the environment is: "'+y+'".');}var e=q.types,l=new Map,x=new WeakMap,k=new WeakSet,p=new WeakSet,z=new WeakSet,n=new WeakMap,
16
- D={CallExpression:function(a){var b=a.node.callee,d=null;switch(b.type){case "Identifier":d=b.name;break;case "MemberExpression":d=b.property.name}if(null!==d&&/^use[A-Z]/.test(d)&&(b=a.scope.getFunctionParent(),null!==b)){b=b.block;n.has(b)||n.set(b,[]);b=n.get(b);var c="";"VariableDeclarator"===a.parent.type&&(c=a.parentPath.get("id").getSource());var f=a.get("arguments");"useState"===d&&0<f.length?c+="("+f[0].getSource()+")":"useReducer"===d&&1<f.length&&(c+="("+f[1].getSource()+")");b.push({callee:a.node.callee,
17
- name:d,key:c})}}};return{visitor:{ExportDefaultDeclaration:function(a){var b=a.node,d=b.declaration,c=a.get("declaration");if("CallExpression"===d.type&&!k.has(b)){k.add(b);var f=a.parentPath;m("%default%",c,function(a,b,c){null!==c&&(a=r(f,a),c.replaceWith(e.assignmentExpression("=",a,b)))})}},FunctionDeclaration:{enter:function(a){var b=a.node,d=void 0,c=void 0;switch(a.parent.type){case "Program":c=a;d=a.parentPath;break;case "ExportNamedDeclaration":c=a.parentPath;d=c.parentPath;break;case "ExportDefaultDeclaration":c=
18
- a.parentPath;d=c.parentPath;break;default:return}var f=b.id;null!==f&&(f=f.name,t(f)&&!k.has(b)&&(k.add(b),m(f,a,function(a,b){a=r(d,a);c.insertAfter(e.expressionStatement(e.assignmentExpression("=",a,b)))})))},exit:function(a){var b=a.node,d=b.id;if(null!==d){var c=w(b);if(null!==c&&!p.has(b)){p.add(b);b=a.scope.generateUidIdentifier("_s");a.scope.parent.push({id:b,init:e.callExpression(e.identifier("$RefreshSig$"),[])});a.get("body").unshiftContainer("body",e.expressionStatement(e.callExpression(b,
19
- [])));var f=null;a.find(function(a){if(a.parentPath.isBlock())return f=a,!0});null!==f&&f.insertAfter(e.expressionStatement(e.callExpression(b,u(d,c,f.scope))))}}}},"ArrowFunctionExpression|FunctionExpression":{exit:function(a){var b=a.node,d=w(b);if(null!==d&&!p.has(b)){p.add(b);var c=a.scope.generateUidIdentifier("_s");a.scope.parent.push({id:c,init:e.callExpression(e.identifier("$RefreshSig$"),[])});a.get("body").unshiftContainer("body",e.expressionStatement(e.callExpression(c,[])));if("VariableDeclarator"===
20
- a.parent.type){var f=null;a.find(function(a){if(a.parentPath.isBlock())return f=a,!0});null!==f&&f.insertAfter(e.expressionStatement(e.callExpression(c,u(a.parent.id,d,f.scope))))}else a.replaceWith(e.callExpression(c,u(b,d,a.scope)))}}},VariableDeclaration:function(a){var b=a.node,d=void 0,c=void 0;switch(a.parent.type){case "Program":c=a;d=a.parentPath;break;case "ExportNamedDeclaration":c=a.parentPath;d=c.parentPath;break;case "ExportDefaultDeclaration":c=a.parentPath;d=c.parentPath;break;default:return}if(!k.has(b)&&
21
- (k.add(b),a=a.get("declarations"),1===a.length)){var f=a[0];m(f.node.id.name,f,function(a,b,h){null!==h&&(a=r(d,a),"ArrowFunctionExpression"!==b.type&&"FunctionExpression"!==b.type||"VariableDeclarator"!==h.parent.type?h.replaceWith(e.assignmentExpression("=",a,b)):c.insertAfter(e.expressionStatement(e.assignmentExpression("=",a,f.node.id))))})}},Program:{enter:function(a){a.traverse(D)},exit:function(a){var b=l.get(a);if(void 0!==b){var d=a.node;if(!z.has(d)){z.add(d);l.delete(a);var c=[];a.pushContainer("body",
22
- e.variableDeclaration("var",c));b.forEach(function(b){var d=b.handle;b=b.persistentID;a.pushContainer("body",e.expressionStatement(e.callExpression(e.identifier("$RefreshReg$"),[d,e.stringLiteral(b)])));c.push(e.variableDeclarator(d))})}}}}}}}var A={default:v},B=A&&v||A;module.exports=B.default||B;
14
+ !0;break a;default:a=!1}return!a}).map(function(a){return e.cloneDeep(a.callee)})}}function D(a){a=a.hub.file;var b=x.get(a);if(void 0!==b)return b;b=!1;for(var d=a.ast.comments,c=0;c<d.length;c++)if(-1!==d[c].value.indexOf("@refresh reset")){b=!0;break}x.set(a,b);return b}function u(a,b,d){var c=b.key;b=b.customHooks;var f=D(d.path),g=[];b.forEach(function(a){switch(a.type){case "MemberExpression":if("Identifier"===a.object.type)var b=a.object.name;break;case "Identifier":b=a.name}d.hasBinding(b)?
15
+ g.push(a):f=!0});b=c;"function"!==typeof require||y.emitFullSignatures||(b=require("crypto").createHash("sha1").update(c).digest("base64"));a=[a,e.stringLiteral(b)];(f||0<g.length)&&a.push(e.booleanLiteral(f));0<g.length&&a.push(e.functionExpression(null,[],e.blockStatement([e.returnStatement(e.arrayExpression(g))])));return a}var y=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};if("function"===typeof q.getEnv){var z=q.getEnv();if("development"!==z&&!y.skipEnvCheck)throw Error('React Refresh Babel transform should only be enabled in development environment. Instead, the environment is: "'+
16
+ z+'". If you want to override this check, pass {skipEnvCheck: true} as plugin options.');}var e=q.types,l=new Map,x=new WeakMap,k=new WeakSet,p=new WeakSet,A=new WeakSet,n=new WeakMap,E={CallExpression:function(a){var b=a.node.callee,d=null;switch(b.type){case "Identifier":d=b.name;break;case "MemberExpression":d=b.property.name}if(null!==d&&/^use[A-Z]/.test(d)&&(b=a.scope.getFunctionParent(),null!==b)){b=b.block;n.has(b)||n.set(b,[]);b=n.get(b);var c="";"VariableDeclarator"===a.parent.type&&(c=a.parentPath.get("id").getSource());
17
+ var f=a.get("arguments");"useState"===d&&0<f.length?c+="("+f[0].getSource()+")":"useReducer"===d&&1<f.length&&(c+="("+f[1].getSource()+")");b.push({callee:a.node.callee,name:d,key:c})}}};return{visitor:{ExportDefaultDeclaration:function(a){var b=a.node,d=b.declaration,c=a.get("declaration");if("CallExpression"===d.type&&!k.has(b)){k.add(b);var f=a.parentPath;m("%default%",c,function(a,b,c){null!==c&&(a=r(f,a),c.replaceWith(e.assignmentExpression("=",a,b)))})}},FunctionDeclaration:{enter:function(a){var b=
18
+ a.node;switch(a.parent.type){case "Program":var d=a;var c=a.parentPath;break;case "ExportNamedDeclaration":d=a.parentPath;c=d.parentPath;break;case "ExportDefaultDeclaration":d=a.parentPath;c=d.parentPath;break;default:return}var f=b.id;null!==f&&(f=f.name,t(f)&&!k.has(b)&&(k.add(b),m(f,a,function(a,b){a=r(c,a);d.insertAfter(e.expressionStatement(e.assignmentExpression("=",a,b)))})))},exit:function(a){var b=a.node,d=b.id;if(null!==d){var c=w(b);if(null!==c&&!p.has(b)){p.add(b);b=a.scope.generateUidIdentifier("_s");
19
+ a.scope.parent.push({id:b,init:e.callExpression(e.identifier("$RefreshSig$"),[])});a.get("body").unshiftContainer("body",e.expressionStatement(e.callExpression(b,[])));var f=null;a.find(function(a){if(a.parentPath.isBlock())return f=a,!0});null!==f&&f.insertAfter(e.expressionStatement(e.callExpression(b,u(d,c,f.scope))))}}}},"ArrowFunctionExpression|FunctionExpression":{exit:function(a){var b=a.node,d=w(b);if(null!==d&&!p.has(b)){p.add(b);var c=a.scope.generateUidIdentifier("_s");a.scope.parent.push({id:c,
20
+ init:e.callExpression(e.identifier("$RefreshSig$"),[])});"BlockStatement"!==a.node.body.type&&(a.node.body=e.blockStatement([e.returnStatement(a.node.body)]));a.get("body").unshiftContainer("body",e.expressionStatement(e.callExpression(c,[])));if("VariableDeclarator"===a.parent.type){var f=null;a.find(function(a){if(a.parentPath.isBlock())return f=a,!0});null!==f&&f.insertAfter(e.expressionStatement(e.callExpression(c,u(a.parent.id,d,f.scope))))}else a.replaceWith(e.callExpression(c,u(b,d,a.scope)))}}},
21
+ VariableDeclaration:function(a){var b=a.node;switch(a.parent.type){case "Program":var d=a;var c=a.parentPath;break;case "ExportNamedDeclaration":d=a.parentPath;c=d.parentPath;break;case "ExportDefaultDeclaration":d=a.parentPath;c=d.parentPath;break;default:return}if(!k.has(b)&&(k.add(b),a=a.get("declarations"),1===a.length)){var f=a[0];m(f.node.id.name,f,function(a,b,h){null!==h&&(a=r(c,a),"ArrowFunctionExpression"!==b.type&&"FunctionExpression"!==b.type||"VariableDeclarator"!==h.parent.type?h.replaceWith(e.assignmentExpression("=",
22
+ a,b)):d.insertAfter(e.expressionStatement(e.assignmentExpression("=",a,f.node.id))))})}},Program:{enter:function(a){a.traverse(E)},exit:function(a){var b=l.get(a);if(void 0!==b){var d=a.node;if(!A.has(d)){A.add(d);l.delete(a);var c=[];a.pushContainer("body",e.variableDeclaration("var",c));b.forEach(function(b){var d=b.handle;b=b.persistentID;a.pushContainer("body",e.expressionStatement(e.callExpression(e.identifier("$RefreshReg$"),[d,e.stringLiteral(b)])));c.push(e.variableDeclarator(d))})}}}}}}}
23
+ var B={default:v},C=B&&v||B;module.exports=C.default||C;
@@ -24,93 +24,35 @@ var hasSymbol = typeof Symbol === 'function' && Symbol.for;
24
24
 
25
25
 
26
26
 
27
-
28
-
29
- // TODO: We don't use AsyncMode or ConcurrentMode anymore. They were temporary
27
+ // TODO: We don't use AsyncMode or ConcurrentMode anymore. They were temporary
30
28
  // (unstable) APIs that have been removed. Can we remove the symbols?
31
29
 
32
30
 
31
+
33
32
  var REACT_FORWARD_REF_TYPE = hasSymbol ? Symbol.for('react.forward_ref') : 0xead0;
34
33
 
35
34
 
36
35
  var REACT_MEMO_TYPE = hasSymbol ? Symbol.for('react.memo') : 0xead3;
37
36
 
38
- /**
39
- * Similar to invariant but only logs a warning if the condition is not met.
40
- * This can be used to log issues in development environments in critical
41
- * paths. Removing the logging code for production environments will keep the
42
- * same logic and follow the same code paths.
43
- */
44
-
45
- var warningWithoutStack = function () {};
46
-
47
- {
48
- warningWithoutStack = function (condition, format) {
49
- for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
50
- args[_key - 2] = arguments[_key];
51
- }
52
-
53
- if (format === undefined) {
54
- throw new Error('`warningWithoutStack(condition, format, ...args)` requires a warning ' + 'message argument');
55
- }
56
- if (args.length > 8) {
57
- // Check before the condition to catch violations early.
58
- throw new Error('warningWithoutStack() currently supports at most 8 arguments.');
59
- }
60
- if (condition) {
61
- return;
62
- }
63
- if (typeof console !== 'undefined') {
64
- var argsWithFormat = args.map(function (item) {
65
- return '' + item;
66
- });
67
- argsWithFormat.unshift('Warning: ' + format);
68
-
69
- // We intentionally don't use spread (or .apply) directly because it
70
- // breaks IE9: https://github.com/facebook/react/issues/13610
71
- Function.prototype.apply.call(console.error, console, argsWithFormat);
72
- }
73
- try {
74
- // --- Welcome to debugging React ---
75
- // This error was thrown as a convenience so that you can use this stack
76
- // to find the callsite that caused this warning to fire.
77
- var argIndex = 0;
78
- var message = 'Warning: ' + format.replace(/%s/g, function () {
79
- return args[argIndex++];
80
- });
81
- throw new Error(message);
82
- } catch (x) {}
83
- };
84
- }
85
-
86
- var warningWithoutStack$1 = warningWithoutStack;
87
-
88
- // In old environments, we'll leak previous types after every edit.
89
- var PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map;
90
-
91
- // We never remove these associations.
37
+ var PossiblyWeakMap = typeof WeakMap === 'function' ? WeakMap : Map; // We never remove these associations.
92
38
  // It's OK to reference families, but use WeakMap/Set for types.
39
+
93
40
  var allFamiliesByID = new Map();
94
41
  var allFamiliesByType = new PossiblyWeakMap();
95
- var allSignaturesByType = new PossiblyWeakMap();
96
- // This WeakMap is read by React, so we only put families
42
+ var allSignaturesByType = new PossiblyWeakMap(); // This WeakMap is read by React, so we only put families
97
43
  // that have actually been edited here. This keeps checks fast.
98
44
  // $FlowIssue
99
- var updatedFamiliesByType = new PossiblyWeakMap();
100
45
 
101
- // This is cleared on every performReactRefresh() call.
46
+ var updatedFamiliesByType = new PossiblyWeakMap(); // This is cleared on every performReactRefresh() call.
102
47
  // It is an array of [Family, NextType] tuples.
103
- var pendingUpdates = [];
104
48
 
105
- // This is injected by the renderer via DevTools global hook.
106
- var setRefreshHandler = null;
107
- var scheduleRefresh = null;
108
- var scheduleRoot = null;
109
- var findHostInstancesForRefresh = null;
49
+ var pendingUpdates = []; // This is injected by the renderer via DevTools global hook.
50
+
51
+ var helpersByRendererID = new Map();
52
+ var helpersByRoot = new Map(); // We keep track of mounted roots so we can schedule updates.
53
+
54
+ var mountedRoots = new Set(); // If a root captures an error, we add its element to this Map so we can retry on edit.
110
55
 
111
- // We keep track of mounted roots so we can schedule updates.
112
- var mountedRoots = new Set();
113
- // If a root captures an error, we add its element to this Map so we can retry on edit.
114
56
  var failedRoots = new Map();
115
57
  var didSomeRootFailOnMount = false;
116
58
 
@@ -120,7 +62,8 @@ function computeFullKey(signature) {
120
62
  }
121
63
 
122
64
  var fullKey = signature.ownKey;
123
- var hooks = void 0;
65
+ var hooks;
66
+
124
67
  try {
125
68
  hooks = signature.getCustomHooks();
126
69
  } catch (err) {
@@ -134,22 +77,28 @@ function computeFullKey(signature) {
134
77
 
135
78
  for (var i = 0; i < hooks.length; i++) {
136
79
  var hook = hooks[i];
80
+
137
81
  if (typeof hook !== 'function') {
138
82
  // Something's wrong. Assume we need to remount.
139
83
  signature.forceReset = true;
140
84
  signature.fullKey = fullKey;
141
85
  return fullKey;
142
86
  }
87
+
143
88
  var nestedHookSignature = allSignaturesByType.get(hook);
89
+
144
90
  if (nestedHookSignature === undefined) {
145
91
  // No signature means Hook wasn't in the source code, e.g. in a library.
146
92
  // We'll skip it because we can assume it won't change during this session.
147
93
  continue;
148
94
  }
95
+
149
96
  var nestedHookKey = computeFullKey(nestedHookSignature);
97
+
150
98
  if (nestedHookSignature.forceReset) {
151
99
  signature.forceReset = true;
152
100
  }
101
+
153
102
  fullKey += '\n---\n' + nestedHookKey;
154
103
  }
155
104
 
@@ -164,12 +113,15 @@ function haveEqualSignatures(prevType, nextType) {
164
113
  if (prevSignature === undefined && nextSignature === undefined) {
165
114
  return true;
166
115
  }
116
+
167
117
  if (prevSignature === undefined || nextSignature === undefined) {
168
118
  return false;
169
119
  }
120
+
170
121
  if (computeFullKey(prevSignature) !== computeFullKey(nextSignature)) {
171
122
  return false;
172
123
  }
124
+
173
125
  if (nextSignature.forceReset) {
174
126
  return false;
175
127
  }
@@ -185,9 +137,11 @@ function canPreserveStateBetween(prevType, nextType) {
185
137
  if (isReactClass(prevType) || isReactClass(nextType)) {
186
138
  return false;
187
139
  }
140
+
188
141
  if (haveEqualSignatures(prevType, nextType)) {
189
142
  return true;
190
143
  }
144
+
191
145
  return false;
192
146
  }
193
147
 
@@ -204,118 +158,119 @@ function performReactRefresh() {
204
158
 
205
159
  var staleFamilies = new Set();
206
160
  var updatedFamilies = new Set();
207
-
208
161
  var updates = pendingUpdates;
209
162
  pendingUpdates = [];
210
163
  updates.forEach(function (_ref) {
211
164
  var family = _ref[0],
212
165
  nextType = _ref[1];
213
-
214
166
  // Now that we got a real edit, we can create associations
215
167
  // that will be read by the React reconciler.
216
168
  var prevType = family.current;
217
169
  updatedFamiliesByType.set(prevType, family);
218
170
  updatedFamiliesByType.set(nextType, family);
219
- family.current = nextType;
171
+ family.current = nextType; // Determine whether this should be a re-render or a re-mount.
220
172
 
221
- // Determine whether this should be a re-render or a re-mount.
222
173
  if (canPreserveStateBetween(prevType, nextType)) {
223
174
  updatedFamilies.add(family);
224
175
  } else {
225
176
  staleFamilies.add(family);
226
177
  }
227
- });
178
+ }); // TODO: rename these fields to something more meaningful.
228
179
 
229
- // TODO: rename these fields to something more meaningful.
230
180
  var update = {
231
- updatedFamilies: updatedFamilies, // Families that will re-render preserving state
181
+ updatedFamilies: updatedFamilies,
182
+ // Families that will re-render preserving state
232
183
  staleFamilies: staleFamilies // Families that will be remounted
233
- };
234
-
235
- if (typeof setRefreshHandler !== 'function') {
236
- warningWithoutStack$1(false, 'Could not find the setRefreshHandler() implementation. ' + 'This likely means that injectIntoGlobalHook() was either ' + 'called before the global DevTools hook was set up, or after the ' + 'renderer has already initialized. Please file an issue with a reproducing case.');
237
- return null;
238
- }
239
-
240
- if (typeof scheduleRefresh !== 'function') {
241
- warningWithoutStack$1(false, 'Could not find the scheduleRefresh() implementation. ' + 'This likely means that injectIntoGlobalHook() was either ' + 'called before the global DevTools hook was set up, or after the ' + 'renderer has already initialized. Please file an issue with a reproducing case.');
242
- return null;
243
- }
244
- if (typeof scheduleRoot !== 'function') {
245
- warningWithoutStack$1(false, 'Could not find the scheduleRoot() implementation. ' + 'This likely means that injectIntoGlobalHook() was either ' + 'called before the global DevTools hook was set up, or after the ' + 'renderer has already initialized. Please file an issue with a reproducing case.');
246
- return null;
247
- }
248
- var scheduleRefreshForRoot = scheduleRefresh;
249
- var scheduleRenderForRoot = scheduleRoot;
250
-
251
- // Even if there are no roots, set the handler on first update.
252
- // This ensures that if *new* roots are mounted, they'll use the resolve handler.
253
- setRefreshHandler(resolveFamily);
254
184
 
185
+ };
186
+ helpersByRendererID.forEach(function (helpers) {
187
+ // Even if there are no roots, set the handler on first update.
188
+ // This ensures that if *new* roots are mounted, they'll use the resolve handler.
189
+ helpers.setRefreshHandler(resolveFamily);
190
+ });
255
191
  var didError = false;
256
192
  var firstError = null;
257
193
  failedRoots.forEach(function (element, root) {
194
+ var helpers = helpersByRoot.get(root);
195
+
196
+ if (helpers === undefined) {
197
+ throw new Error('Could not find helpers for a root. This is a bug in React Refresh.');
198
+ }
199
+
258
200
  try {
259
- scheduleRenderForRoot(root, element);
201
+ helpers.scheduleRoot(root, element);
260
202
  } catch (err) {
261
203
  if (!didError) {
262
204
  didError = true;
263
205
  firstError = err;
264
- }
265
- // Keep trying other roots.
206
+ } // Keep trying other roots.
207
+
266
208
  }
267
209
  });
268
210
  mountedRoots.forEach(function (root) {
211
+ var helpers = helpersByRoot.get(root);
212
+
213
+ if (helpers === undefined) {
214
+ throw new Error('Could not find helpers for a root. This is a bug in React Refresh.');
215
+ }
216
+
269
217
  try {
270
- scheduleRefreshForRoot(root, update);
218
+ helpers.scheduleRefresh(root, update);
271
219
  } catch (err) {
272
220
  if (!didError) {
273
221
  didError = true;
274
222
  firstError = err;
275
- }
276
- // Keep trying other roots.
223
+ } // Keep trying other roots.
224
+
277
225
  }
278
226
  });
227
+
279
228
  if (didError) {
280
229
  throw firstError;
281
230
  }
231
+
282
232
  return update;
283
233
  }
284
234
  }
285
-
286
235
  function register(type, id) {
287
236
  {
288
237
  if (type === null) {
289
238
  return;
290
239
  }
240
+
291
241
  if (typeof type !== 'function' && typeof type !== 'object') {
292
242
  return;
293
- }
294
-
295
- // This can happen in an edge case, e.g. if we register
243
+ } // This can happen in an edge case, e.g. if we register
296
244
  // return value of a HOC but it returns a cached component.
297
245
  // Ignore anything but the first registration for each type.
246
+
247
+
298
248
  if (allFamiliesByType.has(type)) {
299
249
  return;
300
- }
301
- // Create family or remember to update it.
250
+ } // Create family or remember to update it.
302
251
  // None of this bookkeeping affects reconciliation
303
252
  // until the first performReactRefresh() call above.
253
+
254
+
304
255
  var family = allFamiliesByID.get(id);
256
+
305
257
  if (family === undefined) {
306
- family = { current: type };
258
+ family = {
259
+ current: type
260
+ };
307
261
  allFamiliesByID.set(id, family);
308
262
  } else {
309
263
  pendingUpdates.push([family, type]);
310
264
  }
311
- allFamiliesByType.set(type, family);
312
265
 
313
- // Visit inner types because we might not have registered them.
266
+ allFamiliesByType.set(type, family); // Visit inner types because we might not have registered them.
267
+
314
268
  if (typeof type === 'object' && type !== null) {
315
269
  switch (type.$$typeof) {
316
270
  case REACT_FORWARD_REF_TYPE:
317
271
  register(type.render, id + '$render');
318
272
  break;
273
+
319
274
  case REACT_MEMO_TYPE:
320
275
  register(type.type, id + '$type');
321
276
  break;
@@ -323,10 +278,9 @@ function register(type, id) {
323
278
  }
324
279
  }
325
280
  }
326
-
327
281
  function setSignature(type, key) {
328
282
  var forceReset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
329
- var getCustomHooks = arguments[3];
283
+ var getCustomHooks = arguments.length > 3 ? arguments[3] : undefined;
330
284
 
331
285
  {
332
286
  allSignaturesByType.set(type, {
@@ -338,41 +292,39 @@ function setSignature(type, key) {
338
292
  }
339
293
  });
340
294
  }
341
- }
342
-
343
- // This is lazily called during first render for a type.
295
+ } // This is lazily called during first render for a type.
344
296
  // It captures Hook list at that time so inline requires don't break comparisons.
297
+
345
298
  function collectCustomHooksForSignature(type) {
346
299
  {
347
300
  var signature = allSignaturesByType.get(type);
301
+
348
302
  if (signature !== undefined) {
349
303
  computeFullKey(signature);
350
304
  }
351
305
  }
352
306
  }
353
-
354
307
  function getFamilyByID(id) {
355
308
  {
356
309
  return allFamiliesByID.get(id);
357
310
  }
358
311
  }
359
-
360
312
  function getFamilyByType(type) {
361
313
  {
362
314
  return allFamiliesByType.get(type);
363
315
  }
364
316
  }
365
-
366
317
  function findAffectedHostInstances(families) {
367
318
  {
368
- if (typeof findHostInstancesForRefresh !== 'function') {
369
- warningWithoutStack$1(false, 'Could not find the findHostInstancesForRefresh() implementation. ' + 'This likely means that injectIntoGlobalHook() was either ' + 'called before the global DevTools hook was set up, or after the ' + 'renderer has already initialized. Please file an issue with a reproducing case.');
370
- return new Set();
371
- }
372
- var findInstances = findHostInstancesForRefresh;
373
319
  var affectedInstances = new Set();
374
320
  mountedRoots.forEach(function (root) {
375
- var instancesForRoot = findInstances(root, families);
321
+ var helpers = helpersByRoot.get(root);
322
+
323
+ if (helpers === undefined) {
324
+ throw new Error('Could not find helpers for a root. This is a bug in React Refresh.');
325
+ }
326
+
327
+ var instancesForRoot = helpers.findHostInstancesForRefresh(root, families);
376
328
  instancesForRoot.forEach(function (inst) {
377
329
  affectedInstances.add(inst);
378
330
  });
@@ -380,44 +332,56 @@ function findAffectedHostInstances(families) {
380
332
  return affectedInstances;
381
333
  }
382
334
  }
383
-
384
335
  function injectIntoGlobalHook(globalObject) {
385
336
  {
386
337
  // For React Native, the global hook will be set up by require('react-devtools-core').
387
338
  // That code will run before us. So we need to monkeypatch functions on existing hook.
388
-
389
339
  // For React Web, the global hook will be set up by the extension.
390
340
  // This will also run before us.
391
341
  var hook = globalObject.__REACT_DEVTOOLS_GLOBAL_HOOK__;
342
+
392
343
  if (hook === undefined) {
393
344
  // However, if there is no DevTools extension, we'll need to set up the global hook ourselves.
394
345
  // Note that in this case it's important that renderer code runs *after* this method call.
395
346
  // Otherwise, the renderer will think that there is no global hook, and won't do the injection.
347
+ var nextID = 0;
396
348
  globalObject.__REACT_DEVTOOLS_GLOBAL_HOOK__ = hook = {
397
349
  supportsFiber: true,
398
- inject: function () {},
350
+ inject: function (injected) {
351
+ return nextID++;
352
+ },
399
353
  onCommitFiberRoot: function (id, root, maybePriorityLevel, didError) {},
400
354
  onCommitFiberUnmount: function () {}
401
355
  };
402
- }
356
+ } // Here, we just want to get a reference to scheduleRefresh.
357
+
403
358
 
404
- // Here, we just want to get a reference to scheduleRefresh.
405
359
  var oldInject = hook.inject;
360
+
406
361
  hook.inject = function (injected) {
407
- findHostInstancesForRefresh = injected.findHostInstancesForRefresh;
408
- scheduleRefresh = injected.scheduleRefresh;
409
- scheduleRoot = injected.scheduleRoot;
410
- setRefreshHandler = injected.setRefreshHandler;
411
- return oldInject.apply(this, arguments);
412
- };
362
+ var id = oldInject.apply(this, arguments);
363
+
364
+ if (typeof injected.scheduleRefresh === 'function' && typeof injected.setRefreshHandler === 'function') {
365
+ // This version supports React Refresh.
366
+ helpersByRendererID.set(id, injected);
367
+ }
368
+
369
+ return id;
370
+ }; // We also want to track currently mounted roots.
371
+
413
372
 
414
- // We also want to track currently mounted roots.
415
373
  var oldOnCommitFiberRoot = hook.onCommitFiberRoot;
374
+
416
375
  hook.onCommitFiberRoot = function (id, root, maybePriorityLevel, didError) {
417
- var current = root.current;
418
- var alternate = current.alternate;
376
+ var helpers = helpersByRendererID.get(id);
377
+
378
+ if (helpers === undefined) {
379
+ return;
380
+ }
419
381
 
420
- // We need to determine whether this root has just (un)mounted.
382
+ helpersByRoot.set(root, helpers);
383
+ var current = root.current;
384
+ var alternate = current.alternate; // We need to determine whether this root has just (un)mounted.
421
385
  // This logic is copy-pasted from similar logic in the DevTools backend.
422
386
  // If this breaks with some refactoring, you'll want to update DevTools too.
423
387
 
@@ -429,16 +393,18 @@ function injectIntoGlobalHook(globalObject) {
429
393
  // Mount a new root.
430
394
  mountedRoots.add(root);
431
395
  failedRoots.delete(root);
432
- } else if (wasMounted && isMounted) {
433
- // Update an existing root.
396
+ } else if (wasMounted && isMounted) {// Update an existing root.
434
397
  // This doesn't affect our mounted root Set.
435
398
  } else if (wasMounted && !isMounted) {
436
399
  // Unmount an existing root.
437
400
  mountedRoots.delete(root);
401
+
438
402
  if (didError) {
439
403
  // We'll remount it on future edits.
440
404
  // Remember what was rendered so we can restore it.
441
405
  failedRoots.set(root, alternate.memoizedState.element);
406
+ } else {
407
+ helpersByRoot.delete(root);
442
408
  }
443
409
  } else if (!wasMounted && !isMounted) {
444
410
  if (didError && !failedRoots.has(root)) {
@@ -463,19 +429,15 @@ function injectIntoGlobalHook(globalObject) {
463
429
  };
464
430
  }
465
431
  }
466
-
467
432
  function hasUnrecoverableErrors() {
468
433
  return didSomeRootFailOnMount;
469
- }
434
+ } // Exposed for testing.
470
435
 
471
- // Exposed for testing.
472
436
  function _getMountedRootCount() {
473
437
  {
474
438
  return mountedRoots.size;
475
439
  }
476
- }
477
-
478
- // This is a wrapper over more primitive functions for setting signature.
440
+ } // This is a wrapper over more primitive functions for setting signature.
479
441
  // Signatures let us decide whether the Hook order has changed on refresh.
480
442
  //
481
443
  // This function is intended to be used as a transform target, e.g.:
@@ -497,11 +459,12 @@ function _getMountedRootCount() {
497
459
  // 'useState{[foo, setFoo]}(0)',
498
460
  // () => [useCustomHook], /* Lazy to avoid triggering inline requires */
499
461
  // );
462
+
500
463
  function createSignatureFunctionForTransform() {
501
464
  {
502
465
  var call = 0;
503
- var savedType = void 0;
504
- var hasCustomHooks = void 0;
466
+ var savedType;
467
+ var hasCustomHooks;
505
468
  return function (type, key, forceReset, getCustomHooks) {
506
469
  switch (call++) {
507
470
  case 0:
@@ -509,17 +472,19 @@ function createSignatureFunctionForTransform() {
509
472
  hasCustomHooks = typeof getCustomHooks === 'function';
510
473
  setSignature(type, key, forceReset, getCustomHooks);
511
474
  break;
475
+
512
476
  case 1:
513
477
  if (hasCustomHooks) {
514
478
  collectCustomHooksForSignature(savedType);
515
479
  }
480
+
516
481
  break;
517
482
  }
483
+
518
484
  return type;
519
485
  };
520
486
  }
521
487
  }
522
-
523
488
  function isLikelyComponentType(type) {
524
489
  {
525
490
  switch (typeof type) {
@@ -531,23 +496,28 @@ function isLikelyComponentType(type) {
531
496
  // React class.
532
497
  return true;
533
498
  }
499
+
534
500
  var ownNames = Object.getOwnPropertyNames(type.prototype);
501
+
535
502
  if (ownNames.length > 1 || ownNames[0] !== 'constructor') {
536
503
  // This looks like a class.
537
504
  return false;
538
- }
539
- // eslint-disable-next-line no-proto
505
+ } // eslint-disable-next-line no-proto
506
+
507
+
540
508
  if (type.prototype.__proto__ !== Object.prototype) {
541
509
  // It has a superclass.
542
510
  return false;
543
- }
544
- // Pass through.
511
+ } // Pass through.
545
512
  // This looks like a regular function with empty prototype.
546
- }
547
- // For plain functions and arrows, use name as a heuristic.
513
+
514
+ } // For plain functions and arrows, use name as a heuristic.
515
+
516
+
548
517
  var name = type.name || type.displayName;
549
518
  return typeof name === 'string' && /^[A-Z]/.test(name);
550
519
  }
520
+
551
521
  case 'object':
552
522
  {
553
523
  if (type != null) {
@@ -556,12 +526,15 @@ function isLikelyComponentType(type) {
556
526
  case REACT_MEMO_TYPE:
557
527
  // Definitely React components.
558
528
  return true;
529
+
559
530
  default:
560
531
  return false;
561
532
  }
562
533
  }
534
+
563
535
  return false;
564
536
  }
537
+
565
538
  default:
566
539
  {
567
540
  return false;
@@ -586,6 +559,8 @@ var ReactFreshRuntime = Object.freeze({
586
559
  });
587
560
 
588
561
  // This is hacky but makes it work with both Rollup and Jest.
562
+
563
+
589
564
  var runtime = ReactFreshRuntime.default || ReactFreshRuntime;
590
565
 
591
566
  module.exports = runtime;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "keywords": [
5
5
  "react"
6
6
  ],
7
- "version": "0.3.0",
7
+ "version": "0.4.2",
8
8
  "homepage": "https://reactjs.org/",
9
9
  "bugs": "https://github.com/facebook/react/issues",
10
10
  "license": "MIT",