Skip to content

Commit edefa66

Browse files
committed
Avoid compile-connected dependencies
Before this commit, LivebookWeb had runtime dependencies into the project, causing large compilation cycles. Using the following command in Elixir v1.17.3+ $ mix xref graph --format stats --label compile-connected Would reveal: Top 10 files with most incoming dependencies: * lib/livebook_web.ex (97) * lib/livebook/config.ex (3) * proto/lib/livebook_proto/deployment_group.pb.ex (2) After this patch: Top 10 files with most incoming dependencies: * lib/livebook/config.ex (3) * proto/lib/livebook_proto/deployment_group.pb.ex (2) * lib/livebook_web/plugs/memory_provider.ex (2)
1 parent bdf17ae commit edefa66

File tree

4 files changed

+52
-50
lines changed

4 files changed

+52
-50
lines changed

lib/livebook/config.ex

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -217,26 +217,6 @@ defmodule Livebook.Config do
217217
Application.get_env(:livebook, LivebookWeb.Endpoint)[:http][:port]
218218
end
219219

220-
@doc """
221-
Returns the base url path for the Livebook endpoint.
222-
"""
223-
@spec base_url_path() :: String.t()
224-
def base_url_path() do
225-
path = Application.get_env(:livebook, LivebookWeb.Endpoint)[:url][:path]
226-
String.trim_trailing(path, "/")
227-
end
228-
229-
@doc """
230-
Returns the base url path for Livebook public endpoints (health & assets)
231-
"""
232-
@spec public_base_url_path() :: String.t()
233-
def public_base_url_path() do
234-
case Application.get_env(:livebook, :public_base_url_path) do
235-
nil -> base_url_path()
236-
path -> String.trim_trailing(path, "/")
237-
end
238-
end
239-
240220
@doc """
241221
Returns the configured port for the iframe endpoint.
242222
"""

lib/livebook_web.ex

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ defmodule LivebookWeb do
8181
# absolute URL helpers. We don't import sigil_p either, because
8282
# we override it.
8383
import Phoenix.VerifiedRoutes, only: []
84-
import LivebookWeb, only: [sigil_p: 2]
84+
import LivebookWeb.VerifiedRoutes, only: [sigil_p: 2]
8585
end
8686
end
8787

@@ -91,31 +91,4 @@ defmodule LivebookWeb do
9191
defmacro __using__(which) when is_atom(which) do
9292
apply(__MODULE__, which, [])
9393
end
94-
95-
# Overrides
96-
require Phoenix.VerifiedRoutes
97-
98-
defmacro sigil_p({:<<>>, _meta, ["/public/" <> _ | _]} = route, extra) do
99-
# We allow configuring a base path for all routes and we configure
100-
# Phoenix to use it. However, we have an additional configuration
101-
# for base path applying only to /public. We use a custom sigil_p
102-
# to insert this base path if needed.
103-
quote do
104-
path = Phoenix.VerifiedRoutes.sigil_p(unquote(route), unquote(extra))
105-
LivebookWeb.__rewrite_public_base_path__(path)
106-
end
107-
end
108-
109-
defmacro sigil_p(route, extra) do
110-
quote do
111-
Phoenix.VerifiedRoutes.sigil_p(unquote(route), unquote(extra))
112-
end
113-
end
114-
115-
def __rewrite_public_base_path__(path) do
116-
base_url_path = Livebook.Config.base_url_path()
117-
public_base_url_path = Livebook.Config.public_base_url_path()
118-
^base_url_path <> rest = path
119-
public_base_url_path <> rest
120-
end
12194
end

lib/livebook_web/components/layouts/root.html.heex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
</.live_title>
1313
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
1414
<script>
15-
window.LIVEBOOK_BASE_URL_PATH = "<%= Livebook.Config.base_url_path() %>";
16-
window.LIVEBOOK_PUBLIC_BASE_URL_PATH = "<%= Livebook.Config.public_base_url_path() %>"
15+
window.LIVEBOOK_BASE_URL_PATH = "<%= LivebookWeb.VerifiedRoutes.base_url_path() %>";
16+
window.LIVEBOOK_PUBLIC_BASE_URL_PATH = "<%= LivebookWeb.VerifiedRoutes.public_base_url_path() %>"
1717
</script>
1818
<LivebookWeb.LayoutComponents.dev_script />
1919
<%!-- This prevents the script to be loaded twice in Chrome --%>

lib/livebook_web/verified_routes.ex

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
defmodule LivebookWeb.VerifiedRoutes do
2+
require Phoenix.VerifiedRoutes
3+
4+
defmacro sigil_p({:<<>>, _meta, ["/public/" <> _ | _]} = route, extra) do
5+
# We allow configuring a base path for all routes and we configure
6+
# Phoenix to use it. However, we have an additional configuration
7+
# for base path applying only to /public. We use a custom sigil_p
8+
# to insert this base path if needed.
9+
quote do
10+
path = Phoenix.VerifiedRoutes.sigil_p(unquote(route), unquote(extra))
11+
LivebookWeb.VerifiedRoutes.__rewrite_public_base_path__(path)
12+
end
13+
end
14+
15+
defmacro sigil_p(route, extra) do
16+
quote do
17+
Phoenix.VerifiedRoutes.sigil_p(unquote(route), unquote(extra))
18+
end
19+
end
20+
21+
@doc """
22+
Returns the base url path for the Livebook endpoint.
23+
"""
24+
@spec base_url_path() :: String.t()
25+
def base_url_path() do
26+
# Use fully qualified module name to avoid compile time dependencies
27+
path = Application.get_env(:livebook, :"Elixir.LivebookWeb.Endpoint")[:url][:path]
28+
String.trim_trailing(path, "/")
29+
end
30+
31+
@doc """
32+
Returns the base url path for Livebook public endpoints (health & assets)
33+
"""
34+
@spec public_base_url_path() :: String.t()
35+
def public_base_url_path() do
36+
case Application.get_env(:livebook, :public_base_url_path) do
37+
nil -> base_url_path()
38+
path -> String.trim_trailing(path, "/")
39+
end
40+
end
41+
42+
@doc false
43+
def __rewrite_public_base_path__(path) do
44+
base_url_path = base_url_path()
45+
public_base_url_path = public_base_url_path()
46+
^base_url_path <> rest = path
47+
public_base_url_path <> rest
48+
end
49+
end

0 commit comments

Comments
 (0)