Skip to content

Commit f07bd9d

Browse files
committed
Merge remote-tracking branch 'origin/master' into fresh-hooks
2 parents e5c4bb4 + ff7d5c7 commit f07bd9d

File tree

3 files changed

+72
-46
lines changed

3 files changed

+72
-46
lines changed

src/reactHotLoader.js

+51-44
Original file line numberDiff line numberDiff line change
@@ -122,55 +122,62 @@ const reactHotLoader = {
122122

123123
reactHotLoader.IS_REACT_MERGE_ENABLED = true;
124124
configuration.showReactDomPatchNotification = false;
125-
// console.warn('react-🔥-loader activated.');
126-
}
127-
/* eslint-enable */
128-
if (!React.createElement.isPatchedByReactHotLoader) {
129-
const originalCreateElement = React.createElement;
130-
// Trick React into rendering a proxy so that
131-
// its state is preserved when the class changes.
132-
// This will update the proxy if it's for a known type.
133-
React.createElement = (type, ...args) => originalCreateElement(resolveType(type), ...args);
134-
React.createElement.isPatchedByReactHotLoader = true;
125+
126+
if (ReactDOM.setHotTypeResolver) {
127+
configuration.intergratedResolver = true;
128+
ReactDOM.setHotTypeResolver(resolveType);
129+
}
135130
}
136131

