-
Notifications
You must be signed in to change notification settings - Fork 801
/
inject.js
174 lines (164 loc) · 5.11 KB
/
inject.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
isNativeFunction,
reactLifeCycleMountMethods,
safeReactConstructor,
getOwnKeys,
shallowStringsEqual,
deepPrototypeUpdate,
} from './utils'
import { REGENERATE_METHOD, PREFIX, GENERATION } from './constants'
import logger from '../logger'
function mergeComponents(
ProxyComponent,
NextComponent,
InitialComponent,
lastInstance,
injectedMembers,
) {
const injectedCode = {}
try {
const nextInstance = safeReactConstructor(NextComponent, lastInstance)
try {
// Bypass babel class inheritance checking
deepPrototypeUpdate(InitialComponent, NextComponent)
} catch (e) {
// It was ES6 class
}
const proxyInstance = safeReactConstructor(ProxyComponent, lastInstance)
if (!nextInstance || !proxyInstance) {
return injectedCode
}
const mergedAttrs = { ...proxyInstance, ...nextInstance }
const hasRegenerate = proxyInstance[REGENERATE_METHOD]
const ownKeys = getOwnKeys(Object.getPrototypeOf(ProxyComponent.prototype))
Object.keys(mergedAttrs).forEach(key => {
if (key.startsWith(PREFIX)) return
const nextAttr = nextInstance[key]
const prevAttr = proxyInstance[key]
if (prevAttr && nextAttr) {
if (isNativeFunction(nextAttr) || isNativeFunction(prevAttr)) {
// this is bound method
const isSameArity = nextAttr.length === prevAttr.length
const existsInPrototype =
ownKeys.indexOf(key) >= 0 || ProxyComponent.prototype[key]
if (isSameArity && existsInPrototype) {
if (hasRegenerate) {
injectedCode[
key
] = `Object.getPrototypeOf(this)['${key}'].bind(this)`
} else {
logger.warn(
'React Hot Loader:,',
'Non-controlled class',
ProxyComponent.name,
'contains a new native or bound function ',
key,
nextAttr,
'. Unable to reproduce',
)
}
} else {
logger.warn(
'React Hot Loader:',
'Updated class ',
ProxyComponent.name,
'contains native or bound function ',
key,
nextAttr,
'. Unable to reproduce, use arrow functions instead.',
`(arity: ${nextAttr.length}/${prevAttr.length}, proto: ${
existsInPrototype ? 'yes' : 'no'
}`,
)
}
return
}
const nextString = String(nextAttr)
const injectedBefore = injectedMembers[key]
if (
nextString !== String(prevAttr) ||
(injectedBefore && nextString !== String(injectedBefore))
) {
if (!hasRegenerate) {
if (
nextString.indexOf('function') < 0 &&
nextString.indexOf('=>') < 0
) {
// just copy prop over
injectedCode[key] = nextAttr
} else {
logger.warn(
'React Hot Loader:',
' Updated class ',
ProxyComponent.name,
'had different code for',
key,
nextAttr,
'. Unable to reproduce. Regeneration support needed.',
)
}
} else {
injectedCode[key] = nextAttr
}
}
}
})
} catch (e) {
logger.warn('React Hot Loader:', e)
}
return injectedCode
}
function checkLifeCycleMethods(ProxyComponent, NextComponent) {
try {
const p1 = Object.getPrototypeOf(ProxyComponent.prototype)
const p2 = NextComponent.prototype
reactLifeCycleMountMethods.forEach(key => {
const d1 = Object.getOwnPropertyDescriptor(p1, key) || { value: p1[key] }
const d2 = Object.getOwnPropertyDescriptor(p2, key) || { value: p2[key] }
if (!shallowStringsEqual(d1, d2)) {
logger.warn(
'React Hot Loader:',
'You did update',
ProxyComponent.name,
's lifecycle method',
key,
'. Unable to repeat',
)
}
})
} catch (e) {
// Ignore errors
}
}
function inject(target, currentGeneration, injectedMembers) {
if (target[GENERATION] !== currentGeneration) {
const hasRegenerate = !!target[REGENERATE_METHOD]
Object.keys(injectedMembers).forEach(key => {
try {
if (hasRegenerate) {
target[REGENERATE_METHOD](
key,
`(function REACT_HOT_LOADER_SANDBOX () {
var _this = this; // common babel transpile
var _this2 = this; // common babel transpile
var _this3 = this; // common babel transpile
return ${injectedMembers[key]};
}).call(this)`,
)
} else {
target[key] = injectedMembers[key]
}
} catch (e) {
logger.warn(
'React Hot Loader: Failed to regenerate method ',
key,
' of class ',
target,
)
logger.warn('got error', e)
}
})
target[GENERATION] = currentGeneration
}
}
export { mergeComponents, checkLifeCycleMethods, inject }