JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q53024F
  • License MIT

Get and traverse graph of ECMAScript/TypeScript modules

Package Exports

  • get-modules-graph

Readme

get-modules-graph

NPM version minzipped size code style: prettier Conventional Commits License MIT

Get and traverse graph of ECMAScript/TypeScript modules.

Basic example

import {dirname, join, sep} from 'node:path';

import {getModulesGraph} from 'get-modules-graph';

const modulesGraph = await getModulesGraph<number>({
  chooseIndexModule: (resolvedPath, directoryPath, directoryContent) => {
    if ('index.ts' in directoryContent) {
      return 'index.ts';
    }

    throw new Error(
      `Cannot choose index module in directory \`${directoryPath}\` for \`${resolvedPath}\``,
    );
  },
  chooseModule: (resolvedPath, parsedPath, directoryContent) => {
    const fileName = `${parsedPath.base}.ts`;

    if (fileName in directoryContent) {
      return fileName;
    }

    throw new Error(`Cannot choose module for \`${resolvedPath}\``);
  },
  directories: [],
  modules: ['./index.ts'],
  onAddDependencies: () => {},
  onAddModule: (_module, source) => source.length,
  resolvePath: (modulePath, rawPath) => {
    if (rawPath[0] === '.') {
      return `.${sep}${join(dirname(modulePath), rawPath)}`;
    }

    return rawPath;
  },
  skipDirectory: () => false,
  skipModule: () => false,
  transformSource: (_path, source) => source,
});

console.log(modulesGraph.circularDependencies);
console.log(modulesGraph.errors);
console.log(modulesGraph.modules);
console.log(modulesGraph.packages);

Install

Requires node version 10 or higher:

npm install get-modules-graph

get-modules-graph works in any environment that supports ES2018 (because package uses RegExp Named Capture Groups).

License

MIT