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

esbuild-dependency-graph

v0.13.0

Published

Generate a dependency graph using esbuild's metadata

Downloads

262

Readme

esbuild-dependency-graph

Dependency graph helper based on esbuild's metafile

Installation

npm install esbuild-dependency-graph
# or yarn
yarn add esbuild-dependency-graph

Usage

import * as esbuild from 'esbuild';
import { DependencyGraph } from 'esbuild-dependency-graph';

const result = await esbuild.build({
  metafile: true,
  /* other build options */
});

const graph = new DependencyGraph({
  /**
   * Root path for lookup modules.
   *
   * Defaults to `process.cwd()`.
   */
  root: '/path/to/root-dir',
});

// Generate or update the dependency graph using the esbuild metafile.
graph.load(result.metafile);

// Get dependencies of the specified module.
graph.dependenciesOf('path/to/code.ts'); // `Module[]`

// Get dependents of the specified module.
graph.dependentsOf('path/to/code.ts'); // `Module[]`

// Get inverse dependencies of the specified module.
graph.inverseDependenciesOf('path/to/code.ts'); // `Module[]`

// You can also provide the module's ID.
graph.dependenciesOf(1);
graph.dependentsOf(2);
graph.inverseDependenciesOf(3);

// Reset dependency graph.
graph.reset();
import { DependencyGraph, isExternal } from 'esbuild-dependency-graph';

// Check if the module exists.
graph.hasModule('path/to/code.ts'); // `boolean`

// Get module data by module path.
graph.getModule('path/to/code.ts'); // `Module`

// Register new module to the graph.
graph.addModule(
  'path/to/code.ts',
  ['path/to/dependency-a', 'path/to/dependency-b'],
  ['path/to/dependent'],
); // `Module`

// Update registered module.
graph.updateModule(
  'path/to/code.ts',
  ['path/to/dependency-a', 'path/to/dependency-b'],
  ['path/to/dependent'],
); // `Module`

// Remove module from graph.
graph.removeModule('path/to/code.ts'); // `void`

// You can also provide the module's ID.
graph.hasModule(0);
graph.getModule(1);
graph.addModule(2, [3, 4], [5]);
graph.updateModule(6, [7, 8], [9]);
graph.removeModule(10);

// Check if this module is external.
isExternal(targetModule); // `boolean`
type EsbuildModule = Metafile['inputs'][string];

interface Module {
  id: number;
  path: string;
  dependencies: number[];
  dependents: number[];
}

Where is the module path?

/**
 * @type {import('esbuild').Metafile}
 */
interface Metafile {
  inputs: {
    [path: string]: {
      // Can be used as module path!
      imports: {
        path: string; // Can be used as module path!
        /* ... */
      }[];
      /* ... */
    };
  };
  outputs: {
    /* ... */
  };
}

Demo

Demo code here.

// Module: src/components/Section.tsx
{
  id: 1122,
  path: 'src/components/Section.tsx',
  dependencies: [ 509, 73, 1043 ],
  dependents: [ 1123 ]
}

// Dependencies
[
  {
    id: 509,
    path: 'node_modules/react/jsx-runtime.js',
    dependencies: [ 508 ],
    dependents: [
       512,  514,  633,  655,  657,  679,  680,  774,  784,  790,
       804,  806,  807,  808,  809,  811,  812,  813,  826,  840,
       862,  871,  876,  878,  887,  900,  906,  908,  996, 1000,
      1025, 1028, 1030, 1035, 1036, 1038, 1050, 1053, 1056, 1057,
      1058, 1061, 1064, 1068, 1069, 1070, 1071, 1076, 1080, 1081,
      1082, 1094, 1106, 1107, 1109, 1110, 1111, 1113, 1114, 1115,
      1116, 1120, 1121, 1122, 1139, 1141, 1143, 1145, 1147, 1148,
      1150, 1152, 1154, 1158, 1160, 1162, 1164, 1166, 1168, 1170,
      1172, 1176, 1178, 1181, 1183, 1185, 1187, 1189, 1190, 1352,
      1353, 1359, 1361, 1363, 1368
    ]
  },
  {
    id: 73,
    path: 'node_modules/react/index.js',
    dependencies: [ 72 ],
    dependents: [
       74, 206, 207, 237, 243, 257, 259, 262, 263, 265, 266, 270,
      271, 272, 276, 277, 279, 280, 282, 302, 304, 308, 309, 329,
      330, 336, 339, 344, 345, 374, 375, 376, 378, 380, 383, 387,
      388, 390, 391, 395, 399, 400, 401, 402, 403, 404, 407, 408,
      409, 410, 412, 416, 417, 418, 439, 440, 443, 446, 457, 459,
      460, 461, 462, 449, 464, 465, 466, 467, 468, 469, 470, 397,
      473, 474, 475, 478, 479, 480, 481, 483, 484, 487, 488, 398,
      489, 490, 492, 493, 494, 495, 498, 499, 500, 503, 504, 505,
      506, 507, 508, 512,
      ... 190 more items
    ]
  },
  {
    id: 1043,
    path: 'node_modules/dripsy/src/index.ts',
    dependencies: [ 1042 ],
    dependents: [ 1120, 1122, 1359, 1361, 1367, 1368 ]
  }
]

// Dependents
[
  {
    id: 1123,
    path: 'src/components/index.ts',
    dependencies: [ 1120, 1121, 1122 ],
    dependents: [ 1359, 1361 ]
  }
]

// Inverse dependencies
[
  {
    id: 1123,
    path: 'src/components/index.ts',
    dependencies: [ 1120, 1121, 1122 ],
    dependents: [ 1359, 1361 ]
  },
  {
    id: 1359,
    path: 'src/screens/MainScreen.tsx',
    dependencies: [
       509,   73,  448,
       518, 1043, 1123,
      1358
    ],
    dependents: [ 1362 ]
  },
  {
    id: 1361,
    path: 'src/screens/IntroScreen.tsx',
    dependencies: [ 509, 73, 448, 1043, 1123, 1360 ],
    dependents: [ 1362 ]
  },
  {
    id: 1362,
    path: 'src/screens/index.ts',
    dependencies: [ 1359, 1361 ],
    dependents: [ 1363 ]
  },
  {
    id: 1363,
    path: 'src/navigators/RootStack.tsx',
    dependencies: [ 509, 73, 1119, 1362 ],
    dependents: [ 1364 ]
  },
  {
    id: 1364,
    path: 'src/navigators/index.ts',
    dependencies: [ 1363 ],
    dependents: [ 1368 ]
  },
  {
    id: 1368,
    path: 'src/App.tsx',
    dependencies: [
       509,   73,  518,
       814,  914,  986,
      1043, 1364, 1367
    ],
    dependents: [ 1370 ]
  },
  {
    id: 1370,
    path: 'index.js',
    dependencies: [ 254, 448, 1368, 1369 ],
    dependents: []
  }
]

License

MIT