0.3.0 • Published 4 months ago

esbuild-dependency-graph v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

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 { DependencyGraph, isExternal } from 'esbuild-dependency-graph';

const graph = new DependencyGraph(metafile, 'entry.ts');

// Get module id by actual module path.
graph.getModuleId('path/to/code.ts'); // `ModuleId`

// Get module.
graph.getModule(moduleId); // `Module`

// Get dependencies of the specified module.
graph.dependenciesOf(moduleId); // `ModuleId[]`

// Get inverse dependencies of the specified module.
graph.inverseDependenciesOf(moduleId); // `ModuleId[]`

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

type InternalModule = EsbuildModule & {
  id: ModuleId;
  path: string;
  dependencies: Set<ModuleId>;
  inverseDependencies: Set<ModuleId>;
};

interface ExternalModule {
  id: ModuleId;
  path: string;
  __external: true;
}

type ModuleId = number;
type Module = InternalModule | ExternalModule;

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.

{
  "id": 1123,
  "path": "src/components/Section.tsx",
  "dependencies": [
    { "id": 510, "path": "node_modules/react/jsx-runtime.js" },
    { "id": 74, "path": "node_modules/react/index.js" },
    { "id": 1044, "path": "node_modules/dripsy/src/index.ts" }
  ],
  "inverseDependencies": [
    { "id": 1123, "path": "src/components/Section.tsx" },
    { "id": 1124, "path": "src/components/index.ts" },
    { "id": 1360, "path": "src/screens/MainScreen.tsx" },
    { "id": 1362, "path": "src/screens/IntroScreen.tsx" },
    { "id": 1363, "path": "src/screens/index.ts" },
    { "id": 1364, "path": "src/navigators/RootStack.tsx" },
    { "id": 1365, "path": "src/navigators/index.ts" },
    { "id": 1369, "path": "src/App.tsx" },
    { "id": 0, "path": "index.js" }
  ]
}

License

MIT

0.3.0

4 months ago

0.2.1

4 months ago

0.2.0

4 months ago

0.1.0

5 months ago