|
| 1 | +// function parser() { |
| 2 | +const esprima = require('esprima'); |
| 3 | +const estraverse = require('estraverse'); |
| 4 | +const escodegen = require('escodegen'); |
| 5 | +const path = require('path'); |
| 6 | +const _ = require('lodash'); |
| 7 | +const fs = require('fs'); |
| 8 | +const fetch = require('isomorphic-fetch'); |
| 9 | + |
| 10 | + |
| 11 | +// react files |
| 12 | +const useReducerfile = '/node_modules/react/cjs/react.development.js'; |
| 13 | +const commitAllHostEffectsfile = '/node_modules/react-dom/cjs/react-dom.development.js'; |
| 14 | + |
| 15 | +// generated file names |
| 16 | +const genReactDev = 'generatedReact.development.js'; |
| 17 | +const genReactDom = 'generatedReact-dom.development.js'; |
| 18 | + |
| 19 | +// convert file to string and parse |
| 20 | +function parseFile(file) { |
| 21 | + const dir = path.join(__dirname, file); |
| 22 | + const fileString = fs.readFile(dir, { encoding: 'utf-8' }); |
| 23 | + const ast = esprima.parseModule(fileString); |
| 24 | + return ast; |
| 25 | +} |
| 26 | +// declare functions to insert |
| 27 | +// TODO: Un-comment timeTravelTracker |
| 28 | +function useReducerReplacement() { |
| 29 | + const dispatcher = resolveDispatcher(); |
| 30 | + function reducerWithTracker(state, action) { |
| 31 | + const newState = reducer(state, action); |
| 32 | + // timeTravelTracker[timeTravelTracker.length - 1].actionDispatched = true; |
| 33 | + window.postMessage({ |
| 34 | + type: 'DISPATCH', |
| 35 | + data: { |
| 36 | + state: newState, |
| 37 | + action, |
| 38 | + }, |
| 39 | + }); |
| 40 | + return newState; |
| 41 | + } |
| 42 | + return dispatcher.useReducer(reducerWithTracker, initialArg, init); |
| 43 | +} |
| 44 | +function commitAllHostEffectsReplacement() { |
| 45 | + while (nextEffect !== null) { |
| 46 | + { |
| 47 | + setCurrentFiber(nextEffect); |
| 48 | + } |
| 49 | + recordEffect(); |
| 50 | + |
| 51 | + let {effectTag} = nextEffect; |
| 52 | + |
| 53 | + if (effectTag & ContentReset) { |
| 54 | + commitResetTextContent(nextEffect); |
| 55 | + } |
| 56 | + |
| 57 | + if (effectTag & Ref) { |
| 58 | + let current$$1 = nextEffect.alternate; |
| 59 | + if (current$$1 !== null) { |
| 60 | + commitDetachRef(current$$1); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // The following switch statement is only concerned about placement, |
| 65 | + // updates, and deletions. To avoid needing to add a case for every |
| 66 | + // possible bitmap value, we remove the secondary effects from the |
| 67 | + // effect tag and switch on that value. |
| 68 | + let primaryEffectTag = effectTag & (Placement | Update | Deletion); |
| 69 | + switch (primaryEffectTag) { |
| 70 | + case Placement: |
| 71 | + { |
| 72 | + // editbyme |
| 73 | + window.postMessage({ |
| 74 | + type: 'EFFECT', |
| 75 | + data: { |
| 76 | + primaryEffectTag: 'PLACEMENT', |
| 77 | + effect: _.cloneDeep(nextEffect), |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + commitPlacement(nextEffect); |
| 82 | + // Clear the "placement" from effect tag so that we know that this is inserted, before |
| 83 | + // any life-cycles like componentDidMount gets called. |
| 84 | + // TODO: findDOMNode doesn't rely on this any more but isMounted |
| 85 | + // does and isMounted is deprecated anyway so we should be able |
| 86 | + // to kill this. |
| 87 | + nextEffect.effectTag &= ~Placement; |
| 88 | + break; |
| 89 | + } |
| 90 | + case PlacementAndUpdate: |
| 91 | + { |
| 92 | + // Placement |
| 93 | + commitPlacement(nextEffect); |
| 94 | + // Clear the "placement" from effect tag so that we know that this is inserted, before |
| 95 | + // any life-cycles like componentDidMount gets called. |
| 96 | + nextEffect.effectTag &= ~Placement; |
| 97 | + |
| 98 | + // Update |
| 99 | + let _current = nextEffect.alternate; |
| 100 | + commitWork(_current, nextEffect); |
| 101 | + break; |
| 102 | + } |
| 103 | + case Update: |
| 104 | + { |
| 105 | + // editbyme |
| 106 | + window.postMessage({ |
| 107 | + type: 'EFFECT', |
| 108 | + data: { |
| 109 | + primaryEffectTag: 'UPDATE', |
| 110 | + effect: _.cloneDeep(nextEffect), |
| 111 | + current: _.cloneDeep(nextEffect.alternate), |
| 112 | + }, |
| 113 | + }); |
| 114 | + |
| 115 | + let _current2 = nextEffect.alternate; |
| 116 | + commitWork(_current2, nextEffect); |
| 117 | + break; |
| 118 | + } |
| 119 | + case Deletion: |
| 120 | + { |
| 121 | + // editbyme |
| 122 | + window.postMessage({ |
| 123 | + type: 'EFFECT', |
| 124 | + data: { |
| 125 | + primaryEffectTag: 'DELETION', |
| 126 | + effect: _.cloneDeep(nextEffect), |
| 127 | + }, |
| 128 | + }); |
| 129 | + |
| 130 | + commitDeletion(nextEffect); |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + nextEffect = nextEffect.nextEffect; |
| 135 | + } |
| 136 | + |
| 137 | + { |
| 138 | + resetCurrentFiber(); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// traverse ast to find method and replace body with our node's body |
| 143 | +function traverseTree(replacementNode, functionName, ast) { |
| 144 | + // let completed = false; |
| 145 | + estraverse.replace(ast, { |
| 146 | + enter(node) { |
| 147 | + if (node.type === 'FunctionDeclaration') { |
| 148 | + if (node.id.name === functionName) { |
| 149 | + console.log('found', functionName); |
| 150 | + node.body = replacementNode.body[0].body; |
| 151 | + // completed = true; |
| 152 | + } |
| 153 | + } |
| 154 | + }, |
| 155 | + }); |
| 156 | + // return completed; |
| 157 | +} |
| 158 | + |
| 159 | +function generateFile(filename, ast) { |
| 160 | + const code = escodegen.generate(ast); |
| 161 | + fs.writeFileSync(filename, code, (err) => { |
| 162 | + if (err) throw new Error(err.message); |
| 163 | + }); |
| 164 | +} |
| 165 | + |
| 166 | +const parseAndGenerate = () => { |
| 167 | + // first file |
| 168 | + // let ast = parseFile(useReducerfile); |
| 169 | + fetch('https://unpkg.com/react-dom@16/umd/react-dom.development.js') |
| 170 | + .then(r => r.text()) |
| 171 | + .then((codeString) => { |
| 172 | + console.log(codeString); |
| 173 | + if (codeString.search('react')) { |
| 174 | + console.log('react result', codeString.search('react')); |
| 175 | + const ast = esprima.parseModule(codeString); |
| 176 | + if (codeString.search('react-dom')) { |
| 177 | + console.log('react-DOM result', codeString.search('react-dom')); |
| 178 | + const injectableCommitAllHostEffects = esprima.parseScript(commitAllHostEffectsReplacement.toString()); |
| 179 | + traverseTree(injectableCommitAllHostEffects, 'commitAllHostEffects', ast); |
| 180 | + } else { |
| 181 | + const injectableUseReducer = esprima.parseScript(useReducerReplacement.toString()); |
| 182 | + traverseTree(injectableUseReducer, 'useReducer', ast); |
| 183 | + } |
| 184 | + const code = escodegen.generate(ast); |
| 185 | + // console.log(code); |
| 186 | + // return code; |
| 187 | + fs.writeFileSync('dom.js', code, (err) => { |
| 188 | + if (err) throw new Error(err.message); |
| 189 | + }); |
| 190 | + } |
| 191 | + return -1; |
| 192 | + }); |
| 193 | +}; |
| 194 | +parseAndGenerate(); |
| 195 | + |
| 196 | +// } |
| 197 | +module.exports = parseAndGenerate; |
0 commit comments