Skip to content

Commit 4ce814e

Browse files
authored
Improve example for importing code from dependencies in colocated hooks (#4008)
This example provides a simple LiveComponent which clarifies where the imports should go when using a `ColocatedHook`.
1 parent 13ccc06 commit 4ce814e

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

lib/phoenix_live_view/colocated_js.ex

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,21 @@ defmodule Phoenix.LiveView.ColocatedJS do
113113
`esbuild`. Because colocated JS is extracted to a folder outside the regular `assets` folder,
114114
special care is necessary when you need to import other files inside the colocated JS:
115115
116-
```javascript
117-
import { someFunction } from "some-dependency";
118-
import somethingElse from "@/vendor/vendored-file";
116+
```heex
117+
def sha256(assigns) do
118+
~H"""
119+
<div id="sha-256" phx-hook=".Sha256">Hello World</div>
120+
<script :type={Phoenix.LiveView.ColocatedHook} name=".Sha256">
121+
import { sha256 } from "my-example-sha256-library"
122+
import { reverse } from "@/vendor/vendored-file"
123+
export default {
124+
mounted() {
125+
this.el.innerHTML = sha256(reverse(this.el.innerHTML))
126+
}
127+
}
128+
</script>
129+
"""
130+
end
119131
```
120132

121133
While dependencies from `node_modules` should work out of the box, you cannot simply refer to your

0 commit comments

Comments
 (0)