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

find-entry-points

v1.0.0

Published

Find the entry points in a set of JavaScript files.

Downloads

13

Readme

Install

$ npm i find-entry-points

Huh?

Example 1

Given a sync or async iterable of paths to the JavaScript files with the following dependencies between them (a directed arrow from a.js to b.js means a.js imports b.js):

The findEntryPoints function would return [['a.js']] because a.js is the entry point of the above JavaScript library or application (it is not imported by any JavaScript file). The findSingleEntryPoints function would return ['a.js'].

Example 2

Given a sync or async iterable of paths to the JavaScript files with the following dependencies between them:

The findEntryPoints function would return [['a.js'], ['f.js'], ['g.js']] (order not guaranteed) because a.js, f.js, and g.js are all not imported by any JavaScript file. Notice that it is possible to have a disconnected dependency graph. The findSingleEntryPoints function would return ['a.js', 'f.js', 'g.js'] (order not guaranteed).

Example 3

Given a sync or async iterable of paths to the JavaScript files with the following dependencies between them:

The findEntryPoints function would return [['a.js'], ['g.js']] (order not guaranteed). Notice that cycles are possible because imports are cached. The findSingleEntryPoints function would return ['a.js', 'g.js'] (order not guaranteed).

Example 4

Given a sync or async iterable of paths to the JavaScript files with the following dependencies between them:

The findEntryPoints function would return [['a.js'], ['g.js', 'h.js', 'i.js']] (order not guaranteed). Notice that this is the first example where an inner array contains more than one element. This is because g.js, h.js, and i.js are all valid entry points for their graph component. The findSingleEntryPoints function would return ['a.js'] (order not guaranteed).

Usage

Suppose the JavaScript files with the following dependencies between them are all in a src directory and that h.js imports i.js via a dynamic import:

import { findEntryPoints, findSingleEntryPoints } from 'find-entry-points'
import globby from 'globby'
import * as swc from '@swc/core'

// `globby.stream` returns an async iterable of file paths
console.log(await findEntryPoints(globby.stream('src/**/*.js')))
//=> [['src/a.js'], ['src/g.js', 'src/h.js', 'src/i.js']]

// `findSingleEntryPoints` ignores cyclic entry points
console.log(await findSingleEntryPoints(globby.stream('src/**/*.js')))
// => ['src/a.js']

console.log(
  await findEntryPoints(globby.stream('src/**/*.js'), {
    followDynamicImports: false
  })
)
//=> [['src/a.js'], ['src/i.js']]

console.log(
  await findSingleEntryPoints(globby.stream('src/**/*.js'), {
    followDynamicImports: false
  })
)
// => ['src/a.js', 'src/i.js']

// Use the `transform` option to transform non-standard syntax
// like JSX to standard ECMAScript so that imports can be parsed
console.log(
  await findEntryPoints(globby.stream('src/**/*.js'), {
    transform: async ({ path, code }) =>
      (
        await transform(code, {
          filename: path,
          jsc: { parser: { jsx: true } }
        })
      ).code
  })
)
//=> [['src/a.js'], ['src/g.js', 'src/h.js', 'src/i.js']]

console.log(
  await findSingleEntryPoints(globby.stream('src/**/*.js'), {
    transform: async ({ path, code }) =>
      (
        await transform(code, {
          filename: path,
          jsc: { parser: { jsx: true } }
        })
      ).code
  })
)
//=> ['src/a.js']

// Use the `parseImports` option to parse imports yourself
console.log(
  await findEntryPoints(globby.stream('src/**/*.js'), {
    parseImports: async ({ followDynamicImports, file }) => {
      const { path, read } = file

      // Read the file if you want
      const code = await read()
      const importedFilenames = yourFancyImportParser(code)

      return importedFilenames
    }
  })
)

See the commented type definitions for clarification.

How?

The package uses parse-imports (another package of mine) to construct a dependency graph, which is a directed graph, from the given set of JavaScript files. Then the package finds the strongly connected components of the dependency graph using Tarjan's strongly connected components algorithm, and constructs a directed acyclic graph from the strongly connected components. Finally, the package returns the strongly connected components corresponding to the vertices with in-degree 1 in the new directed acyclic graph.

Contributing

Stars are always welcome!

For bugs and feature requests, please create an issue.

For pull requests, please read the contributing guidelines.

License

Apache 2.0

This is not an official Google product.