|
1 | 1 | const acorn = require('acorn'); |
2 | 2 | const injectJSX = require('acorn-jsx/inject'); |
| 3 | +const walk = require('acorn/dist/walk'); |
3 | 4 | const injectObjectSpread = require('acorn5-object-spread/inject'); |
4 | 5 | const checkStaticRequireWithMemberExpressionsRecursively = require('./checkMemberImports'); |
5 | 6 | const parseIdentifiers = require('./parseIdentifiers'); |
@@ -63,55 +64,61 @@ const resolver = (moduleContents, fileName) => { |
63 | 64 | ast.body = []; |
64 | 65 | } |
65 | 66 | const nodes = ast.body; |
| 67 | + |
66 | 68 | for (let j = 0; j < nodes.length; j += 1) { |
67 | | - const node = nodes[j]; |
68 | | - let memberImport; |
69 | | - let source; |
| 69 | + const topLevelNode = nodes[j]; |
| 70 | + walk.full(topLevelNode, node => { |
| 71 | + let memberImport; |
| 72 | + let source; |
| 73 | + |
| 74 | + if (checkifVariableDeclaratorWithFunctionCall(node)) { |
| 75 | + if (checkIfDefaultRequire(node)) { |
| 76 | + source = { |
| 77 | + source: |
| 78 | + node.declarations[0].init.arguments[0].value || |
| 79 | + '***dynamic***', |
| 80 | + path: '' |
| 81 | + }; |
| 82 | + } else { |
| 83 | + memberImport = checkStaticRequireWithMemberExpressionsRecursively( |
| 84 | + node.declarations[0].init |
| 85 | + ); |
| 86 | + if (memberImport !== 'n/a') { |
| 87 | + // todo: prepend . to path if it doesnt start with( |
| 88 | + source = memberImport; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
70 | 92 |
|
71 | | - if (checkifVariableDeclaratorWithFunctionCall(node)) { |
72 | | - if (checkIfDefaultRequire(node)) { |
| 93 | + if (!source && checkIfSoleRequire(node)) { |
73 | 94 | source = { |
74 | | - source: node.declarations[0].init.arguments[0].value, |
| 95 | + source: |
| 96 | + node.expression.arguments[0].value || '***dynamic***', |
75 | 97 | path: '' |
76 | 98 | }; |
77 | | - } else { |
| 99 | + dependencies.push({ |
| 100 | + ...source, |
| 101 | + imports: [], |
| 102 | + type: 'commonjs' |
| 103 | + }); |
| 104 | + } else if (!source && checkIfExpression(node)) { |
78 | 105 | memberImport = checkStaticRequireWithMemberExpressionsRecursively( |
79 | | - node.declarations[0].init |
| 106 | + node.expression |
80 | 107 | ); |
81 | 108 | if (memberImport !== 'n/a') { |
82 | 109 | // todo: prepend . to path if it doesnt start with( |
83 | 110 | source = memberImport; |
| 111 | + dependencies.push({ |
| 112 | + ...source, |
| 113 | + imports: [], |
| 114 | + type: 'commonjs' |
| 115 | + }); |
84 | 116 | } |
| 117 | + } else if (source) { |
| 118 | + const imports = parseIdentifiers(node.declarations[0].id); |
| 119 | + dependencies.push({ ...source, imports, type: 'commonjs' }); |
85 | 120 | } |
86 | | - } |
87 | | - |
88 | | - if (!source && checkIfSoleRequire(node)) { |
89 | | - source = { |
90 | | - source: node.expression.arguments[0].value, |
91 | | - path: '' |
92 | | - }; |
93 | | - dependencies.push({ |
94 | | - ...source, |
95 | | - imports: [], |
96 | | - type: 'commonjs' |
97 | | - }); |
98 | | - } else if (!source && checkIfExpression(node)) { |
99 | | - memberImport = checkStaticRequireWithMemberExpressionsRecursively( |
100 | | - node.expression |
101 | | - ); |
102 | | - if (memberImport !== 'n/a') { |
103 | | - // todo: prepend . to path if it doesnt start with( |
104 | | - source = memberImport; |
105 | | - dependencies.push({ |
106 | | - ...source, |
107 | | - imports: [], |
108 | | - type: 'commonjs' |
109 | | - }); |
110 | | - } |
111 | | - } else if (source) { |
112 | | - const imports = parseIdentifiers(node.declarations[0].id); |
113 | | - dependencies.push({ ...source, imports, type: 'commonjs' }); |
114 | | - } |
| 121 | + }); |
115 | 122 | } |
116 | 123 |
|
117 | 124 | return dependencies; |
|
0 commit comments