forked from gaearon/react-hot-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeExportsHot.js
54 lines (43 loc) · 1.65 KB
/
makeExportsHot.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
'use strict';
var isReactClassish = require('./isReactClassish'),
isReactElementish = require('./isReactElementish');
function makeExportsHot(m, React) {
if (isReactElementish(m.exports, React)) {
// React elements are never valid React classes
return false;
}
var freshExports = m.exports,
exportsReactClass = isReactClassish(m.exports, React),
foundReactClasses = false;
if (exportsReactClass) {
m.exports = m.makeHot(m.exports, '__MODULE_EXPORTS');
foundReactClasses = true;
}
for (var key in m.exports) {
if (!Object.prototype.hasOwnProperty.call(freshExports, key)) {
continue;
}
if (exportsReactClass && key === 'type') {
// React 0.12 also puts classes under `type` property for compat.
// Skip to avoid updating twice.
continue;
}
var value;
try {
value = freshExports[key];
} catch (err) {
continue;
}
if (!isReactClassish(value, React)) {
continue;
}
if (Object.getOwnPropertyDescriptor(m.exports, key).writable) {
m.exports[key] = m.makeHot(value, '__MODULE_EXPORTS_' + key);
foundReactClasses = true;
} else {
console.warn("Can't make class " + key + " hot reloadable due to being read-only. To fix this you can try two solutions. First, you can exclude files or directories (for example, /node_modules/) using 'exclude' option in loader configuration. Second, if you are using Babel, you can enable loose mode for `es6.modules` using the 'loose' option. See: http://babeljs.io/docs/advanced/loose/ and http://babeljs.io/docs/usage/options/");
}
}
return foundReactClasses;
}
module.exports = makeExportsHot;