detective-amd

Find all dependencies within a JavaScript file using AMD module syntax

MIT 37 个版本
安装
npm install detective-amd
yarn add detective-amd
pnpm add detective-amd
bun add detective-amd
README

detective-amd

CI npm version npm downloads

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

Usage

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.

ESM

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']

CommonJS

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.
    • Note: this does not affect the REM form since those inner requires are not "lazily" fetched.

Syntax Support

Supports the 4 forms of AMD module syntax:

  • "named": define('name', [deps], func)
  • "dependency list": define([deps], func)
  • "factory": define(func(require))
  • "no dependencies": define({})

Extra forms:

  • "driver script" (or entry-point) syntax: require([deps], func)
  • "REM" (or CommonJS-like) form: 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' ]

  • Note: if the argument is a function call (e.g. require(getModule())), it cannot be statically determined and is omitted from the list.

License

MIT

版本列表
7.0.0 2026-05-19
6.1.0 2026-04-25
6.0.2 2026-04-21
6.0.1 2025-02-01
6.0.0 2024-04-14
5.0.2 2023-05-15
5.0.1 2023-05-08
5.0.0 2023-05-05
4.2.0 2023-04-25
4.1.0 2023-03-19
4.0.1 2022-03-03
4.0.0 2022-03-03
3.1.2 2022-02-22
3.1.1 2022-02-21
3.1.0 2021-03-19
3.0.1 2020-10-26
3.0.0 2018-09-06
2.4.0 2016-09-29
2.3.5 2016-09-28
2.3.4 2016-06-16
2.3.3 2015-10-24
2.3.2 2015-07-04
2.3.1 2015-06-27
2.3.0 2014-12-14
2.2.2 2014-12-13
2.2.1 2014-11-01
2.2.0 2014-10-25
2.1.5 2014-09-11
2.1.4 2014-07-18
2.1.3 2014-07-18
2.1.2 2014-07-18
2.1.1 2014-07-07
2.1.0 2014-06-06
2.0.3 2014-06-06
2.0.2 2014-04-13
2.0.1 2014-03-28
2.0.0 2014-03-21