Skip to content

Commit 8b2fd0b

Browse files
committed
Use a reference to consuming projects Endpoint module to generate digested asset paths in production.
1 parent 6310546 commit 8b2fd0b

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ import topbar from 'topbar';
5050

5151
You'll also need to replace the contents of `assets/vendor/topbar.js` with a wrapped version that supports ESM, like this [from jsDelivr](https://cdn.jsdelivr.net/npm/topbar@2.0.0/topbar.js/+esm).
5252

53-
In `lib/<project>/components/layouts/root.html.heex` replace the `app.js` `<script>` tag with:
53+
In `lib/<project>/components/layouts/root.html.heex` replace the `app.js` `<script>` tag.
54+
55+
Be sure to use your own project's module name in place of `YourAppWeb`.
5456

5557
```html
5658
<script type="importmap">
57-
<%= raw PhoenixImportmap.importmap() %>
59+
<%= raw PhoenixImportmap.importmap(YourAppWeb.Endpoint) %>
5860
</script>
5961
<script type="module">
6062
import 'app';

lib/phoenix_importmap.ex

+8-6
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,16 @@ defmodule PhoenixImportmap do
4848
4949
You'll also need to replace the contents of `assets/vendor/topbar.js` with a wrapped version that supports ESM, like this [from jsDelivr](https://cdn.jsdelivr.net/npm/topbar@2.0.0/topbar.js/+esm).
5050
51-
In `lib/<project>/components/layouts/root.html.heex` replace the `app.js` `<script>` tag with:
51+
In `lib/<project>/components/layouts/root.html.heex` replace the `app.js` `<script>` tag.
52+
53+
Be sure to use your own project's module name in place of `YourAppWeb`.
5254
5355
```html
5456
<script type="importmap">
55-
<%= raw PhoenixImportmap.importmap() %>
57+
<%= raw PhoenixImportmap.importmap(YourAppWeb.Endpoint) %>
5658
</script>
5759
<script type="module">
58-
import "app";
60+
import 'app';
5961
</script>
6062
```
6163
@@ -87,11 +89,11 @@ defmodule PhoenixImportmap do
8789
@doc """
8890
Returns a JSON-formatted importmap based on your application configuration.
8991
90-
Asset paths will have `:public_asset_path_prefix` stripped out.
92+
Requires `YourAppWeb.Endpoint` to be passed in for path generation.
9193
"""
92-
def importmap() do
94+
def importmap(endpoint) do
9395
application_importmap()
94-
|> Importmap.prepare()
96+
|> Importmap.prepare(endpoint)
9597
|> Importmap.json()
9698
end
9799

lib/phoenix_importmap/importmap.ex

+6-3
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,21 @@ defmodule PhoenixImportmap.Importmap do
3939
end
4040

4141
@doc """
42-
Strips `:public_asset_path_prefix` from asset paths so they may be resolved
42+
Maps local paths from the configured importmap to the location they are being served from.
43+
44+
- Strips `:public_asset_path_prefix` from asset paths so they may be resolved
4345
by `Plug.Static`.
46+
- Uses `YourAppWeb.Endpoint.static_path/1` to determine whether to use digest URLs.
4447
"""
45-
def prepare(importmap = %{}) do
48+
def prepare(importmap = %{}, endpoint) do
4649
%{
4750
imports:
4851
importmap
4952
|> Enum.reduce(%{}, fn {specifier, path}, acc ->
5053
Map.put(
5154
acc,
5255
specifier,
53-
Asset.public_path(path)
56+
Asset.public_path(path) |> endpoint.static_path()
5457
)
5558
end)
5659
}

test/phoenix_importmap_test.exs

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ defmodule PhoenixImportmapTest do
77

88
@moduletag :tmp_dir
99

10+
defmodule MockEndpoint do
11+
def static_path(path), do: "#{path}?busted=t"
12+
end
13+
1014
setup %{tmp_dir: tmp_dir} do
1115
relative_tmp_dir = Util.relative_path(tmp_dir)
1216

@@ -27,7 +31,7 @@ defmodule PhoenixImportmapTest do
2731
end
2832

2933
test "importmap" do
30-
assert PhoenixImportmap.importmap() ==
31-
"{\"imports\":{\"remote\":\"https://cdn.es6/package.js\",\"app\":\"/assets/app.js\"}}"
34+
assert PhoenixImportmap.importmap(MockEndpoint) ==
35+
"{\"imports\":{\"remote\":\"https://cdn.es6/package.js?busted=t\",\"app\":\"/assets/app.js?busted=t\"}}"
3236
end
3337
end

0 commit comments

Comments
 (0)