1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | var sourceMap = _interopDefault(require('source-map'));
|
8 | var fs = _interopDefault(require('fs'));
|
9 | var path = _interopDefault(require('path'));
|
10 | var loaderUtils = _interopDefault(require('loader-utils'));
|
11 |
|
12 | var SourceMapGenerator = sourceMap.SourceMapGenerator;
|
13 |
|
14 | function 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 |
|
35 | var makeIdentitySourceMap_1 = makeIdentitySourceMap;
|
36 |
|
37 | var 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 |
|
44 | var 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&¤t$$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&¤t.type===element.type)', 'if (current!== null&&hotCompareElements(current.type,element.type,hotUpdateChild(current)))']
|
49 | };
|
50 |
|
51 | var 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 |
|
53 | var defaultEnd = ['var ReactDOM = {', ReactHotLoaderInjection];
|
54 |
|
55 | var defaultEndCompact = ['var ReactDOM={', ReactHotLoaderInjection];
|
56 |
|
57 | var injectionEnd = {
|
58 | '16.6': defaultEnd,
|
59 | '16.4': defaultEnd,
|
60 | '16.6-compact': defaultEndCompact,
|
61 | '16.4-compact': defaultEndCompact
|
62 | };
|
63 |
|
64 | var sign = '/* 🔥 this is hot-loader/react-dom 🔥 */';
|
65 |
|
66 | function 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 |
|
73 | function transform(source) {
|
74 | if (source.indexOf('reconcileSingleElement') < 0) {
|
75 |
|
76 | return source;
|
77 | }
|
78 | if (source.indexOf(sign) >= 0) {
|
79 |
|
80 | return source;
|
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 |
|
91 | var patch = transform;
|
92 |
|
93 | var SourceNode = sourceMap.SourceNode,
|
94 | SourceMapConsumer = sourceMap.SourceMapConsumer;
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | var tagCommonJSExportsSource = null;
|
101 |
|
102 | function 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 |
|
122 |
|
123 |
|
124 | if (!tagCommonJSExportsSource) {
|
125 | tagCommonJSExportsSource = fs.readFileSync(path.join(__dirname, 'webpackTagCommonJSExports.js'), 'utf8')
|
126 |
|
127 |
|
128 | .replace(/['"]use strict['"];/, '')
|
129 |
|
130 | .replace(/\/\/ eslint-disable-line .*\n/g, '\n').replace(/\/\* global.*\*\//, '').split(/\n\s*/).join(' ');
|
131 | }
|
132 |
|
133 |
|
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);
|
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 |
|
152 | transform$1.patch = patch;
|
153 |
|
154 | var webpack = transform$1;
|
155 |
|
156 | exports.default = webpack;
|