Skip to content

Commit fc710ff

Browse files
authored
feat: support module.createRequire (#459)
## Description Added support for tracing dependencies loaded via `module.createRequire`. Fixes #430
1 parent 5777c8b commit fc710ff

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

src/analyze.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,20 @@ export default async function analyze(
10821082
if (returned) setKnownBinding(fnName.name, { value: BOUND_REQUIRE });
10831083
}
10841084
}
1085+
// Support module.createRequire
1086+
if (
1087+
node.type === 'CallExpression' &&
1088+
node.callee.type === 'MemberExpression' &&
1089+
node.callee.object.type === 'Identifier' &&
1090+
node.callee.object.name === 'module' &&
1091+
node.callee.property.type === 'Identifier' &&
1092+
node.callee.property.name === 'createRequire'
1093+
) {
1094+
if (parent.type === 'VariableDeclarator') {
1095+
const requireName = parent.id.name;
1096+
setKnownBinding(requireName, { value: BOUND_REQUIRE });
1097+
}
1098+
}
10851099
},
10861100
async leave(_node, _parent) {
10871101
const node: Node = _node as any;
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as module from 'node:module';
2+
3+
const req = module.createRequire(import.meta.url);
4+
const lib = req('./lib.node');
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Native module for testing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
"package.json",
3+
"test/unit/module-create-require/input.js",
4+
"test/unit/module-create-require/lib.node"
5+
]

0 commit comments

Comments
 (0)