# import-chain

> import chain

Latest version **0.2.2** (published 2023-04-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install import-chain
pnpm add import-chain
yarn add import-chain
bun add import-chain
```

Provides the command `import-chain`.

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2023-04-10 |
| First published | 2023-03-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | ^12.20.0 \|\| ^14.13.1 \|\| >=16.0.0 |
| Dependencies | 4 |
| Unpacked size | 22 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | yangzhou |
| Maintainers | candy8ullet |

## Links

- npm: https://www.npmjs.com/package/import-chain
- Repository: https://github.com/sishenhei7/import-chain
- Homepage: https://github.com/sishenhei7/import-chain#readme
- Issues: https://github.com/sishenhei7/import-chain/issues
- npm.io page: https://npm.io/package/import-chain

## Dependencies (4)

- [debug](https://npm.io/package/debug.md) ^4.3.4
- [fast-glob](https://npm.io/package/fast-glob.md) ^3.2.12
- [picocolors](https://npm.io/package/picocolors.md) ^1.0.0
- [typescript](https://npm.io/package/typescript.md) ^4.9.5

## Recent versions

- 0.2.2 (latest) — 2023-04-10
- 0.2.1 — 2023-03-17
- 0.2.0 — 2023-03-17
- 0.1.6 — 2023-03-17
- 0.1.5 — 2023-03-16
- 0.1.4 — 2023-03-16
- 0.1.3 — 2023-03-16
- 0.1.2 — 2023-03-16
- 0.1.1 — 2023-03-16
- 0.1.0 — 2023-03-16
- 0.0.8 — 2023-03-15
- 0.0.7 — 2023-03-15
- 0.0.6 — 2023-03-15
- 0.0.5 — 2023-03-14
- 0.0.4 — 2023-03-14
- … 3 more at https://npm.io/package/import-chain/versions

## README

# import-chain

> show which file import the target file

通过一级级的查找引用目标文件的父文件，来找到引用目标文件的最顶层文件，并且给出引用链路。

## 可以用它来做什么？

1. 当我们修改一个文件之后，可以通过这个库来得知这个修改的影响范围。
2. 可以和 lint-staged 搭配，获取每次 commit 的影响范围
3. 可以检测哪些文件没有被使用

## 库

这个库导出一个函数 getImportChain，它的第一个参数是需要查找的目标文件路径数据，第二个参数是配置项，配置项的签名是：

```ts
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
}
```

函数会返回一个数组，里面的每一项的签名如下：

```ts
export interface ImportChainResultItem {
  path: string            // 目标文件的路径
  root: string[]          // 目标文件被引用的顶层文件
  parents: string[][]     // 目标文件的被引用链路
}
```

## cli

这个库还能被当做 cli 使用。只不过 cli 只能识别一个目标文件，使用方法如下：

```bash
import-chain './pages/**/*.vue' './components/**/*.vue' './components/package-options/mobile/sku/components/time.vue'
```

cli 会识别最后一个 args 作为目标文件，前面的 args 都会识别为 includedPath。

---
_Source: https://npm.io/package/import-chain · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
