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 🙏

© 2026 – Pkg Stats / Ryan Hefner

astro-import-tree

v0.1.3

Published

A library to parse Astro page URL paths and import trees using AST

Readme

astro-import-tree

ASTを使用してAstroページのURLパスとインポートツリーを解析するライブラリです。

機能

  • AstroプロジェクトのURLパスとインポート依存関係のマッピングを分析
  • すべてのインポート(.astro.js.jsx.ts.tsxファイル)を再帰的に追跡
  • 静的インポートと動的インポートの両方をサポート
  • 構造化データまたはLLMフレンドリーなテキスト出力を生成
  • frontmatterやコンポーネントインポートなどのAstro固有のパターンを処理

インストール

npm install astro-import-tree

# またはCLI使用のためにグローバルインストール
npm install -g astro-import-tree

CLI使用方法

# LLMフレンドリーなインポートツリーを表示
astro-import-tree llm ./my-astro-project

# JSONインポートツリーを表示
astro-import-tree json ./my-astro-project

# カスタムpagesディレクトリを指定
astro-import-tree llm ./my-astro-project --pages-dir src/content

# 警告メッセージを抑制
astro-import-tree llm ./my-astro-project --quiet

# ヘルプを表示
astro-import-tree --help

Yarnでの開発

開発時は、yarnコマンドを使用してCLIをデバッグできます:

# 引数付きでCLIを実行
yarn cli llm ./testdata/astro

# LLM出力のクイック実行
yarn cli:llm ./testdata/astro

プログラムでの使い方

TypeScript/ES Modules

// 推奨: 名前付きインポート
import { astroImportTree } from 'astro-import-tree';

// Astroプロジェクトを解析
const tree = await astroImportTree.parse({
  dir: './', // Astroプロジェクトのルートディレクトリ
  pagesDir: 'src/pages' // オプション、デフォルトは'src/pages'
});

console.log(tree);
// 出力:
// {
//   "pages": [
//     {
//       "path": "/",
//       "imports": [
//         "@src/pages/index.astro",
//         "@src/components/Header.astro",
//         "@src/components/Hero.astro",
//         "@src/components/Hero/Image.astro",
//         "@src/components/Footer.astro"
//       ]
//     },
//     {
//       "path": "/about",
//       "imports": [
//         "@src/pages/about.astro",
//         "@src/components/Header.astro",
//         "@src/components/Footer.astro"
//       ]
//     }
//   ]
// }

// LLMフレンドリーなテキストを生成
const llmText = astroImportTree.toLLM(tree);
console.log(llmText);
// 出力:
// ## /, /index.html
// 
// - @src/pages/index.astro
// - @src/components/Header.astro
// - @src/components/Hero.astro
// - @src/components/Hero/Image.astro
// - @src/components/Footer.astro
// 
// ## /about, /about/index.html
// 
// - @src/pages/about.astro
// - @src/components/Header.astro
// - @src/components/Footer.astro

CommonJS

const astroImportTree = require('astro-import-tree').default;

// Astroプロジェクトを解析
const tree = await astroImportTree.parse({
  dir: './',
  pagesDir: 'src/pages'
});

その他のインポート方法

// 複数のインスタンスが必要な場合はクラスを直接インポート
import { AstroImportTree } from 'astro-import-tree';
const myInstance = new AstroImportTree();

// デフォルトインポート(一部の環境では.defaultアクセスが必要)
import astroImportTreeModule from 'astro-import-tree';
const astroImportTree = astroImportTreeModule.default || astroImportTreeModule;

API

astroImportTree.parse(options)

Astroプロジェクトを解析してインポートツリーを返します。

オプション

  • dir (string, 必須): Astroプロジェクトのルートディレクトリ
  • pagesDir (string, オプション): ルートからの相対的なpagesディレクトリ。デフォルトは'src/pages'

戻り値

ImportTreeオブジェクトを解決するPromiseを返します:

interface ImportTree {
  pages: ImportTreePage[];
}

interface ImportTreePage {
  path: string;      // URLパス(例:"/", "/about")
  imports: string[]; // インポートパスの配列(例:"@src/components/Header.astro")
}

astroImportTree.toLLM(tree)

インポートツリーをLLMフレンドリーなテキスト形式に変換します。

パラメータ

  • tree (ImportTree): parse()から取得したインポートツリーオブジェクト

戻り値

各ページのインポート情報をフォーマットした文字列を返します。

動作原理

  1. ページ検出: pagesディレクトリ内のすべての.astroファイルを検索
  2. URLマッピング: ファイルパスをURLパスに変換(例:src/pages/about.astro/about
  3. インポート分析:
    • Astroのfrontmatterからインポートを解析
    • JavaScript/TypeScriptのインポートを分析
    • 相対インポートとエイリアスインポートを追跡
    • 動的インポートとAstro.glob()を処理
  4. 再帰的トラバース: すべてのインポートを追跡して完全な依存関係ツリーを構築
  5. 出力生成: データを構造化JSONまたは読みやすいテキストとしてフォーマット

インポートパスの形式

ライブラリは、すべてのインポートパスをプロジェクトルートからの相対パスに@プレフィックスを付けてフォーマットします:

  • @src/pages/index.astro
  • @src/components/Header.astro
  • @src/layouts/BaseLayout.astro

ライセンス

MIT