Skip to content

esfinder 是一个基于Babel 和 SWC 的用于分析和解析 JavaScript 和 TypeScript 项目中文件的导入及其相关依赖的工具。它能够高效地追踪与导入路径相关的文件,支持静态和动态导入。

License

Notifications You must be signed in to change notification settings

Sunny-117/esfinder

Repository files navigation

esfinder

npm version npm downloads bundle JSDocs License

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.

Installation

npm install esfinder

API Documentation

parseExports(filePath: string): Promise<Set<string>>

Parameters:

  • filePath (string): The absolute or relative path of the file to parse.

Returns:

  • Promise<Set<string>>: A Promise that resolves to a Set of 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[]>: A Promise that 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' // 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:

  1. The exports of a.js and c.js are cached.
  2. The function getRelatedFiles finds 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.

License

MIT License © Sunny-117

About

esfinder 是一个基于Babel 和 SWC 的用于分析和解析 JavaScript 和 TypeScript 项目中文件的导入及其相关依赖的工具。它能够高效地追踪与导入路径相关的文件,支持静态和动态导入。

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published