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

analyze-es6-modules

v0.6.2

Published

Performs static analysis of ES6 modules in your codebase.

Downloads

11

Readme

analyze-es6-modules Build Status

analyze-es6-modules is a program that reads and statically analyzes ES6 module statements in your Javascript project. It builds a list of all modules in your project, including their imports and exports, then determines if those imports and exports align. It can warn about mistakes such as bad module references or bad imports, and even warn about unused exports. The task is highly configurable so that it can adapt to a wide range of projects.

Usage

This module exports only a single function that returns an A+ promise, so it can be used like so:

var analyzeModules = require('analyze-es6-modules');

analyzeModules(configuration).then(resolvedHandler, rejectedHandler);

Configuration

The exported function takes an object of configuration options that are described below.

  • cwd: The main root of your project. All of your sources should lie somewhere in this directory tree. If left out, process.cwd() will be used instead.
  • sources: An array of globbing patterns that point to your source files. You can prefix any globbing pattern with a ! to specifically exclude those files (this goes against what the node-glob documentation says). Your patterns should only point to files that contain Javascript source code, do not include any other type of file as it will cause errors.
  • fileReader: A function that takes an absolute file path and returns a promise that resolves to the contents of the file. If left out a function that reads from the file system will be used. But you can override this function if you have a more efficient way of getting file contents (such as a cache or stream).
  • aliases: An object that contains module aliases used in your sources. There are two types of aliases: module aliases and path aliases. Module aliases are direct 1-to-1 mappings from one module to another. Path aliases are path prefixes used to create shortcuts and redirects. The format is better seen in the example below:
const aliases = {
   module: {
       // When you import from `config`, it'll actually import from `source/config/all`
       // Note that module aliases can NOT be relative paths.
       'config': 'source/config/all'
   },
   path: {
       // This makes all `app/*` references point to the root of your project
       // Note that the prefix must end with a slash
       // The destination is relative to your project root, must begin with a dot, and cannot end with a slash
       'app/': '.',
       // This will route something like `util/time` to `source/misc/util/time`
       'util/': './source/misc/util'
   }
}
  • babel: These are options that may be passed to Babel when parsing. At this time only plugins are supported. By using plugins you can expand the available syntax to include non-standard synatx (like JSX or Flow).
{
    "plugins": [require('babel-plugin-syntax-jsx'), require('babel-plugin-syntax-flow')]
}
  • predefined: This is an object containing modules that can be used but do not exist within your project directly. This is where you can include module descriptions for things like third-party libraries.
{
    // Using `true` will allow you to import anything from the `d3` module
    "d3": true,
    // This will only allow you to import a default import from the `jQuery` module
    "jQuery": {
        'default': true
    },
    // This will only allow you to import the `forEach` and `filter` named exports from `lodash`
    "lodash": {
        'default': false,
        named: ['forEach', 'filter']
    }
}
  • ignoreUnused: This is an object containing rules for ignoring unused warnings for modules and exports.
{
    // Using `true` will ignore any unused warnings from that module
    "index": true,
    // This will ignore unused default export warnings
    "class": {
        'default': true
    },
    // This will ignore unused named export warnings for 'pi' and 'e'
    "math": {
        'default': false,
        named: ['pi', 'e']
    }
}
  • resolveModulePath: A function that can replace any module path with an arbitrary user-defined module. The function receives a single object argument with three properties:
  • cwd: The analyzer's current working directory
  • path: The path that can be resolved to a different path
  • importingModulePath: The resolved path of the module importing the path above The function can either return a string which is the new path that will be used, or undefined to signal that the path should be resolved in the default manner.

Note that this function is called before any other kind of resolution is done, including aliases. Here's a usage example:

resolveModulePath: function(options) {
    if (options.path.indexOf('/old_directory/') >= 0) {
        return options.path.replace('/old_directory/', '/new_directory/');
    }
    
    if (options.path === 'config') {
        return 'app/configuration/main';
    }
}

Output

The output format is still somewhat in flux, but should remain mostly backwards compatible. You can find a description of it using Typescript interfaces here. For more real-world examples, check out the test scenarios here. The output format was designed to be flexible enough to report the issues in a project-specific manner.

Plugins

A Grunt plugin can be found here.

Development

TODO