AST分析常用工具函数集。
interface ImportMap {
/**
* @description 导入的信息的唯一标识(文件绝对路径)
*/
id: string
/**
* @description 导入来源的相对路径
*/
source: string
/**
* @description 导出的值的类型, 值或者ts类型
*/
importKind?: 'value' | 'type'
/**
* @description 在导出文件的引用名称
*/
exportName?: string
}
解析文件所有import,返回一个组装后的map。
interface AnalyzeImports {
(filePath: string, filter?: (source: string) => boolean): {
[identifier: string]: ImportMap
}
}
const imports = analyzeImports(absoluteFilePath)
根据入口文件,返回所有相关依赖(递归)
interface ImportMapWithDeps extends ImportMap {
beImportedId?: string
dependencies?: Record<string, ImportMapWithDeps>
}
interface AnalyzeDependencies {
(filePath: string, filter?: (source: string) => boolean): {
[identifier: string]: ImportMapWithDeps
}
}
const deps = analyzeDependencies(absoluteFilePath)