UNPKG

10.1 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.10': ['if (child.tag === Fragment ? element.type === REACT_FRAGMENT_TYPE : child.elementType === element.type || ( // Keep this check inline so it only runs on the false path:\n isCompatibleFamilyForHotReloading(child, element)))', 'if (child.tag === Fragment ? element.type === REACT_FRAGMENT_TYPE : hotCompareElements(child.elementType, element.type, hotUpdateChild(child), child.type))'],
39 '16.9': ['if (child.tag === Fragment ? element.type === REACT_FRAGMENT_TYPE : child.elementType === element.type || (\n // Keep this check inline so it only runs on the false path:\n isCompatibleFamilyForHotReloading(child, element)))', 'if (child.tag === Fragment ? element.type === REACT_FRAGMENT_TYPE : hotCompareElements(child.elementType, element.type, hotUpdateChild(child), child.type))'],
40 '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))'],
41 '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))'],
42 '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)) {'],
43 '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))']
44};
45
46var additional = {
47 '16.10-update': ['( // Keep this check inline so it only runs on the false path:\n isCompatibleFamilyForHotReloading(current$$1, element)))', '(hotCompareElements(current$$1.elementType, element.type, hotUpdateChild(current$$1), current$$1.type)))'],
48 '16.9-update': ['(\n // Keep this check inline so it only runs on the false path:\n isCompatibleFamilyForHotReloading(current$$1, element)))', '(hotCompareElements(current$$1.elementType, element.type, hotUpdateChild(current$$1), current$$1.type)))'],
49 '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)) {'],
50 '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))'],
51 '16.4-update': ['if (current !== null && current.type === element.type) {', 'if (current !== null && hotCompareElements(current.type, element.type, hotUpdateChild(current),current.type)) {'],
52 '16.4-update-compact': ['if (current!== null&&current.type===element.type)', 'if (current!== null&&hotCompareElements(current.type,element.type,hotUpdateChild(current)))'],
53
54 '16.8-type': ['function createFiberFromTypeAndProps(type, // React$ElementType\nkey, pendingProps, owner, mode, expirationTime) {', 'function createFiberFromTypeAndProps(type, // React$ElementType\nkey, pendingProps, owner, mode, expirationTime) {type = hotResolveType(type);'],
55
56 '16.8-type-compact': ['function createFiberFromTypeAndProps(type,// React$ElementType\nkey,pendingProps,owner,mode,expirationTime){', 'function createFiberFromTypeAndProps(type,// React$ElementType\nkey,pendingProps,owner,mode,expirationTime){type = hotResolveType(type);']
57};
58
59var 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 hotResolveType = function (type) {\n return type;\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 setHotTypeResolver: function (newResolver) {\n hotResolveType = newResolver;\n },\n';
60
61var defaultEnd = ['var ReactDOM = {', ReactHotLoaderInjection];
62
63var defaultEndCompact = ['var ReactDOM={', ReactHotLoaderInjection];
64
65var injectionEnd = {
66 '16.10': defaultEnd,
67 '16.9': defaultEnd,
68 '16.6': defaultEnd,
69 '16.4': defaultEnd,
70 '16.6-compact': defaultEndCompact,
71 '16.4-compact': defaultEndCompact
72};
73
74var sign = '/* 🔥 this is hot-loader/react-dom 4.8+ 🔥 */';
75
76function additionalTransform(source) {
77 for (var key in additional) {
78 source = source.split(additional[key][0]).join(additional[key][1]);
79 }
80 return source;
81}
82
83function transform(source) {
84 if (source.indexOf('reconcileSingleElement') < 0) {
85 // early reject
86 return source;
87 }
88 if (source.indexOf(sign) >= 0) {
89 // already patched
90 return source;
91 }
92 for (var key in injectionStart) {
93 if (source.indexOf(injectionStart[key][0]) > 0 && source.indexOf(injectionEnd[key][0]) > 0) {
94 var result = additionalTransform(source.replace(injectionStart[key][0], injectionStart[key][1]).replace(injectionEnd[key][0], injectionEnd[key][1]));
95 return sign + '\n' + result + '\n' + sign;
96 }
97 }
98 return source;
99}
100
101var patch = transform;
102
103var SourceNode = sourceMap.SourceNode,
104 SourceMapConsumer = sourceMap.SourceMapConsumer;
105
106
107
108
109
110var tagCommonJSExportsSource = null;
111
112function transform$1(source, map) {
113 var callback = this.async();
114 var resourcePath = this.resourcePath;
115 if (source && source.types && source.types.IfStatement) {
116 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.');
117 }
118
119 if (this.cacheable) {
120 this.cacheable();
121 }
122
123 var options = Object.assign({ withPatch: true }, loaderUtils.getOptions(this));
124 if (options.withPatch) {
125 source = patch(source);
126 }
127
128 if (source.indexOf('reactHotLoader.register') > 0 || options.noRegister) {
129 return callback(null, source, map);
130 }
131 // This is a Webpack loader, but the user put it in the Babel config.
132
133 // Read the helper once.
134 if (!tagCommonJSExportsSource) {
135 tagCommonJSExportsSource = fs.readFileSync(path.join(__dirname, 'webpackTagCommonJSExports.js'), 'utf8')
136 // Babel inserts these.
137 // Ideally we'd opt out for one file but this is simpler.
138 .replace(/['"]use strict['"];/, '')
139 // eslint comments don't need to end up in the output
140 .replace(/\/\* (.*) \*\//, '').replace(/\/\/ eslint-disable-line .*\n/g, '\n').replace(/\/\* global.*\*\//, '').split(/\n\s*/).join(' ');
141 }
142
143 // Parameterize the helper with the current filename.
144 var separator = '\n';
145 var appendText = tagCommonJSExportsSource.replace(/__FILENAME__/g, JSON.stringify(resourcePath));
146
147 if (this.sourceMap === false) {
148 return callback(null, [source, appendText].join(separator));
149 }
150
151 if (!map) {
152 map = makeIdentitySourceMap_1(source, resourcePath); // eslint-disable-line no-param-reassign
153 }
154 var sourceMapConsumer = new SourceMapConsumer(map);
155
156 var onSourceMapReady = function onSourceMapReady(consumedMap) {
157 var node = new SourceNode(null, null, null, [SourceNode.fromStringWithSourceMap(source, consumedMap), new SourceNode(null, null, resourcePath, appendText)]).join(separator);
158 var result = node.toStringWithSourceMap();
159 callback(null, result.code, result.map.toJSON() || undefined);
160 };
161
162 if (sourceMapConsumer.then) {
163 sourceMapConsumer.then(onSourceMapReady);
164 } else {
165 onSourceMapReady(sourceMapConsumer);
166 }
167}
168
169transform$1.patch = patch;
170
171var webpack = transform$1;
172
173exports.default = webpack;