-
Notifications
You must be signed in to change notification settings - Fork 802
/
patch.js
142 lines (126 loc) · 5.03 KB
/
patch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const injectionStart = {
'16.6': [
'if (child.tag === Fragment ? element.type === REACT_FRAGMENT_TYPE : child.elementType === element.type)',
'if (child.tag === Fragment ? element.type === REACT_FRAGMENT_TYPE : hotCompareElements(child.elementType, element.type, hotUpdateChild(child), child.type))',
],
'16.6-compact': [
'if(child.tag===Fragment?element.type===REACT_FRAGMENT_TYPE:child.elementType===element.type)',
'if(child.tag===Fragment?element.type===REACT_FRAGMENT_TYPE:hotCompareElements(child.elementType,element.type, hotUpdateChild(child), child.type))',
],
'16.4': [
'if (child.tag === Fragment ? element.type === REACT_FRAGMENT_TYPE : child.type === element.type) {',
'if (child.tag === Fragment ? element.type === REACT_FRAGMENT_TYPE : hotCompareElements(child.type, element.type, hotUpdateChild(child), child.type)) {',
],
'16.4-compact': [
'if(child.tag===Fragment?element.type===REACT_FRAGMENT_TYPE:child.type===element.type)',
'if(child.tag===Fragment?element.type===REACT_FRAGMENT_TYPE:hotCompareElements(child.type,element.type, hotUpdateChild(child), child.type))',
],
};
const additional = {
'16.6-update': [
'if (current$$1 !== null && current$$1.elementType === element.type) {',
'if (current$$1 !== null && hotCompareElements(current$$1.elementType, element.type, hotUpdateChild(current$$1),current$$1.type)) {',
],
'16.6-update-compact': [
'if(current$$1!==null&¤t$$1.elementType===element.type)',
'if(current$$1!==null&&hotCompareElements(current$$1.elementType,element.type,hotUpdateChild(current$$1),current$$1.type))',
],
'16.4-update': [
'if (current !== null && current.type === element.type) {',
'if (current !== null && hotCompareElements(current.type, element.type, hotUpdateChild(current),current.type)) {',
],
'16.4-update-compact': [
'if (current!== null&¤t.type===element.type)',
'if (current!== null&&hotCompareElements(current.type,element.type,hotUpdateChild(current)))',
],
'16.8-type': [
'function createFiberFromTypeAndProps(type, // React$ElementType\nkey, pendingProps, owner, mode, expirationTime) {',
'function createFiberFromTypeAndProps(type, // React$ElementType\nkey, pendingProps, owner, mode, expirationTime) {type = hotResolveType(type);',
],
'16.8-type-compact': [
'function createFiberFromTypeAndProps(type,// React$ElementType\nkey,pendingProps,owner,mode,expirationTime){',
'function createFiberFromTypeAndProps(type,// React$ElementType\nkey,pendingProps,owner,mode,expirationTime){type = hotResolveType(type);',
]
};
const ReactHotLoaderInjection = `
var hotUpdateChild = function (child) {
return function (newType) {
child.type = newType;
if (child.alternate) {
child.alternate.type = newType;
}
}
};
var hotResolveType = function (type) {
return type;
};
var hotCompareElements = function (oldType, newType) {
return oldType === newType
};
var hotCleanupHooks = function () {
if (typeof resetHooks !== 'undefined') {
resetHooks();
}
}
var ReactDOM = {
evalInReactContext: function (injection) {
return eval(injection);
},
hotCleanup: hotCleanupHooks,
hotRenderWithHooks: function (current, render) {
hotCleanupHooks();
if (typeof nextCurrentHook !== 'undefined' && typeof ReactCurrentDispatcher$1 !== 'undefined') {
nextCurrentHook = current !== null ? current.memoizedState : null;
if(typeof firstCurrentHook !== 'undefined') {
firstCurrentHook = nextCurrentHook;
}
ReactCurrentDispatcher$1.current = nextCurrentHook === null ? HooksDispatcherOnMountInDEV : HooksDispatcherOnUpdateInDEV;
}
var rendered = render();
hotCleanupHooks();
return rendered;
},
setHotElementComparator: function (newComparator) {
hotCompareElements = newComparator
},
setHotTypeResolver: function (newResolver) {
hotResolveType = newResolver;
},
`;
const defaultEnd = ['var ReactDOM = {', ReactHotLoaderInjection];
const defaultEndCompact = ['var ReactDOM={', ReactHotLoaderInjection];
const injectionEnd = {
'16.6': defaultEnd,
'16.4': defaultEnd,
'16.6-compact': defaultEndCompact,
'16.4-compact': defaultEndCompact,
};
const sign = '/* 🔥 this is hot-loader/react-dom 4.8+ 🔥 */';
function additionalTransform(source) {
for (const key in additional) {
source = source.split(additional[key][0]).join(additional[key][1]);
}
return source;
}
function transform(source) {
if (source.indexOf('reconcileSingleElement') < 0) {
// early reject
return source;
}
if (source.indexOf(sign) >= 0) {
// already patched
return source;
}
for (const key in injectionStart) {
if (source.indexOf(injectionStart[key][0]) > 0 && source.indexOf(injectionEnd[key][0]) > 0) {
const result = additionalTransform(
source
.replace(injectionStart[key][0], injectionStart[key][1])
.replace(injectionEnd[key][0], injectionEnd[key][1]),
);
return `${sign}\n${result}\n${sign}`;
}
}
return source;
}
module.exports = transform;