0.0.2 • Published 10 years ago
seek-require v0.0.2
seek-require
Installation
$ npm install seek-require --save
Usage
Return object will contains the require calls with module as key.
For all unanalysable calls, the node will be stored in 'unresolved' property.
var seeker = require('seek-require');
var ast = require('esprima').parse("var a = require('util');");
var options = {
strictArguments: true
};
var requires = seeker(ast, require('escope').analyze(ast), options); // pass scopes so scopes could be re-use for performance
// or
var requires = seeker(ast, options);
// requires = {
// 'util': [{<AST NodeObject>}] // an array of require calls nodes
// };
// 'unresolved' is a non-enumerable property in result, which means it will no show up in Object.keys(requires) or for(var p in requires);
var modules = Object.keys(requires); // ['util']
for(var mod in requires) {
console.log(mod); // only get 'util'
}
// all non standard require calls are in requires.unresolved
requires = seeker('var a = "util"; var b = require(a);', { literalOnly: false });
Object.keys(requires).length; // return 0
requires.unresolved.length; // return 1
Options
- literalOnly =boolean= only found require calls with literal arguments, default to false
- strictArguments =boolean= only accept one argument for require calls, default to true
Licence
MIT