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

get-es-imports-exports

v1.0.8

Published

Recursively get imports and exports for files using ES import/export syntax

Downloads

105

Readme

get-es-imports-exports

Recursively or non-recursively gets all ES6 imports and exports used within a project.

E.g.

// a.js
import b from './b';
export b;
...
// b.js
export * from './c';
...
// c.js
import { find } from 'lodash';
export { find };
...

Returns an object in the form:

{
  imports: {
    'full path of ./b': ['default'],
    'full path of ./c': ['*'],
    'full path of lodash': ['find'],
  },
  exports: {
    'full path of ./a': ['default'],
    'full path of ./b': ['*'],
    'full path of ./c': ['find'],
  }
}

API

import getEsImportsExports from 'get-es-imports-exports';

const { imports, exports, loadedFiles, stats } = await getEsImportsExports({
  files = [],
  recurse = true,
  exclude = [],
  parser = defaultParser,
  parserOptions = defaultParserOptions,
  resolveOptions = {},
});

files is an array of paths for JS files you want to check.

The recurse option will recursively look at the files imported to find more imports. Will not recurse and look for dependencies within node_modules, but imports to these files are still recorded.

The exclude option is an array of file globs for files you don't want to check when recursing. As with node_modules, imports to these files are still reported.

The parser and parserOptions options are identical to ESLint. By default, it will supports ES6 modules and JSX.

The resolveOptions is passed to resolve to resolve imports.

Return values

imports is a map of absolute file path to an array of imports. Named imports are left as-is, default imports are reported as 'default', and namespace imports are reported as '*'.

I.e.

import { a } from './a'; // 'a'
import { a as b } from './a'; // 'a'
import a from './a'; // 'default'
import * as a from './a'; // '*'

Note that exports can also report as an import.

export * from 'lodash'; // equates to import * from 'lodash'

exports is also a map of absolute filepath to an array, but this time, exports. As before, the same naming convention is used.

export const five = 5; // 'five'
export { five as FIVE } // 'FIVE'
export default 5; // 'default'
export * from './a'; // '*'

loadedFiles is the files that were loaded and checked.

stats is an array of warnings. Currently the only warning is if a file failed to parse, and had an extension that wasn't .js (which would otherwise throw).

The default parser and default parser options are exported for your convenience.

Tips

To use babel-eslint, use,

getEsImportsExports({
  ...
  parser: 'babel-eslint',
  ...
})