From 5273e0669f9c57ad86feeb63187121d3392f2f4a Mon Sep 17 00:00:00 2001 From: Anton Korzunov Date: Tue, 12 Feb 2019 18:28:46 +1100 Subject: [PATCH] move patch to the React-Hot-Loader --- package.json | 1 + patch/build.js | 2 +- patch/patch.js | 105 ------------------------------------------------- 3 files changed, 2 insertions(+), 106 deletions(-) delete mode 100644 patch/patch.js diff --git a/package.json b/package.json index e45e755..096c3ea 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "react-dom": "^16.8.1" }, "dependencies": { + "react-hot-loader":"^4.7.0", "loose-envify": "^1.1.0", "object-assign": "^4.1.1", "prop-types": "^15.6.2", diff --git a/patch/build.js b/patch/build.js index 4dcb29f..c54aa5d 100644 --- a/patch/build.js +++ b/patch/build.js @@ -1,7 +1,7 @@ const {writeFileSync, readFileSync, readdirSync, statSync, mkdirSync} = require('fs'); const pkg = require('react-dom/package'); const self = require('../source/package.base'); -const patch = require('./patch'); +const {default: {patch}} = require('react-hot-loader/webpack'); let patchedFiles = []; diff --git a/patch/patch.js b/patch/patch.js deleted file mode 100644 index 59e2dc6..0000000 --- a/patch/patch.js +++ /dev/null @@ -1,105 +0,0 @@ -const injectionStart = { - '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, updateChild(child), child.type))' - ], - '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, updateChild(child), child.type))' - ], - '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, updateChild(child), child.type)) {' - ], - '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, updateChild(child), child.type))' - ], -}; - -const additional = { - '16.6-update': [ - 'if (current$$1 !== null && current$$1.elementType === element.type) {', - 'if (current$$1 !== null && hotCompareElements(current$$1.elementType, element.type, updateChild(current$$1),current$$1.type)) {' - ], - '16.6-update-compact': [ - 'if(current$$1!==null&¤t$$1.elementType===element.type)', - 'if(current$$1!==null&&hotCompareElements(current$$1.elementType,element.type,updateChild(current$$1),current$$1.type))' - ], - '16.4-update': [ - 'if (current !== null && current.type === element.type) {', - 'if (current !== null && hotCompareElements(current.type, element.type, updateChild(current),current.type)) {' - ], - '16.4-update-compact': [ - 'if (current!== null&¤t.type===element.type)', - 'if (current!== null&&hotCompareElements(current.type,element.type,updateChild(current)))' - ] -}; - -const defaultEnd = [ - 'var ReactDOM = {', - `var updateChild = function (child) { return function (newType) { - child.type = newType; - if (child.alternate) { - child.alternate.type = newType; - } - }}; - var hotCompareElements = function (oldType, newType) { return oldType === newType }; - var ReactDOM = { - setHotElementComparator: function (newComparator) { hotCompareElements = newComparator }, - ` -]; - -const defaultEndCompact = [ - 'var ReactDOM={', - `var updateChild = function (child) { return function (newType) { - child.type = newType; - if (child.alternate) { - child.alternate.type = newType; - } - }}; - var hotCompareElements = function (oldType, newType) { return oldType === newType }; - var ReactDOM = { - setHotElementComparator: function (newComparator) { hotCompareElements = newComparator }, - ` -]; - - -const injectionEnd = { - '16.6': defaultEnd, - '16.4': defaultEnd, - '16.6-compact': defaultEndCompact, - '16.4-compact': defaultEndCompact, -}; - -const sign = '/* 🔥 this is hot-loader/react-dom 🔥 */'; - -function additionalTransform(source) { - for (const key in additional) { - source = source.split(additional[key][0]).join(additional[key][1]) - } - return source; -} - -function transform(source) { - if (source.indexOf('reconcileSingleElement') < 0) { - // early reject - return source; - } - for (const key in injectionStart) { - if ( - source.indexOf(injectionStart[key][0]) > 0 && - source.indexOf(injectionEnd[key][0]) > 0 - ) { - const result = additionalTransform( - source - .replace(injectionStart[key][0], injectionStart[key][1]) - .replace(injectionEnd[key][0], injectionEnd[key][1]) - ); - return `${sign}\n${result}\n${sign}`; - } - } - return source; -} - -module.exports = transform;