npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-detective

v2.0.0

Published

Babel 5/6 plugin that scans the AST for require calls and import statements

Downloads

25,253

Readme

babel-plugin-detective Build Status Coverage Status

Babel 5/6 plugin that scans the AST for require calls and import statements

Install

$ npm install --save babel-plugin-detective babel-core

Usage

import babel from 'babel-core';

const detective = require('babel-plugin-detective');
const myModule = require('my' + 'module');

// See below for available options
const options = {};

// Babel 5
// `position` can be 'before' or 'after'
let result = babel.transformFileSync(path, {
	plugins: [{transformer: detective, position: position}],
	extra: {
			detective: options
	}
});

// Babel 6
result = babel.transformFileAsync('/path/to/file', {
	plugins:[['detective', options]]
});

// metadata will be stored on `result.metadata.requires`
// a convenience method is provided to extract it.
const metadata = detective.metadata(result);

console.log(metadata);
// {
//   strings: [
//     'babel-core',
//     'babel-plugin-detective'
//   ],
//   expressions: [
//     {start: 110, end: 125, loc: {...}} // 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

options.generated

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.

Works on Babel 6 only

generated:true can be combined with import:false to get only the require statements of the post transform code.

options.import

Type: boolean Default: true

Include ES2015 imports in the metadata. All ES2015 imports will be of type string.

options.export

Type: boolean Default: true

Include ES2015 re-exports in the metadata. All ES2015 re-exports will be of type string.

export * from './foo';
export {bar as baz} from './quz';
export {hello} from './goodbye';

options.require

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, And a loc object that includes the line/column numbers (useful for creating error statements).

options.word

Type: string Default: 'require'

The name of the require function. You most likely do not need to change this.

options.source

Type: boolean Default: false

Attach the actual expression code to each member of requires.expressions.

expressions: [
	 {start: 110, end: 125, loc: {...} code: "'my' + 'module'"}
]

options.nodes

Type: boolean Default: false

Return the actual nodes instead of extracting strings.

strings : [{type: 'StringLiteral', value: 'foo', ...}],
expressions: [
	 {type: 'BinaryExpression', ...}
]

Everything in strings will be a Literal (Babel 5), or StringLiteral (Babel 6). The path required will be on node.value.

The expressions array can contain any valid Expression node.

Manipulating Require Statements

Warning: Exploratory Support Only: The documentation here is intentionally sparse. While every attempt will be made to avoid breaking changes, it is a new feature so changes are a real possibility. You should look at the source of index.js and the test suite for a better idea on how to use this.

babel-detective/wrap-listener allows you to create your own plugin that can manipulate exports.

The following creates a plugin that upper-cases all require and import statements:

const wrapListener = require('babel-detective/wrap-listener');

module.exports = wrapListener(listener, 'uppercase');

function listener(path, file, opts) {
	if (path.isLiteral()) {
		path.node.value = path.node.value.toUpperCase();
	}
}

wrapListener(listener, name, options)

listener

Type: callback(nodePath, file, opts) Required

A listener that performs the actual manipulation. It is called with:

  • nodePath: The actual node in question can be accessed via nodePath.node. nodePath has other properties (parent, isLiteral(), etc.). A full description of that API is out of scope for this document.

  • file: The Babel file metadata object. This is what the main module uses to store metadata for required modules that it finds.

  • opts: The options that were passed to the plugin. This is done via the array syntax in Babel 6, or options.extra[name] in Babel 5.

name

Required Type: string

This is the key used to locate opts in Babel 5 options.extra[name].

options

Optional

Accepts the import, require, and generated options as described above.

Related

  • node-detective Inspiration for this module. Used by browserify to analyze module dependencies.

License

MIT © James Talmage