esfinder
v0.0.2-alpha.5
Published
A fast and lightweight tool for analyzing module dependencies and finding which files depend on a given module.
Readme
esfinder
English | 简体中文
esfinder is a tool to analyze and resolve file imports and their dependencies in JavaScript and TypeScript projects. It allows you to efficiently track related files based on import paths, supporting both static and dynamic imports.
Installation
npm install esfinderAPI Documentation
parseExports(filePath: string): Promise<Set<string>>
Parameters:
filePath(string): The absolute or relative path of the file to parse.
Returns:
Promise<Set<string>>: APromisethat resolves to aSetof export names found in the given file.
Description:
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[]>
Parameters:
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'].
Returns:
Promise<string[]>: APromisethat resolves to an array of file paths that are related to the given files, based on the imports and exports.
Description:
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.
Usage Example
import path from 'node:path'
import { getRelatedFiles, parseExports } from 'esfinder'
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.jsandc.jsare cached. - The function
getRelatedFilesfinds all files in__tests__that are related to the given files based on their imports.
Contributing
We welcome contributions to improve esfinder. Please fork the repository, create a branch for your feature, and submit a pull request.
Guidelines:
- Follow the Code of Conduct.
- Ensure that tests are added for any new features or bug fixes.
- Update documentation as necessary.
