$ npm install detective-amd
Returns a list of dependencies for a given JavaScript file or AST using any of the AMD module syntaxes.
Inspired by substack/node-detective but built for AMD.
npm install detective-amd
Let's say we have the following file definitions:
// a.js
define(['./b', './c'], function (b, c) {
console.log(b, c);
});
// b.js
define({
name: 'foo'
});
// c.js
define(function () {
return 'bar';
});
Here's how you can grab the list of dependencies of a.js synchronously.
import fs from 'node:fs';
import detective from 'detective-amd';
const srcA = fs.readFileSync('a.js', 'utf8');
// Pass in the source code or an AST (if you've already parsed the file)
console.log(detective(srcA)); // prints ['./b', './c']
const { default: detective } = require('detective-amd');
You may also (optionally) configure the detective via a second object argument detective(src, options) that supports the following options:
skipLazyLoaded: (boolean) whether or not to omit inner requires in the list of extracted dependencies.
Supports the 4 forms of AMD module syntax:
define('name', [deps], func)define([deps], func)define(func(require))define({})Extra forms:
require([deps], func)define(function(require, exports, module) {}).Also handles dynamically loaded dependencies (ex: inner requires).
Supports driver scripts
You can also find the dependencies from a script that has a top-level require (an app initialization/driver/entry-point script):
require([
'./a'
], function (a) {
// My app will get booted up from here
});
Expression-based requires
If there's a require call with a non-literal, non-call-expression argument, an escodegen-generated string representation is included in the dependency list.
For example, if a.js was of the "factory" form and contained a dynamic module name using a binary expression:
// a.js
define(function (require) {
// Assume str is some variable that gets set to a string dynamically
// const str = ...
const b = require('./' + str);
const c = require('./c');
console.log(b, c);
});
The dependency list will be: [ '\'./\' + str', './c' ]
require(getModule())), it cannot be statically determined and is omitted from the list.© 2010 - cnpmjs.org x YWFE | Home | YWFE