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-track-usage

v0.3.2

Published

Babel plugin to track calls to a configured set of modules

Downloads

8,755

Readme

Track Usage Plugin

This is an experimental babel plugin that tracks the usage of a set of configured modules. It supports both ES5 and ES6 import syntax.

Usage

npm install --save-dev babel-plugin-track-usage

The module will recognize requires of the configured modules in the transpiled babel modules and will collect statically analyzable calls to the declared functions. Since 0.1.0 this also includes static template literals without expressions.

Plugin Configuration

In your .babelrc (or equivalent):

    ["track-usage", {
        trackedFunctions: {
            i18n:  {
                module: "./service/i18n",
                fn: "",
                varArgs: true
            }
        },
        debug: false
    }]

The plugin options object needs a key "trackedFunctions" that contains function definitions for every function to be tracked.

The name needs to be unique and does not matter (results will show up under this name in the resulting JSON).

The "module" prop is a module location relative to babel sourceRoot.

The "fn" prop is either empty if the module is called as function itself or fn contains the name of the method to invoke on the module.

If the "varArgs" prop is set to true, the method can have additional parameters to the first statically analyzable one. It can also be set to a numeric value specifying how many arguments are captured (true is the same as 1).

All methods calls are identified by a deep-equality comparison of their arguments, which needs to be a javascript literals or object expressions containing literals.

The "debug" plugin option causes the extraction process to log debugging information if set to true.

the "sourceRoot" plugin option allows to have all relative module paths relative to that dir

The source root option cannot start with "./" but will lead to all local modules having a "./" prefix to umambiguously distinguish them from NPM dependency modules which are listed as requires but are not analyzed themselves. The sourceRoot option should end in a "/".

Access Usage Data

    var usageData = require("babel-plugin-track-usage/data").get()

can be used to access the collected data will contain a "usages" prop with one prop per analzyed module

{ 
    "usages": {
        "./components/Grid": {
            "module": "components/Grid",
            "requires": [ "react", "classnames", "./service/i18n"],
            "calls": {
                "i18n": [["No Rows"]]
            }
        }
    }
}

The "module" prop repeats the module name (without leading "./").

The "requires" prop contains a array of modules required by this module. The module name will be absolute for modules required out of node_modules and again relative to source root for relative requires. This is exactly the format of the keys of the "usages" map. This can be used to track transitive call dependencies.

The calls prop contains a mapping from the configured logical names of our methods to an array or argument arrays that are called in the module.

Example with Webpack

The module "./webpack/track-usage-plugin.js" contains a simple webpack plugin to store the collected data in a JSON file.

Configuring the example webpack plugin

var path = require("path");

var TrackUsagePlugin = require("babel-plugin-track-usage/webpack/track-usage-plugin");

module.exports = {
    // ... rest of webpack config...
    plugins: [
        new TrackUsagePlugin({
            output: path.join(__dirname, "src/main/base/resources/js/track-usage.json")
        })
    ]
};

The babel plugin config is as such for my project

    [
        "track-usage",
        {
            "sourceRoot" : "src/main/js/",
            "trackedFunctions": {
                "i18n": {
                    "module": "./service/i18n",
                    "fn": "",
                    "varArgs": true
                }
            },
            "debug": false
        }
    ]