実現したいこと

テーマをthemes/配下に置いただけのサイトでも、Hugoは内部的にプロジェクトとテーマを1つの依存関係グラフとして扱っている。この依存関係を確認したい。

hugo mod graphを使うと、プロジェクトとテーマの依存関係を表示できる。

使い方

hugo mod graphを実行するだけである。

$ hugo mod graph
project example-theme

go.modが無くても動く

themes/配下にテーマのディレクトリを置いているだけでも、hugo mod graphは正常に動作する。

hugo mod --helpにも次のように明記されている。

Most operations here requires a Go version installed on your system (>= Go 1.12) and the relevant VCS client (typically Git).
This is not needed if you only operate on modules inside /themes or if you have vendored them via "hugo mod vendor".

themes/配下のテーマだけを扱う操作にはGo本体のインストールが不要、と書かれているとおりである。

go.modが必要な操作もある

hugo modのすべてのサブコマンドがgo.mod無しで動くわけではない。hugo mod verifyは実際にGoコマンドを呼び出すため、go.modが無いとエラーになる。

$ hugo mod verify
go: go.mod file not found in current directory or any parent directory; see 'go help modules'
Error: failed to execute 'go [mod verify]': failed to execute binary "go" with args [mod verify]: go: go.mod file not found in current directory or any parent directory; see 'go help modules'

hugo mod graphはGo本体を呼び出さずHugo内部でモジュール構成を解決するためgo.modが無くても動くが、verifyはGoモジュールとしての検証をGo本体に委ねるためgo.modが前提になる。

それぞれのモジュールが提供する内容はhugo config mountsで確認する

hugo mod graphは依存関係の存在だけを示し、各モジュールが具体的にどのディレクトリを提供しているかまでは分からない。hugo config mountsを使うと、モジュールごとのマウント元・マウント先を確認できる。

$ hugo config mounts
{
   "path": "project",
   "dir": "[project-root]",
   "mounts": [
      { "source": "package.json", "target": "assets/_jsconfig/package.json" },
      { "source": "content", "target": "content" },
      { "source": "data", "target": "data" },
      { "source": "layouts", "target": "layouts" },
      { "source": "i18n", "target": "i18n" },
      { "source": "archetypes", "target": "archetypes" },
      { "source": "assets", "target": "assets" },
      { "source": "static", "target": "static" }
   ]
},
{
   "path": "example-theme",
   "owner": "project",
   "dir": "[project-root]/themes/example-theme/",
   "mounts": [
      { "source": "archetypes", "target": "archetypes" },
      { "source": "assets", "target": "assets" },
      { "source": "layouts", "target": "layouts" }
   ]
}

example-themeモジュールが提供するのはarchetypesassetslayoutsの3つだけで、contentstaticはプロジェクト側にしか無い。プロジェクト側のpackage.jsonassets/_jsconfig/package.jsonとしてマウントされている点も、この出力で分かる。

参考: 【Hugo】hugo configで最終的に有効な設定値を確認する

参考: hugo mod graph | Hugo