Skip to content

Commit 96e1c42

Browse files
committed
feat(non-global-cjs): add non-global and dynamic require support
1 parent f9e0a9d commit 96e1c42

File tree

3 files changed

+87
-38
lines changed

3 files changed

+87
-38
lines changed

utils/static-require-resolver/checkMemberImports/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ const checkStaticRequireWithMemberExpressionsRecursively = node => {
159159
) {
160160
return {
161161
path: '',
162-
source: node.arguments[0].value
162+
source: node.arguments[0].value || '***dynamic***'
163163
};
164164
}
165165

utils/static-require-resolver/index.js

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const acorn = require('acorn');
22
const injectJSX = require('acorn-jsx/inject');
3+
const walk = require('acorn/dist/walk');
34
const injectObjectSpread = require('acorn5-object-spread/inject');
45
const checkStaticRequireWithMemberExpressionsRecursively = require('./checkMemberImports');
56
const parseIdentifiers = require('./parseIdentifiers');
@@ -63,55 +64,61 @@ const resolver = (moduleContents, fileName) => {
6364
ast.body = [];
6465
}
6566
const nodes = ast.body;
67+
6668
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+
}
7092

71-
if (checkifVariableDeclaratorWithFunctionCall(node)) {
72-
if (checkIfDefaultRequire(node)) {
93+
if (!source && checkIfSoleRequire(node)) {
7394
source = {
74-
source: node.declarations[0].init.arguments[0].value,
95+
source:
96+
node.expression.arguments[0].value || '***dynamic***',
7597
path: ''
7698
};
77-
} else {
99+
dependencies.push({
100+
...source,
101+
imports: [],
102+
type: 'commonjs'
103+
});
104+
} else if (!source && checkIfExpression(node)) {
78105
memberImport = checkStaticRequireWithMemberExpressionsRecursively(
79-
node.declarations[0].init
106+
node.expression
80107
);
81108
if (memberImport !== 'n/a') {
82109
// todo: prepend . to path if it doesnt start with(
83110
source = memberImport;
111+
dependencies.push({
112+
...source,
113+
imports: [],
114+
type: 'commonjs'
115+
});
84116
}
117+
} else if (source) {
118+
const imports = parseIdentifiers(node.declarations[0].id);
119+
dependencies.push({ ...source, imports, type: 'commonjs' });
85120
}
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+
});
115122
}
116123

117124
return dependencies;

utils/static-require-resolver/index.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,46 @@ describe('Static Require Resolvers (CJS)', () => {
179179
const imports = resolver(testModule);
180180
expect(imports).toEqual([]);
181181
});
182+
183+
it('should resolve non-global require', () => {
184+
const testModule = `
185+
186+
const xyz = () => {
187+
const x = require('some-module.js').ABC
188+
}
189+
190+
module.exports = createDelivery;
191+
`;
192+
const imports = resolver(testModule);
193+
expect(imports).toEqual([
194+
{
195+
imports: [{ imported: '*', local: 'x' }],
196+
path: 'ABC',
197+
source: 'some-module.js',
198+
type: 'commonjs'
199+
}
200+
]);
201+
});
202+
203+
it('should resolve dynamic require', () => {
204+
const testModule = `
205+
const y = () => {
206+
const xyz = () => {
207+
const somePath = Math.random() > 0.5 ? './this.js' : './that.js';
208+
const x = require(somePath)({test: 1}).yoo.lol(1).ABC
209+
}
210+
}
211+
212+
module.exports = createDelivery;
213+
`;
214+
const imports = resolver(testModule);
215+
expect(imports).toEqual([
216+
{
217+
imports: [{ imported: '*', local: 'x' }],
218+
path: '().yoo.lol().ABC',
219+
source: '***dynamic***',
220+
type: 'commonjs'
221+
}
222+
]);
223+
});
182224
});

0 commit comments

Comments
 (0)