fix(cjs-module-analyzer): accept any require, not just top level
MODIFY
packages/cjs-module-analyzer/index.js
packages/cjs-module-analyzer/index.js
@@ -166,6 +166,13 @@ function parseSource(cjsSource) {
}
switch (ch) {
+ case 114 /* r */: {
+ const startPos = pos;
+ if (tryParseRequire(Import) && keywordStart(startPos))
+ tryBacktrackAddStarExportBinding(startPos - 1);
+ lastTokenPos = pos;
+ continue;
+ }
case 101 /* e */:
if (source.startsWith('xport', pos + 1) && keywordStart(pos)) {
if (source.charCodeAt(pos + 6) === 115 /* s */)
@@ -1015,6 +1022,7 @@ function tryParseRequire(requireType) {
// require('...')
const revertPos = pos;
if (source.startsWith('equire', pos + 1)) {
+ process._rawDebug("SRC"+source.substr(pos,20))
pos += 7;
let ch = commentWhitespace();
if (ch === 40 /* ( */) {

MODIFY
packages/cjs-module-analyzer/test/test-cjs-module-analyzer.js
packages/cjs-module-analyzer/test/test-cjs-module-analyzer.js
@@ -431,6 +431,14 @@ test('Rollup Babel reexports', t => {
t.is(reexports[14], './Accordion');
});
+test('Identify require calls in function arguments', t => {
+ const { requires } = analyzeCommonJS(`
+ let Mime = require('./Mime');
+ Mime(require('./types/standard'), require('./types/other'));
+ `);
+ t.is(requires.length, 3);
+});
+
test('invalid exports cases', t => {
const { exports } = analyzeCommonJS(`
module.exports['?invalid'] = 'asdf';
@@ -439,7 +447,7 @@ test('invalid exports cases', t => {
});
test('module exports reexport spread', t => {
- const { exports, reexports } = analyzeCommonJS(`
+ const { exports, reexports, requires } = analyzeCommonJS(`
module.exports = {
...a,
...b,
@@ -455,6 +463,7 @@ test('module exports reexport spread', t => {
t.is(reexports.length, 2);
t.is(reexports[0], 'dep1');
t.is(reexports[1], 'dep2');
+ t.is(requires.length, 2);
});
test('Regexp case', t => {

fix(cjs-module-analyzer): accept any require, not just top level