Skip to content

Commit 403bba6

Browse files
committed
Read phoenix js assests at compile time
Stop bundling phoenix and liveview assests. Instead, read them at compile time and concatinate with app js. Solution nabbed from phoenix_live_dashboard
1 parent f718ad9 commit 403bba6

File tree

2 files changed

+53
-40
lines changed

2 files changed

+53
-40
lines changed

assets/js/app.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
1-
import "phoenix_html"
2-
import { Socket, LongPoll } from "phoenix"
3-
import { LiveSocket } from "phoenix_live_view"
4-
import topbar from "topbar"
5-
6-
import { loadAll } from "./lib/settings"
7-
8-
import Charter from "./hooks/charter"
9-
import Completer from "./hooks/completer"
10-
import Instantiator from "./hooks/instantiator"
11-
import Refresher from "./hooks/refresher"
12-
import Relativize from "./hooks/relativize"
13-
import Shortcuts from "./hooks/shortcuts"
14-
import Themer from "./hooks/themer"
15-
import Tippy from "./hooks/tippy"
16-
17-
const hooks = { Charter, Completer, Instantiator, Refresher, Relativize, Shortcuts, Themer, Tippy }
1+
// Phoenix assets are imported from dependencies
2+
import topbar from "topbar";
3+
4+
import { loadAll } from "./lib/settings";
5+
6+
import Charter from "./hooks/charter";
7+
import Completer from "./hooks/completer";
8+
import Instantiator from "./hooks/instantiator";
9+
import Refresher from "./hooks/refresher";
10+
import Relativize from "./hooks/relativize";
11+
import Shortcuts from "./hooks/shortcuts";
12+
import Themer from "./hooks/themer";
13+
import Tippy from "./hooks/tippy";
14+
15+
const hooks = {
16+
Charter,
17+
Completer,
18+
Instantiator,
19+
Refresher,
20+
Relativize,
21+
Shortcuts,
22+
Themer,
23+
Tippy,
24+
};
1825

1926
// Topbar ---
2027

21-
let topBarScheduled = undefined
28+
let topBarScheduled = undefined;
2229

2330
topbar.config({
2431
barColors: { 0: "#0284c7" },
2532
shadowColor: "rgba(0, 0, 0, .3)",
26-
})
33+
});
2734

2835
window.addEventListener("phx:page-loading-start", (info) => {
2936
if (!topBarScheduled) {
30-
topBarScheduled = setTimeout(() => topbar.show(), 500)
37+
topBarScheduled = setTimeout(() => topbar.show(), 500);
3138
}
32-
})
39+
});
3340

3441
window.addEventListener("phx:page-loading-stop", (info) => {
35-
clearTimeout(topBarScheduled)
36-
topBarScheduled = undefined
37-
topbar.hide()
38-
})
42+
clearTimeout(topBarScheduled);
43+
topBarScheduled = undefined;
44+
topbar.hide();
45+
});
3946

4047
// Mounting ---
4148

42-
const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
43-
const liveTran = document.querySelector("meta[name='live-transport']").getAttribute("content")
44-
const livePath = document.querySelector("meta[name='live-path']").getAttribute("content")
49+
const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content");
50+
const liveTran = document.querySelector("meta[name='live-transport']").getAttribute("content");
51+
const livePath = document.querySelector("meta[name='live-path']").getAttribute("content");
4552

46-
const liveSocket = new LiveSocket(livePath, Socket, {
47-
transport: liveTran === "longpoll" ? LongPoll : WebSocket,
53+
const liveSocket = new LiveView.LiveSocket(livePath, Phoenix.Socket, {
54+
transport: liveTran === "longpoll" ? Phoenix.LongPoll : WebSocket,
4855
params: { _csrf_token: csrfToken, init_state: loadAll() },
4956
hooks: hooks,
50-
})
57+
});
5158

52-
liveSocket.connect()
59+
liveSocket.connect();

lib/oban/web/components/layouts.ex

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
defmodule Oban.Web.Layouts do
22
use Oban.Web, :html
33

4+
phoenix_js_paths =
5+
for app <- ~w(phoenix phoenix_html phoenix_live_view)a do
6+
path = Application.app_dir(app, ["priv", "static", "#{app}.js"])
7+
Module.put_attribute(__MODULE__, :external_resource, path)
8+
path
9+
end
10+
411
@static_path Application.app_dir(:oban_web, ["priv", "static"])
512

6-
@external_resource Path.join(@static_path, "app.css")
7-
@external_resource Path.join(@static_path, "app.js")
13+
@external_resource css_path = Path.join(@static_path, "app.css")
14+
@external_resource js_path = Path.join(@static_path, "app.js")
815

9-
@css @static_path
10-
|> Path.join("app.css")
11-
|> File.read!()
16+
@css File.read!(css_path)
1217

13-
@js @static_path
14-
|> Path.join("app.js")
15-
|> File.read!()
18+
@js """
19+
#{for path <- phoenix_js_paths, do: path |> File.read!() |> String.replace("//# sourceMappingURL=", "// ")}
20+
#{File.read!(js_path)}
21+
"""
1622

1723
def render("app.css"), do: @css
1824
def render("app.js"), do: @js

0 commit comments

Comments
 (0)