Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional compilation in Elixir

Tags:

elixir

I have an Elixir module that contains a demo that I use in development and for integration testing with other systems. I do not want that module to be compiled for the prod environment. Is there some cute trick with mix/config I can use to make sure this module is unavailable in certain contexts?

like image 796
janders Avatar asked Mar 10 '26 05:03

janders


1 Answers

The way Phoenix solves this problem is by specifying different value for elixirc_paths in mix.exs for different environments. Here's how you can achieve that:

def project do
  [
    ..,
    elixirc_paths: elixirc_paths(Mix.env),
    ..,
  ]
end

defp elixirc_paths(:prod), do: ["lib"]
defp elixirc_paths(_),     do: ["lib", "not-prod"]

Now put your .ex files that you want to not be present in :prod inside /not-prod/ (You might want to use a better name for this...).

like image 133
Dogbert Avatar answered Mar 12 '26 13:03

Dogbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!