137-
if (!React.cloneElement.isPatchedByReactHotLoader) {
138-
const originalCloneElement = React.cloneElement;
139-
140-
React.cloneElement = (element, ...args) => {
141-
const newType = element.type && resolveType(element.type);
142-
if (newType && newType !== element.type) {
143-
return originalCloneElement(
144-
{
145-
...element,
146-
type: newType,
147-
},
148-
...args,
149-
);
150-
}
151-
return originalCloneElement(element, ...args);
152-
};
132+
if (!configuration.intergratedResolver) {
133+
/* eslint-enable */
134+
if (!React.createElement.isPatchedByReactHotLoader) {
135+
const originalCreateElement = React.createElement;
136+
// Trick React into rendering a proxy so that
137+
// its state is preserved when the class changes.
138+
// This will update the proxy if it's for a known type.
139+
React.createElement = (type, ...args) => originalCreateElement(resolveType(type), ...args);
140+
React.createElement.isPatchedByReactHotLoader = true;
141+
}
153142

154-
React.cloneElement.isPatchedByReactHotLoader = true;
155-
}
143+
if (!React.cloneElement.isPatchedByReactHotLoader) {
144+
const originalCloneElement = React.cloneElement;
145+
146+
React.cloneElement = (element, ...args) => {
147+
const newType = element.type && resolveType(element.type);
148+
if (newType && newType !== element.type) {
149+
return originalCloneElement(
150+
{
151+
...element,
152+
type: newType,
153+
},
154+
...args,
155+
);
156+
}
157+
return originalCloneElement(element, ...args);
158+
};
156159

157-
if (!React.createFactory.isPatchedByReactHotLoader) {
158-
// Patch React.createFactory to use patched createElement
159-
// because the original implementation uses the internal,
160-
// unpatched ReactElement.createElement
161-
React.createFactory = type => {
162-
const factory = React.createElement.bind(null, type);
163-
factory.type = type;
164-
return factory;
165-
};
166-
React.createFactory.isPatchedByReactHotLoader = true;
167-
}
160+
React.cloneElement.isPatchedByReactHotLoader = true;
161+
}
168162

169-
if (!React.Children.only.isPatchedByReactHotLoader) {
170-
const originalChildrenOnly = React.Children.only;
171-
// Use the same trick as React.createElement
172-
React.Children.only = children => originalChildrenOnly({ ...children, type: resolveType(children.type) });
173-
React.Children.only.isPatchedByReactHotLoader = true;
163+
if (!React.createFactory.isPatchedByReactHotLoader) {
164+
// Patch React.createFactory to use patched createElement
165+
// because the original implementation uses the internal,
166+
// unpatched ReactElement.createElement
167+
React.createFactory = type => {
168+
const factory = React.createElement.bind(null, type);
169+
factory.type = type;
170+
return factory;
171+
};
172+
React.createFactory.isPatchedByReactHotLoader = true;
173+
}
174+
175+
if (!React.Children.only.isPatchedByReactHotLoader) {
176+
const originalChildrenOnly = React.Children.only;
177+
// Use the same trick as React.createElement
178+
React.Children.only = children => originalChildrenOnly({ ...children, type: resolveType(children.type) });
179+
React.Children.only.isPatchedByReactHotLoader = true;
180+
}
174181
}
175182

176183
if (React.useEffect && !React.useState.isPatchedByReactHotLoader) {

src/reconciler/componentComparator.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { areSwappable } from './utils';
1212
import { PROXY_KEY, UNWRAP_PROXY } from '../proxy';
1313
import { resolveType } from './resolver';
1414
import logger from '../logger';
15+
import configuration from '../configuration';
1516

1617
const getInnerComponentType = component => {
1718
const unwrapper = component[UNWRAP_PROXY];
@@ -137,11 +138,13 @@ const compareComponents = (oldType, newType, setNewType, baseType) => {
137138
const knownPairs = new WeakMap();
138139
const emptyMap = new WeakMap();
139140

140-
export const hotComponentCompare = (oldType, newType, setNewType, baseType) => {
141+
export const hotComponentCompare = (oldType, preNewType, setNewType, baseType) => {
141142
const hotActive = hotComparisonOpen();
143+
const newType = configuration.intergratedResolver ? resolveType(preNewType) : preNewType;
142144
let result = oldType === newType;
143145

144146
if (
147+
result ||
145148
!isReloadableComponent(oldType) ||
146149
!isReloadableComponent(newType) ||
147150
isColdType(oldType) ||

src/webpack/patch.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ const additional = {
3434
'if (current!== null&&current.type===element.type)',
3535
'if (current!== null&&hotCompareElements(current.type,element.type,hotUpdateChild(current)))',
3636
],
37+
38+
'16.8-type': [
39+
'function createFiberFromTypeAndProps(type, // React$ElementType\nkey, pendingProps, owner, mode, expirationTime) {',
40+
'function createFiberFromTypeAndProps(type, // React$ElementType\nkey, pendingProps, owner, mode, expirationTime) {type = hotResolveType(type);',
41+
],
42+
43+
'16.8-type-compact': [
44+
'function createFiberFromTypeAndProps(type,// React$ElementType\nkey,pendingProps,owner,mode,expirationTime){',
45+
'function createFiberFromTypeAndProps(type,// React$ElementType\nkey,pendingProps,owner,mode,expirationTime){type = hotResolveType(type);',
46+
]
3747
};
3848

3949
const ReactHotLoaderInjection = `
@@ -45,6 +55,9 @@ var hotUpdateChild = function (child) {
4555
}
4656
}
4757
};
58+
var hotResolveType = function (type) {
59+
return type;
60+
};
4861
var hotCompareElements = function (oldType, newType) {
4962
return oldType === newType
5063
};
@@ -79,6 +92,9 @@ var ReactDOM = {
7992
setHotElementComparator: function (newComparator) {
8093
hotCompareElements = newComparator
8194
},
95+
setHotTypeResolver: function (newResolver) {
96+
hotResolveType = newResolver;
97+
},
8298
`;
8399

84100
const defaultEnd = ['var ReactDOM = {', ReactHotLoaderInjection];
@@ -92,7 +108,7 @@ const injectionEnd = {
92108
'16.4-compact': defaultEndCompact,
93109
};
94110

95-
const sign = '/* 🔥 this is hot-loader/react-dom 🔥 */';
111+
const sign = '/* 🔥 this is hot-loader/react-dom 4.8+ 🔥 */';
96112

97113
function additionalTransform(source) {
98114
for (const key in additional) {

0 commit comments

Comments
 (0)