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)
This commit is contained in:
José Valim 2024-08-07 01:14:45 +02:00
parent bdf17ae482
commit edefa6649a
4 changed files with 52 additions and 50 deletions

View file

@ -217,26 +217,6 @@ defmodule Livebook.Config do
Application.get_env(:livebook, LivebookWeb.Endpoint)[:http][:port]
end
@doc """
Returns the base url path for the Livebook endpoint.
"""
@spec base_url_path() :: String.t()
def base_url_path() do
path = Application.get_env(:livebook, LivebookWeb.Endpoint)[:url][:path]
String.trim_trailing(path, "/")
end
@doc """
Returns the base url path for Livebook public endpoints (health & assets)
"""
@spec public_base_url_path() :: String.t()
def public_base_url_path() do
case Application.get_env(:livebook, :public_base_url_path) do
nil -> base_url_path()
path -> String.trim_trailing(path, "/")
end
end
@doc """
Returns the configured port for the iframe endpoint.
"""

View file

@ -81,7 +81,7 @@ defmodule LivebookWeb do
# absolute URL helpers. We don't import sigil_p either, because
# we override it.
import Phoenix.VerifiedRoutes, only: []
import LivebookWeb, only: [sigil_p: 2]
import LivebookWeb.VerifiedRoutes, only: [sigil_p: 2]
end
end
@ -91,31 +91,4 @@ defmodule LivebookWeb do
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
# Overrides
require Phoenix.VerifiedRoutes
defmacro sigil_p({:<<>>, _meta, ["/public/" <> _ | _]} = route, extra) do
# We allow configuring a base path for all routes and we configure
# Phoenix to use it. However, we have an additional configuration
# for base path applying only to /public. We use a custom sigil_p
# to insert this base path if needed.
quote do
path = Phoenix.VerifiedRoutes.sigil_p(unquote(route), unquote(extra))
LivebookWeb.__rewrite_public_base_path__(path)
end
end
defmacro sigil_p(route, extra) do
quote do
Phoenix.VerifiedRoutes.sigil_p(unquote(route), unquote(extra))
end
end
def __rewrite_public_base_path__(path) do
base_url_path = Livebook.Config.base_url_path()
public_base_url_path = Livebook.Config.public_base_url_path()
^base_url_path <> rest = path
public_base_url_path <> rest
end
end

View file

@ -12,8 +12,8 @@
</.live_title>
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />
<script>
window.LIVEBOOK_BASE_URL_PATH = "<%= Livebook.Config.base_url_path() %>";
window.LIVEBOOK_PUBLIC_BASE_URL_PATH = "<%= Livebook.Config.public_base_url_path() %>"
window.LIVEBOOK_BASE_URL_PATH = "<%= LivebookWeb.VerifiedRoutes.base_url_path() %>";
window.LIVEBOOK_PUBLIC_BASE_URL_PATH = "<%= LivebookWeb.VerifiedRoutes.public_base_url_path() %>"
</script>
<LivebookWeb.LayoutComponents.dev_script />
<%!-- This prevents the script to be loaded twice in Chrome --%>

View file

@ -0,0 +1,49 @@
defmodule LivebookWeb.VerifiedRoutes do
require Phoenix.VerifiedRoutes
defmacro sigil_p({:<<>>, _meta, ["/public/" <> _ | _]} = route, extra) do
# We allow configuring a base path for all routes and we configure
# Phoenix to use it. However, we have an additional configuration
# for base path applying only to /public. We use a custom sigil_p
# to insert this base path if needed.
quote do
path = Phoenix.VerifiedRoutes.sigil_p(unquote(route), unquote(extra))
LivebookWeb.VerifiedRoutes.__rewrite_public_base_path__(path)
end
end
defmacro sigil_p(route, extra) do
quote do
Phoenix.VerifiedRoutes.sigil_p(unquote(route), unquote(extra))
end
end
@doc """
Returns the base url path for the Livebook endpoint.
"""
@spec base_url_path() :: String.t()
def base_url_path() do
# Use fully qualified module name to avoid compile time dependencies
path = Application.get_env(:livebook, :"Elixir.LivebookWeb.Endpoint")[:url][:path]
String.trim_trailing(path, "/")
end
@doc """
Returns the base url path for Livebook public endpoints (health & assets)
"""
@spec public_base_url_path() :: String.t()
def public_base_url_path() do
case Application.get_env(:livebook, :public_base_url_path) do
nil -> base_url_path()
path -> String.trim_trailing(path, "/")
end
end
@doc false
def __rewrite_public_base_path__(path) do
base_url_path = base_url_path()
public_base_url_path = public_base_url_path()
^base_url_path <> rest = path
public_base_url_path <> rest
end
end