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

es-info

v0.4.0

Published

Analyze ES module and extract information about imports, exports, and dynamic imports.

Downloads

6,288

Readme

es-info

.github/workflows/build.yml codecov

Analyze ES module and extract information about imports, exports, and dynamic imports.

Usage

const {Parser} = require("acorn");
const {analyze} = require("es-info");

const code = `
import foo from "foo";
import {bar} from "bar";
import * as baz from "baz";

export default "bar";
export const boo = "boo";
export {bla} from "bla";

if (foo === "doSomething") {
  import("dynamic-imported")
    .then(module => module.doSomething());
}
`;
const ast = Parser.parse(code, {sourceType: "module", ecmaVersion: 2022});

const result = await analyze({ast, subtree: true});

Result:

{
  import: {
    foo: {
      default: true,
      named: [],
      all: false,
      used: ["default"]
    },
    bar: {
      default: false,
      named: ["bar"],
      all: false,
      used: []
    },
    baz: {
      default: false,
      named: [],
      all: true,
      used: []
    },
    bla: {
      default: false,
      named: ["bla"],
      all: false,
      used: []
    }
  },
  export: {
    default: true,
    named: ["boo", "bla"],
    all: false
  },
  dynamicImport: ["dynamic-imported"]
}

API reference

This module exports following members.

  • analyze: A function which can analyze an AST and extract imports, exports information.

analyze

async analyze({
  ast,
  subtree?: boolean
}) => {
  import: {
    [importee]: {
      default: boolean,
      named: Array<string>,
      all: boolean,
      used: Array<string>
    }
  }
  export: {
    default: boolean,
    named: Array<string>,
    all: boolean
  },
  dynamicImport: Array<string>
}

options

  • ast is an estree object.
  • subtree - if true, the analyzer will analyze the entire tree, otherwise only those top-level nodes (import/export declarations) are analyzed. You have to set subtree to true if you want to collect the usage of imported names and to collect dynamic imports. Default: false.

result.import

  • result.import is an importee -> importInfo object map.

  • result.import[importee].default - if true then the default member is imported from the importee module.

  • result.import[importee].named contains a list of imported names.

  • result.import[importee].all - if true then all names are imported from the module i.e. import * from ....

  • result.import[importee].used is an array of imported names. If a name is included in this array, then it is referenced somewhere in the code. You can use this array to determine which names are actually used when .all is true.

    Note that export {foo} from "bar" doesn't use foo.

result.export

  • result.export.default - if true then the module exports a default member.
  • result.export.named is a list of exported names.
  • result.export.all - if true then the module exports all members from another module e.g. export * from "foo".

result.dynamicImport

  • dynamicImport is a list of module ID which are imported with dynamic import() statement.

Changelog

  • 0.4.0 (Aug 8, 2022)

    • Bump dependencies.
    • Change: analyze is now an async function.
    • Fix: support ImportExpression.
  • 0.3.0 (Jun 13, 2019)

    • Breaking: the signature of analyze() is changed.
    • Add: subtree option.
  • 0.2.1 (Jun 6, 2019)

    • Fix: rename .scope to ._esInfoScope to avoid the conflict.
  • 0.2.0 (Jun 6, 2019)

    • Breaking: drop options.dynamicImport. Now the module always analyze the entire tree.
    • Add: importInfo.used. Use it to check which names are used.
  • 0.1.1 (Apr 29, 2018)

    • Fix: ClassDeclaration.
  • 0.1.0 (Apr 28, 2018)

    • Initial release.