npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

import-chain

v0.2.2

Published

import chain

Downloads

32

Readme

import-chain

show which file import the target file

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

可以用它来做什么?

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

这个库导出一个函数 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。