PhoenixAssets.Generated.Watcher (PhoenixAssets v0.1.0)

View Source

Regenerates frontend contracts when backend source changes, in development.

Subscribes (via :file_system) to the host application's Elixir source and gettext directories. On a change it debounces briefly, reloads the host's code through Phoenix.CodeReloader (when an endpoint is configured and the reloader is running), and then runs PhoenixAssets.Generated.generate/1. The reload step matters: the file event fires on save, before anything recompiles the edited module -- without it, contracts would be generated from the previous code state and stay one edit behind. Because generation is content-gated, a change that does not affect any contract performs no write and triggers no Vite HMR; a change that does updates exactly the affected files, which Vite then hot reloads.

The watcher never watches the generated-output directory (it lives under the asset root, not under lib/), so it can never trigger itself -- there is no feedback loop.

Summary

Functions

Returns a specification to start this module under a supervisor.

Recompiles the host's changed modules through Phoenix.CodeReloader.

Starts the watcher.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

reload_code(arg1)

@spec reload_code(PhoenixAssets.Context.t()) :: :ok

Recompiles the host's changed modules through Phoenix.CodeReloader.

A no-op when no endpoint is configured, Phoenix.CodeReloader is not available, or the endpoint's reloader is not running (e.g. code_reloader: false) -- regeneration then proceeds against the currently loaded code.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts the watcher.

Options:

  • :ctx (required) -- the PhoenixAssets.Context to regenerate from.
  • :dirs -- directories to watch (default ["lib", "priv/gettext"], filtered to those that exist).
  • :debounce_ms -- coalescing window in milliseconds (default 150).
  • :reload_fun -- the code-reload hook run before each regeneration (default reload_code/1; overridable for tests and embedders).
  • :name -- the registered name (default the module).