Skip to content

Commit

Permalink
feat: support module.createRequire (#459)
Browse files Browse the repository at this point in the history
## Description
Added support for tracing dependencies loaded via
`module.createRequire`.


Fixes #430
  • Loading branch information
maxam2017 authored Jan 2, 2025
1 parent 5777c8b commit fc710ff
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,20 @@ export default async function analyze(
if (returned) setKnownBinding(fnName.name, { value: BOUND_REQUIRE });
}
}
// Support module.createRequire
if (
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.type === 'Identifier' &&
node.callee.object.name === 'module' &&
node.callee.property.type === 'Identifier' &&
node.callee.property.name === 'createRequire'
) {
if (parent.type === 'VariableDeclarator') {
const requireName = parent.id.name;
setKnownBinding(requireName, { value: BOUND_REQUIRE });
}
}
},
async leave(_node, _parent) {
const node: Node = _node as any;
Expand Down
4 changes: 4 additions & 0 deletions test/unit/module-create-require/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as module from 'node:module';

const req = module.createRequire(import.meta.url);
const lib = req('./lib.node');
1 change: 1 addition & 0 deletions test/unit/module-create-require/lib.node
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Native module for testing
5 changes: 5 additions & 0 deletions test/unit/module-create-require/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"package.json",
"test/unit/module-create-require/input.js",
"test/unit/module-create-require/lib.node"
]

0 comments on commit fc710ff

Please sign in to comment.