/phoenix-colocated-js:phoenix-colocated-jsfrom phoenix-colocated-js
Use when we need an explanation of Phoenix.LiveView.ColocatedJS and how colocated JS is compiled and imported
Ships in phoenix-colocated-js. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Phoenix.LiveView.ColocatedJS is a special HEEx :type that extracts arbitrary JavaScript from a co-located <script> tag at compile time. It generalizes colocated hooks: hooks are built on top of ColocatedJS, but you can also use it for things like web components, global listeners, or other module exports.
phoenix-colocated/my_app exposes your exports for bundlersDefine colocated JS in your HEEx template and optionally name the export:
<script :type={Phoenix.LiveView.ColocatedJS} name="MyWebComponent">
export default class MyWebComponent extends HTMLElement {
connectedCallback() {
this.innerHTML = "Hello, world!";
}
}
</script>
Then import it in your main JS entrypoint (for example assets/js/app.js):
import colocated from "phoenix-colocated/my_app";
customElements.define("my-web-component", colocated.MyWebComponent);
Notes:
name attribute controls the key on the manifest export (here MyWebComponent)name, the file is imported for side effects onlyPhoenix.LiveView.ColocatedHookDuring compilation, LiveView writes colocated JS into a phoenix-colocated directory inside your build path:
_build/$MIX_ENV/phoenix-colocated/
_build/$MIX_ENV/phoenix-colocated/my_app/
_build/$MIX_ENV/phoenix-colocated/my_app/index.js
_build/$MIX_ENV/phoenix-colocated/my_app/MyAppWeb.DemoLive/line_HASH.js
_build/$MIX_ENV/phoenix-colocated/my_dependency/MyDependency.Module/line_HASH.js
Key points:
phoenix-colocatedindex.js is the manifest you import from phoenix-colocated/my_appphoenix-colocated folder should not be committed to version controlFor Phoenix 1.8 apps using esbuild, the default config already points NODE_PATH at Mix.Project.build_path() so import "phoenix-colocated/my_app" works out of the box:
config :esbuild,
my_app: [
args:
~w(js/app.js --bundle --target=es2022 --outdir=../priv/static/assets/js --external:/fonts/* --external:/images/* --alias:@=.),
cd: Path.expand("../assets", __DIR__),
env: %{
"NODE_PATH" => [Path.expand("../deps", __DIR__), Mix.Project.build_path()]
}
]
If you use a different bundler, you must configure it so that phoenix-colocated is resolvable (for example via NODE_PATH, an alias, or a symlink into node_modules).
You can also change the target directory LiveView writes to:
config :phoenix_live_view, :colocated_js,
target_directory: Path.expand("../assets/node_modules/phoenix-colocated", __DIR__)
Warning: LiveView assumes full ownership of the :target_directory and may delete unmanaged files under it when compiling.
Colocated JS files are bundled like any other module. Imports from node_modules generally work without extra config, but imports into your app’s assets tree require care because colocated files live under _build.
Example:
def sha256(assigns) do
~H"""
<div id="sha-256" phx-hook=".Sha256">Hello World</div>
<script :type={Phoenix.LiveView.ColocatedHook} name=".Sha256">
import { sha256 } from "my-example-sha256-library";
import { reverse } from "@/vendor/vendored-file";
export default {
mounted() {
this.el.innerHTML = sha256(reverse(this.el.innerHTML));
}
};
</script>
"""
end
Guidelines:
node_modules@) to refer back into your assets folder instead of relative paths--alias=@=. by default so @/vendor/... maps into assetsIf your node_modules path is nonstandard, configure it via :node_modules_path in mix.exs:
# mix.exs
def project do
[
compilers: [:phoenix_live_view] ++ Mix.compilers(),
phoenix_live_view: [colocated_js: [node_modules_path: "assets/node_modules"]]
]
end
This is per-project and distinct from :target_directory.
Phoenix.LiveView.ColocatedJS supports several attributes on the <script> tag:
name – Name under which the default export is available from the manifest. If omitted, the script is imported for side effects only.key – Custom export key used in the manifest. Hooks use this to group exports under hooks (for example export { ... as hooks }). Must be a valid JS identifier and requires name to be set.extension – Custom extension for the extracted file. Defaults to js.manifest – Custom manifest filename instead of index.js (for example web_components.ts). Remember to update your import paths if you change this.Use this skill when you need to remember how colocated JS is compiled, named, and imported, or when deciding between colocated hooks and general colocated JS:
phx-hook, this.pushEventTo, this.handleEvent, etc.) – see the phoenix-colocated-hooks and phoenix-hooks skills.Phoenix.LiveView.ColocatedJS for JS that does not need hook semantics: web components, one-off helpers, or shared utilities colocated with templates.mix compile before your asset build (or include it in aliases) so the phoenix-colocated manifest is up to date.What SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub dvcrn/skills --plugin phoenix-colocated-js