@@ -35,27 +35,27 @@ function commitAllHostEffectsReplacement() {
3535 }
3636 recordEffect ( ) ;
3737
38- let { effectTag} = nextEffect ;
38+ const { effectTag } = nextEffect ;
3939
4040 if ( effectTag & ContentReset ) {
4141 commitResetTextContent ( nextEffect ) ;
4242 }
4343
4444 if ( effectTag & Ref ) {
45- let current$$1 = nextEffect . alternate ;
45+ const current$$1 = nextEffect . alternate ;
4646 if ( current$$1 !== null ) {
4747 commitDetachRef ( current$$1 ) ;
4848 }
4949 }
5050
51- let primaryEffectTag = effectTag & ( Placement | Update | Deletion ) ;
51+ const primaryEffectTag = effectTag & ( Placement | Update | Deletion ) ;
5252 switch ( primaryEffectTag ) {
5353 case Placement :
5454 {
5555 // editbyme
5656 timeTravelLList . append ( {
57- primaryEffectTag : 'PLACEMENT' ,
58- effect : _ . cloneDeep ( nextEffect ) ,
57+ primaryEffectTag : 'PLACEMENT' ,
58+ effect : _ . cloneDeep ( nextEffect ) ,
5959 } ) ;
6060
6161 commitPlacement ( nextEffect ) ;
@@ -67,7 +67,7 @@ function commitAllHostEffectsReplacement() {
6767 {
6868 commitPlacement ( nextEffect ) ;
6969 nextEffect . effectTag &= ~ Placement ;
70- let _current = nextEffect . alternate ;
70+ const _current = nextEffect . alternate ;
7171 commitWork ( _current , nextEffect ) ;
7272 break ;
7373 }
@@ -80,7 +80,7 @@ function commitAllHostEffectsReplacement() {
8080 current : _ . cloneDeep ( nextEffect . alternate ) ,
8181 } ) ;
8282
83- let _current2 = nextEffect . alternate ;
83+ const _current2 = nextEffect . alternate ;
8484 commitWork ( _current2 , nextEffect ) ;
8585 break ;
8686 }
@@ -106,17 +106,22 @@ function commitAllHostEffectsReplacement() {
106106// regex method signatures
107107const uRsig = new RegExp ( / \b ( u s e R e d u c e r ) \b \( r e d u c e r , i n i t i a l A r g , i n i t \) / ) ;
108108const cAHEsig = new RegExp ( / \b ( f u n c t i o n ) \b \s \b ( c o m m i t A l l H o s t E f f e c t s ) \b \( \) / , 'g' ) ;
109-
110- // get replacer method bodies
109+ // method names
110+ const USEREDUCER = 'useReducer' ;
111+ const COMMITALLHOSTEFFECTS = 'commitAllHostEffects' ;
112+ // library key inside of bundle
113+ const reactLibraryPath = './node_modules/react/cjs/react.development.js' ;
114+ const reactDOMLibraryPath = './node_modules/react-dom/cjs/react-dom.development.js' ;
115+ // get replacer method
111116let injectableUseReducer = esprima . parseScript ( useReducerReplacement . toString ( ) ) ;
112- let injectableUseReducerString = escodegen . generate ( injectableUseReducer . body [ 0 ] . body ) ;
117+ const injectableUseReducerString = escodegen . generate ( injectableUseReducer . body [ 0 ] . body ) ;
113118
114119let injectableCommitAllHostEffects = esprima . parseScript ( commitAllHostEffectsReplacement . toString ( ) ) ;
115- let injectableCommitAllHostEffectsString = escodegen . generate ( injectableCommitAllHostEffects . body [ 0 ] . body ) ;
120+ const injectableCommitAllHostEffectsString = escodegen . generate ( injectableCommitAllHostEffects . body [ 0 ] . body ) ;
116121
117122// traverse ast to find method and replace body with our node's body
118123function traverseTree ( replacementNode , functionName , ast ) {
119- console . log ( 'traverse called' ) ;
124+ console . log ( 'unbundled traverse called' ) ;
120125 estraverse . replace ( ast , {
121126 enter ( node ) {
122127 if ( node . type === 'FunctionDeclaration' ) {
@@ -129,7 +134,7 @@ function traverseTree(replacementNode, functionName, ast) {
129134 } ) ;
130135}
131136function stringParser ( string , newBody , methodSig ) {
132- let stack = [ ] ;
137+ const stack = [ ] ;
133138 const foundMethod = methodSig . test ( string ) ;
134139 let oldBody = '' ;
135140 let output ;
@@ -151,32 +156,62 @@ function stringParser(string, newBody, methodSig) {
151156 }
152157 return output ;
153158}
159+ function traverseBundledTree ( replacementNode , functionName , ast , library ) {
160+ estraverse . replace ( ast , {
161+ enter ( node ) {
162+ if ( node . key && node . key . value === library ) {
163+ if ( node . value . body . body [ 1 ] . type === 'ExpressionStatement' ) {
164+ if ( node . value . body . body [ 1 ] . expression . callee . name === 'eval' ) {
165+ // create new ast
166+ // const reactLib = esprima.parseScript(node.value.body.body[1].expression.arguments[0].value, { range: true, tokens: true, comment: true });
167+ const reactLib = esprima . parseScript ( node . value . body . body [ 1 ] . expression . arguments [ 0 ] . value ) ;
168+ estraverse . replace ( reactLib , {
169+ enter ( libNode ) {
170+ if ( libNode . type === 'FunctionDeclaration' ) {
171+ if ( libNode . id . name === functionName ) {
172+ libNode . body = replacementNode . body [ 0 ] . body ;
173+ console . log ( 'From parser. REPLACING!' , libNode . id . name ) ;
174+ // return libNode;
175+ }
176+ }
177+ } ,
178+ } ) ;
179+ // reactLib = escodegen.attachComments(reactLib, reactLib.comments, reactLib.tokens);
180+ node . value . body . body [ 1 ] . expression . arguments = escodegen . generate ( reactLib ) ;
181+ console . log ( 'generated' ) ;
182+ // node.value.body.body[1].expression.arguments[0].value = escodegen.generate(reactLib, { comment: true });
183+ // node.value.body.body[1].expression.arguments[0].value = stringParser(node.value.body.body[1].expression.arguments[0].value, replacementNode, functionName);
184+ }
185+ }
186+ }
187+ } ,
188+ } ) ;
189+ }
190+
154191const parseAndGenerate = ( codeString ) => {
155192 if ( codeString . search ( 'react' ) !== - 1 ) {
156- let ast ;
157- try {
158- ast = esprima . parseModule ( codeString ) ;
159- } catch ( error ) {
160- // esprima throws parsing error webpack devtool setting generates code
161- console . log ( 'unable to use esprima parser' ) ;
162- codeString = stringParser ( codeString , injectableUseReducerString , uRsig ) ;
163- codeString = stringParser ( codeString , injectableCommitAllHostEffectsString , cAHEsig ) ;
164- return codeString ;
193+ const ast = esprima . parseModule ( codeString ) ;
194+ // Webpack bundle is wrapped in function call
195+ if ( ast . body [ 0 ] . expression . type === 'CallExpression' ) {
196+ // if (ast.body[0].expression.arguments[0].properties[6].key.value ==='./node_modules/react/cjs/react.development.js'){
197+ // const reactLib = esprima.parseModule(ast.body[0].expression.arguments[0].properties[6].value.body.body[1].expression.arguments[0].value);
198+ // .value at end is a string
199+ traverseBundledTree ( injectableUseReducer , USEREDUCER , ast , reactLibraryPath ) ;
200+ traverseBundledTree ( injectableCommitAllHostEffects , COMMITALLHOSTEFFECTS , ast , reactDOMLibraryPath ) ;
201+ // traverseBundledTree(injectableUseReducerString, uRsig, ast, reactLibraryPath);
202+ // traverseBundledTree(injectableCommitAllHostEffectsString, cAHEsig, ast, reactDOMLibraryPath);
203+ } else {
204+ // parse react-dom code
205+ injectableCommitAllHostEffects = esprima . parseScript ( commitAllHostEffectsReplacement . toString ( ) ) ;
206+ traverseTree ( injectableCommitAllHostEffects , 'commitAllHostEffects' , ast ) ;
207+
208+ // parse react code
209+ injectableUseReducer = esprima . parseScript ( useReducerReplacement . toString ( ) ) ;
210+ traverseTree ( injectableUseReducer , 'useReducer' , ast ) ;
165211 }
166- // parse react-dom code
167- injectableCommitAllHostEffects = esprima . parseScript ( commitAllHostEffectsReplacement . toString ( ) ) ;
168- traverseTree ( injectableCommitAllHostEffects , 'commitAllHostEffects' , ast ) ;
169-
170- // parse react code
171- injectableUseReducer = esprima . parseScript ( useReducerReplacement . toString ( ) ) ;
172- traverseTree ( injectableUseReducer , 'useReducer' , ast ) ;
173-
174212 const code = escodegen . generate ( ast ) ;
175213 console . log ( 'returning code.' ) ;
176214 return code ;
177215 }
178- console . log ( 'returning string.' ) ;
179- return codeString ;
180216} ;
181-
182217module . exports = parseAndGenerate ;
0 commit comments