0.2.2 • Published 1 year ago
import-chain v0.2.2
import-chain
show which file import the target file
通过一级级的查找引用目标文件的父文件,来找到引用目标文件的最顶层文件,并且给出引用链路。
可以用它来做什么?
- 当我们修改一个文件之后,可以通过这个库来得知这个修改的影响范围。
- 可以和 lint-staged 搭配,获取每次 commit 的影响范围
- 可以检测哪些文件没有被使用
库
这个库导出一个函数 getImportChain,它的第一个参数是需要查找的目标文件路径数据,第二个参数是配置项,配置项的签名是:
export interface ImportChainOptions {
includedPath?: string[] // 目标文件路径数组,会被传入 fast-glob 里面
excludedPath?: string[] // 不包含的目标文件路径数组,会被传入 fast-glob 的 ignore里面
extensions: string[] // 支持的文件后缀,默认值为:['.ts', '.vue', '.js', '.jsx', '.tsx', '.d.ts']
baseUrl?: string // 和 tsconfig 里面的 baseUrl 一样,默认值为 '.'
paths?: Record<string, string[]> // 和 tsconfig 里面的 paths 一样
tsconfigPath?: string // tsconfig 的路径。默认会查找根目录下的 tsconfig
}
函数会返回一个数组,里面的每一项的签名如下:
export interface ImportChainResultItem {
path: string // 目标文件的路径
root: string[] // 目标文件被引用的顶层文件
parents: string[][] // 目标文件的被引用链路
}
cli
这个库还能被当做 cli 使用。只不过 cli 只能识别一个目标文件,使用方法如下:
import-chain './pages/**/*.vue' './components/**/*.vue' './components/package-options/mobile/sku/components/time.vue'
cli 会识别最后一个 args 作为目标文件,前面的 args 都会识别为 includedPath。