UNPKG

7.79 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
6
7var sourceMap = _interopDefault(require('source-map'));
8var fs = _interopDefault(require('fs'));
9var path = _interopDefault(require('path'));
10var loaderUtils = _interopDefault(require('loader-utils'));
11
12var SourceMapGenerator = sourceMap.SourceMapGenerator;
13
14function makeIdentitySourceMap(content, resourcePath) {
15 var map = new SourceMapGenerator();
16 map.setSourceContent(resourcePath, content);
17
18 content.split('\n').forEach(function (line, index) {
19 map.addMapping({
20 source: resourcePath,
21 original: {
22 line: index + 1,
23 column: 0
24 },
25 generated: {
26 line: index + 1,
27 column: 0
28 }
29 });
30 });
31
32 return map.toJSON();
33}
34
35var makeIdentitySourceMap_1 = makeIdentitySourceMap;
36
37var injectionStart = {
38 '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))'],
39 '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))'],
40 '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)) {'],
41 '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))']
42};
43
44var additional = {
45 '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)) {'],
46 '16.6-update-compact': ['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))'],
47 '16.4-update': ['if (current !== null && current.type === element.type) {', 'if (current !== null && hotCompareElements(current.type, element.type, hotUpdateChild(current),current.type)) {'],
48 '16.4-update-compact': ['if (current!== null&&current.type===element.type)', 'if (current!== null&&hotCompareElements(current.type,element.type,hotUpdateChild(current)))']
49};
50
51var ReactHotLoaderInjection = '\nvar hotUpdateChild = function (child) {\n return function (newType) {\n child.type = newType;\n if (child.alternate) {\n child.alternate.type = newType;\n }\n }\n};\nvar hotCompareElements = function (oldType, newType) {\n return oldType === newType\n};\nvar hotCleanupHooks = function () {\n if (typeof resetHooks !== \'undefined\') {\n resetHooks();\n }\n}\nvar ReactDOM = {\n evalInReactContext: function (injection) {\n return eval(injection);\n },\n hotCleanup: hotCleanupHooks,\n hotRenderWithHooks: function (current, render) { \n hotCleanupHooks();\n \n if (typeof nextCurrentHook !== \'undefined\' && typeof ReactCurrentDispatcher$1 !== \'undefined\') { \n nextCurrentHook = current !== null ? current.memoizedState : null;\n if(typeof firstCurrentHook !== \'undefined\') {\n firstCurrentHook = nextCurrentHook;\n }\n \n ReactCurrentDispatcher$1.current = nextCurrentHook === null ? HooksDispatcherOnMountInDEV : HooksDispatcherOnUpdateInDEV;\n }\n \n var rendered = render();\n \n hotCleanupHooks();\n \n return rendered;\n },\n setHotElementComparator: function (newComparator) {\n hotCompareElements = newComparator\n },\n';
52
53var defaultEnd = ['var ReactDOM = {', ReactHotLoaderInjection];
54
55var defaultEndCompact = ['var ReactDOM={', ReactHotLoaderInjection];
56
57var injectionEnd = {
58 '16.6': defaultEnd,
59 '16.4': defaultEnd,
60 '16.6-compact': defaultEndCompact,
61 '16.4-compact': defaultEndCompact
62};
63
64var sign = '/* 🔥 this is hot-loader/react-dom 🔥 */';
65
66function additionalTransform(source) {
67 for (var key in additional) {
68 source = source.split(additional[key][0]).join(additional[key][1]);
69 }
70 return source;
71}
72
73function transform(source) {
74 if (source.indexOf('reconcileSingleElement') < 0) {
75 // early reject
76 return source;
77 }
78 if (source.indexOf(sign) >= 0) {
79 // already patched
80 return;
81 }
82 for (var key in injectionStart) {
83 if (source.indexOf(injectionStart[key][0]) > 0 && source.indexOf(injectionEnd[key][0]) > 0) {
84 var result = additionalTransform(source.replace(injectionStart[key][0], injectionStart[key][1]).replace(injectionEnd[key][0], injectionEnd[key][1]));
85 return sign + '\n' + result + '\n' + sign;
86 }
87 }
88 return source;
89}
90
91var patch = transform;
92
93var SourceNode = sourceMap.SourceNode,
94 SourceMapConsumer = sourceMap.SourceMapConsumer;
95
96
97
98
99
100var tagCommonJSExportsSource = null;
101
102function transform$1(source, map) {
103 var _this = this;
104
105 var callback = this.async();
106 if (source && source.types && source.types.IfStatement) {
107 throw new Error('React Hot Loader: You are erroneously trying to use a Webpack loader ' + 'as a Babel plugin. Replace "react-hot-loader/webpack" with ' + '"react-hot-loader/babel" in the "plugins" section of your .babelrc file. ' + 'While we recommend the above, if you prefer not to use Babel, ' + 'you may remove "react-hot-loader/webpack" from the "plugins" section of ' + 'your .babelrc file altogether, and instead add "react-hot-loader/webpack" ' + 'to the "loaders" section of your Webpack configuration.');
108 }
109
110 if (this.cacheable) {
111 this.cacheable();
112 }
113
114 var options = Object.assign({ withPatch: true }, loaderUtils.getOptions(this));
115 if (options.withPatch) {
116 source = patch(source);
117 }
118 if (source.indexOf('reactHotLoader.register') > 0) {
119 return callback(null, source, map);
120 }
121 // This is a Webpack loader, but the user put it in the Babel config.
122
123 // Read the helper once.
124 if (!tagCommonJSExportsSource) {
125 tagCommonJSExportsSource = fs.readFileSync(path.join(__dirname, 'webpackTagCommonJSExports.js'), 'utf8')
126 // Babel inserts these.
127 // Ideally we'd opt out for one file but this is simpler.
128 .replace(/['"]use strict['"];/, '')
129 // eslint comments don't need to end up in the output
130 .replace(/\/\/ eslint-disable-line .*\n/g, '\n').replace(/\/\* global.*\*\//, '').split(/\n\s*/).join(' ');
131 }
132
133 // Parameterize the helper with the current filename.
134 var separator = '\n\n;';
135 var appendText = tagCommonJSExportsSource.replace(/__FILENAME__/g, JSON.stringify(this.resourcePath));
136
137 if (this.sourceMap === false) {
138 return callback(null, [source, appendText].join(separator));
139 }
140
141 if (!map) {
142 map = makeIdentitySourceMap_1(source, this.resourcePath); // eslint-disable-line no-param-reassign
143 }
144 var sourceMapConsumer = new SourceMapConsumer(map);
145 sourceMapConsumer.then(function (consumedMap) {
146 var node = new SourceNode(null, null, null, [SourceNode.fromStringWithSourceMap(source, consumedMap), new SourceNode(null, null, _this.resourcePath, appendText)]).join(separator);
147 var result = node.toStringWithSourceMap();
148 callback(null, result.code, result.map.toJSON() || undefined);
149 });
150}
151
152transform$1.patch = patch;
153
154var webpack = transform$1;
155
156exports.default = webpack;