Package Exports
- babel-plugin-detective
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (babel-plugin-detective) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
babel-detective 
A Babel 6 plugin that scans the AST for require calls and import statements
Install
$ npm install --save babel-plugin-detective babel-coreUsage
import babel from 'babel-core';
const detective = require('babel-plugin-detective');
const myModule = require('my' + 'module');
const opts = {};
// data will be stored on `result.metadata.requires`
var result = babel.transformFileAsync('/path/to/file', {
plugins:[['detective', opts]]
});
// convenience method to extract metadata
var metadata = detective.metadata(result);
console.log(metadata);
// {
// strings: [
// 'babel-core',
// 'babel-plugin-detective'
// ],
// expressions: [
// {start: 110, end: 125} // loc of 'my' + 'module' expression
// ]
// }API
detective.metadata(previousParseResult)
During traversal, the plugin stores each discovered require/import on the Babel metadata object.
It can be extracted manually from parseResult.metadata.requires, or you can pass the parse result
to this convenience method.
Returned Metadata
After a babel traversal with this plugin, metadata will be attached at parseResult.metadata.requires
requires.strings
Type: Array<string>
Array of every module imported with an ES2015 import statement, and every module required using a string literal
(it does not include dynamic requires).
requires.expressions
Type: Array<locationData>
Array of location data for expressions that are used as the first argument to require (i.e. dynamic requires).
The source of the expression can be attached using the attachExpressionSource option.
If you wish to disallow dynamic requires, you should throw if this has length greater than 0.
Options
opts.includeGenerated
Type: boolean
Default: false
If set to true, it will include require calls generated by previous plugins in the
tool chain. This will lead to some duplicate entries if ES2015 import statements are
present in the file. This plugin already scans for ES2015 import statements, so you
only need to use this if there is some other type of generated require statement you
want to know about.
opts.includeImport
Type: boolean
Default: true
Include ES2015 imports in the metadata. All ES2015 imports will be of type string.
opts.includeRequire
Type: boolean
Default: true
Include CommonJS style require(...) statements in the metadata. CommonJS require
statements will be pushed on to requires.strings if the argument is a string literal.
For dynamic expressions (i.e. require(foo + bar)), an object will be pushed on to requires.expressions.
It will have start and end properties that can be used to extract the code directly from the original source.
opts.word
Type: string
Default: 'require'
The name of the require function. You most likely do not need to change this.
opts.attachExpressionSource
Type: boolean
Default: false
Attach the actual expression code to each member of requires.expressions.
expressions: [
{start: 110, end: 125, code: "'my' + 'module'"}
]License
MIT © James Talmage