marcandre
Avoiding compile dependencies in router via plugs
I’m fighting against a mess of compile dependencies.
One issue is that many things have a runtime dependency on the router (ok) and that the router has multiple compile-time dependencies on views and layouts (not ok).
I notice that changing the modules of these views to Module.concat solves the problem:
# router.ex
# introduces a compile-time dependency
pipeline :example do
plug(:put_layout, {MyApp.LayoutView, :example})
end
# no dependency
pipeline :example do
plug(:put_layout, {Module.concat(:MyApp, :LayoutView), :example})
end
-
Is this expected / am I missing something?
-
Before I write a macro to do this, is there a better way to resolve this?
This kind of issue does not seem unique.
I was naively thinking that this pattern was pretty natural and would be handled properly, either by put_layout accepting a string for the module name, or by plug being a smart macro that could avoid the compile-time dependency altogether, or by pipelines being in a different files from routes for example.
Most Liked
marcandre
Very surprising to me, but moving a plug module from pipe_through and by using an intermediary pipeline removes the compile-time dependency?!
Again, is that expected, where is that documented, and can’t this be improved?
$ mix xref graph --source lib/bobby_web/router.ex --label compile | wc -l
113
$ git stash pop && git diff
lib/bobby_web/router.ex
──────────────────────────────────┐
429: defmodule BobbyWeb.Router do │
──────────────────────────────────┘
+ pipeline :active_policies do
+ plug Plugs.ActivePolicies
+ end
scope "/", BobbyWeb do
- pipe_through([:browser, Plugs.ActivePolicies])
+ pipe_through([:browser, :active_policies])
$ mix xref graph --source lib/bobby_web/router.ex --label compile | wc -l
86







