detect-dep
v1.1.3
Published
Detect the dependencies by walking AST
Downloads
173
Readme
detect-dep
Detect the dependencies by walking AST
Support detecting dynamic import and require.context
Installation
npm install detect-dep
# or use yarn
yarn add detect-dep
Usage
Package
import { detectDep } from 'detect-dep'
const dependencies = detectDep('some code' /* opts */)
Cli
npm i detect-dep -g
detect-dep --help
detect-dep --version
API
AST
- **See: Abstract syntax tree **
- **See: babylon **
Type: Object
detectDep
Parameters
source
{String|AST}options
{Object}options.esModule
{Boolean} DisableesExport
esImport
dynamicImport
when is falsy (optional, defaulttrue
)options.requireEnsure
{Boolean} whether detectingrequire.ensure(['./a', 'b'])
or not (optional, defaulttrue
)options.requireContext
{Boolean} whether detectingrequire.context('./root', false, /\.jsx?$/)
or not (optional, defaulttrue
)options.dynamicImport
{Boolean} whether detectingimport('./foo')
import('./foo/' + name)
or not (optional, defaulttrue
)options.esExport
{Boolean} whether detectingexport xx from 'xxx'
or not (optional, defaulttrue
)options.esImport
{Boolean} whether detectingimport ...
or not (optional, defaulttrue
)options.requireImport
{Boolean} whether detectingrequire('...')
or not (optional, defaulttrue
)options.localImport
{Boolean} whether requiringrequire('./local/path')
or not (optional, defaulttrue
)options.moduleImport
{Boolean} whether requiringrequire('path')
or not (optional, defaulttrue
)options.extensions
{string[]} Which file with matching extension should be detected recursively (optional, default['.js','.jsx']
)options.resolveExtensions
{string[]} The resolved path's extensions which are allowed (would be extends options.extensions) (optional, defaultObject.keys(require.extensions)
)options.from
{String} where is the source come from (filename) (optional, default''
)options.recursive
{boolean} Detecting the source recursively. (optional, defaulttrue
)options.resolver
{(source, options) => string[]} The core method for resolving dependencies. (optional, defaultrequire('./resolver')
)options.returnAbsolutePath
{boolean} whether returning the local module's absolute path, or use real module id instead. (optional, defaulttrue
)
Examples
const { detectDep } = require('detect-dep')
const dependencies = detectDep('some code', {})
Returns any dependencies {String[]} - dependencies list
Module
Type: Object
tree
Parameters
path
{string}options
SeedetectDep
(optional, default{}
)
Examples
const {tree} = require('detect-dep')
const moduleTree = tree('/path/to/file')
{
id: '/path/to/file',
children: [
{
id: '/path/to/file/b.jsx',
children: []
}
]
}
Returns Module
Contributing
- Fork it!
- Create your new branch:
git checkout -b feature-new
orgit checkout -b fix-which-bug
- Start your magic work now
- Make sure npm test passes
- Commit your changes:
git commit -am 'feat: some description (close #123)'
orgit commit -am 'fix: some description (fix #123)'
- Push to the branch:
git push
- Submit a pull request :)
Authors
This library is written and maintained by imcuttle, [email protected].
License
MIT - imcuttle 🐟