English | 简体中文
`esfinder is a tool based on Babel and SWC for analyzing and parsing file imports and their related dependencies in JavaScript and TypeScript projects. It can efficiently track files related to import paths and supports both static and dynamic imports.
npm install esfinder
filePath
(string): The absolute or relative path of the file to parse.
Promise<Set<string>>
: APromise
that resolves to aSet
of export names found in the given file.
This function parses the exports of a file, returning a Set
of all named and default exports. It caches the results to improve performance for repeated calls on the same file.
getRelatedFiles(files: string[], importsDir: string, extensions: string[] = DEFAULT_EXTENSIONS): Promise<string[]>
files
(string[]): A list of file paths to check for related imports.importsDir
(string): The directory containing files to be checked for imports.extensions
(string[]): An optional array of file extensions to resolve. Defaults to['.js', '.ts', '.jsx', '.tsx', '.mjs', '.cjs']
.
Promise<string[]>
: APromise
that resolves to an array of file paths that are related to the given files, based on the imports and exports.
This function checks all files in the importsDir
for import
statements and compares them with the given files
. It uses cached export data and attempts to resolve paths based on the provided extensions.
import path from 'node:path'
import { getRelatedFiles, parseExports } from 'esfinder' // by babel
import { getRelatedFiles, parseExports } from 'esfinder/swc' // by swc
const files = ['./src/a.js', './src/c.js']
const importsDir = './src/__tests__'
// Pre-cache the export contents of target files
Promise.all(files.map(f => parseExports(path.resolve(f))))
.then(() => getRelatedFiles(files, importsDir))
.then(console.log)
.catch(console.error)
In this example:
- The exports of
a.js
andc.js
are cached. - The function
getRelatedFiles
finds all files in__tests__
that are related to the given files based on their imports.
We welcome contributions to improve esfinder
. Please fork the repository, create a branch for your feature, and submit a pull request.
- Follow the Code of Conduct.
- Ensure that tests are added for any new features or bug fixes.
- Update documentation as necessary.