Package Exports
- find-entry-points
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (find-entry-points) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Find Entry Points
Find the entry points in a set of JavaScript files.
Install
Supports Node.js versions 10 and above.
$ 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'
const main = async () => {
// `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
}
})
)
}
main()
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
This is not an official Google product.