From edefa6649ab783c1c2f6a2c067b44fa6dc9de642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 7 Aug 2024 01:14:45 +0200 Subject: [PATCH] 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) --- lib/livebook/config.ex | 20 -------- lib/livebook_web.ex | 29 +---------- .../components/layouts/root.html.heex | 4 +- lib/livebook_web/verified_routes.ex | 49 +++++++++++++++++++ 4 files changed, 52 insertions(+), 50 deletions(-) create mode 100644 lib/livebook_web/verified_routes.ex diff --git a/lib/livebook/config.ex b/lib/livebook/config.ex index 616790fdf..70534294d 100644 --- a/lib/livebook/config.ex +++ b/lib/livebook/config.ex @@ -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. """ diff --git a/lib/livebook_web.ex b/lib/livebook_web.ex index 7acaebde3..4d4fd4b09 100644 --- a/lib/livebook_web.ex +++ b/lib/livebook_web.ex @@ -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 diff --git a/lib/livebook_web/components/layouts/root.html.heex b/lib/livebook_web/components/layouts/root.html.heex index 9d155ec79..fbd5fbb73 100644 --- a/lib/livebook_web/components/layouts/root.html.heex +++ b/lib/livebook_web/components/layouts/root.html.heex @@ -12,8 +12,8 @@ <%!-- This prevents the script to be loaded twice in Chrome --%> diff --git a/lib/livebook_web/verified_routes.ex b/lib/livebook_web/verified_routes.ex new file mode 100644 index 000000000..9bbb7ed40 --- /dev/null +++ b/lib/livebook_web/verified_routes.ex @@ -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