forked from gaearon/react-hot-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (90 loc) · 3.29 KB
/
index.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
'use strict';
var path = require('path'),
SourceNode = require('source-map').SourceNode,
SourceMapConsumer = require('source-map').SourceMapConsumer,
makeIdentitySourceMap = require('./makeIdentitySourceMap');
module.exports = function (source, map) {
if (this.cacheable) {
this.cacheable();
}
var resourcePath = this.resourcePath;
if (/[\\/]webpack[\\/]buildin[\\/]module\.js|[\\/]react-hot-loader[\\/]|[\\/]react[\\/]lib[\\/]/.test(resourcePath)) {
return this.callback(null, source, map);
}
var acceptUpdates = this.query !== '?manual',
filename = path.basename(resourcePath),
separator = '\n\n',
prependText,
appendText,
node,
result;
var reactMountImport;
try {
require('react-dom/lib/ReactMount');
reactMountImport = 'ReactMount = require("react-dom/lib/ReactMount"),';
} catch(e) {
reactMountImport = 'ReactMount = require("react/lib/ReactMount"),';
}
prependText = [
'/* REACT HOT LOADER */',
'if (module.hot) {',
'(function () {',
'var ReactHotAPI = require(' + JSON.stringify(require.resolve('react-hot-api')) + '),',
'RootInstanceProvider = require(' + JSON.stringify(require.resolve('./RootInstanceProvider')) + '),',
reactMountImport,
'React = require("react");',
'module.makeHot = module.hot.data ? module.hot.data.makeHot : ReactHotAPI(function () {',
'return RootInstanceProvider.getRootInstances(ReactMount);',
'}, React);',
'})();',
'}',
'try {',
'(function () {',
].join(' ');
appendText = [
'/* REACT HOT LOADER */',
'}).call(this);',
'} finally {',
'if (module.hot) {',
'(function () {',
'var foundReactClasses = module.hot.data && module.hot.data.foundReactClasses || false;',
'if (module.exports && module.makeHot) {',
'var makeExportsHot = require(' + JSON.stringify(require.resolve('./makeExportsHot')) + ');',
'if (makeExportsHot(module, require("react"))) {',
'foundReactClasses = true;',
'}',
'var shouldAcceptModule = ' + JSON.stringify(acceptUpdates) + ' && foundReactClasses;',
'if (shouldAcceptModule) {',
'module.hot.accept(function (err) {',
'if (err) {',
'console.error("Cannot apply hot update to " + ' + JSON.stringify(filename) + ' + ": " + err.message);',
'}',
'});',
'}',
'}',
'module.hot.dispose(function (data) {',
'data.makeHot = module.makeHot;',
'data.foundReactClasses = foundReactClasses;',
'});',
'})();',
'}',
'}'
].join(' ');
if (this.sourceMap === false) {
return this.callback(null, [
prependText,
source,
appendText
].join(separator));
}
if (!map) {
map = makeIdentitySourceMap(source, this.resourcePath);
}
node = new SourceNode(null, null, null, [
new SourceNode(null, null, this.resourcePath, prependText),
SourceNode.fromStringWithSourceMap(source, new SourceMapConsumer(map)),
new SourceNode(null, null, this.resourcePath, appendText)
]).join(separator);
result = node.toStringWithSourceMap();
this.callback(null, result.code, result.map.toString());
};