Vue コンポーネントの依存関係を
グラフでご案内する試み

Vue コンポーネントや TypeScript ファイルの依存関係を
自動で図に描き出し README.md に載るようにしてみた

2024-08

誰? 2024-08 時点


😀 足立 素音 (Motone Adachi) / waricoma

🎂 2000年12月25日

🏡 埼玉 → 🇰🇭 → 神戸 → 東京 now

🏫 角川ドワンゴN高 → 🇰🇭の大学 → 東京通信大学 三回生

🍶 作,鶴齢,獺祭 なんとなく好き

🧑‍💻 四ツ谷とかの Web 屋さんで業務委託 (2016〜)
 → 新宿渋谷でサラリーマン
 → アニメイト本社の近所の IT ベンチャー now

TypeScript, Vue, Express ...etc

💣 背景・課題


コンポーネント増えまくってつらくなってきた 🤮

※ 1 module に 30 ~ 80 ファイルくらい

※ Vue3, TypeScript, Jest

見づらいということは…


😢 キャッチアップや調査が大変

😇 似たようなコンポーネントやファイルを作りがち

🤮 共通化されたファイルの影響範囲をよく把握しきれないまま改修

...etc

✨ グラフの生成 (npm - dependency-cruiser)


dependency-cruiser ディペンデンシークルーザ https://npmjs.com/dependency-cruiser


✨ 依存関係の解析や検証、可視化をしてくれるツール

📁 対応形式: Vue, React, TypeScript ...etc

👮‍♀ 循環参照の警告

🔧 任意の検証ルールの設定

🗒 解析結果のレポートの出力 (JSON, HTML)

...etc

🔬 早速使ってみる (導入 1/3)



npm i dependency-cruiser -D
# 今回は自社のホームページのリポジトリで試す

🔬 早速使ってみる (導入 2/3)



> npx depcruise --init
✔ It looks like this is an ESM package. Is that correct? … no
✔ Where do your source files live? … src
✔ Do your test files live in a separate folder? … no
✔ Looks like you're using a 'tsconfig.json'. Use that? … yes
✔ Full path to your 'tsconfig.json › tsconfig.json
✔ Also regard TypeScript dependencies that exist only before compilation? … yes

  ✔ Successfully created '.dependency-cruiser.cjs'
            

🔬 早速使ってみる (導入 3/3)



npx depcruise --include-only '^src' --exclude '^node_modules' --output-type dot src | dot -T svg > ./dist/dependency-graph.svg
# graphviz が必要なのでなければ brew install graphviz (for macOS)
            

🎉

📕 README.md に載るようにして GitHub で閲覧


🤖 GitHub Actions



name: publish.yml

on:
  pull_request:
    branches: [main]
    types: [closed]

permissions:
  contents: write

jobs:
  publish:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest]
        node: ['20.x']
    if: github.event.pull_request.merged == true

    steps:
      - name: Checkout 🛎
        uses: actions/checkout@v4

      - name: Setup node env 🏗
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node }}
          check-latest: true

      - name: Get npm cache directory
          id: npm-cache-dir
          shell: bash
          run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT}

      - name: Cache node_modules 📦
          uses: actions/cache@v4
          id: npm-cache # use this to check for `cache-hit` ==> if: steps.npm-cache.outputs.cache-hit != 'true'
          with:
            path: ${{ steps.npm-cache-dir.outputs.dir }}
            key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
            restore-keys: |
              ${{ runner.os }}-node-

      - name: Install dependencies 👨🏻‍💻
        run: npm ci

      - run: npm run build

      - name: Install Graphviz
        run: sudo apt install -y graphviz

      - name: Generate dependency graph 🎨
        run: npx depcruise --include-only '^src' --exclude '^node_modules' --output-type dot src | dot -T svg > ./dist/dependency-graph.svg

      - name: Deploy to GitHub Pages 🚀
          uses: peaceiris/actions-gh-pages@v3
          with:
            github_token: ${{ secrets.GITHUB_TOKEN }}
            publish_dir: ./dist
            

main にマージされたらグラフも作っていただく 🎨

🖊 README.md


追記


## dependency-graph

<!-- GitHub Actions で作ったグラフを表示する -->
![dependency-graph](https://github.com/nightlamp-llc/nl-homepage-v3/blob/gh-pages/dependency-graph.svg?raw=true)
            

✨ 今後


  1. 他の --output-type も試していきたい
    (今のところ dot が良さそうではあるが)
  2. svg 上のコンポーネントやファイルをクリックで GitHub 上の該当のファイルが開くようにしたい
    (生成されたグラフを見た感じ加工すればできそう)
  3. ファイルの関係を視覚的に表示してくれる Visual Studio Code 互換エディタなるものも試してみたい
    https://haystackeditor.com/

ご清聴ありがとうございました🙇