I’m looking for a way to include an optional dependency into my library project. This is about including brotli which is a stateless library (not an OTP app).
The use case is to allow brotli compression for an HTTP caching library, but since this library compiles native code and gzip as an alternative also exists (with 15-20% size overhead compared with brotli), I don’t want to force the users to have to compile native code if they’re fine with gzip.
Elixir’s mix allows that by doing:
{:foobar, "~> 1.0", optional: true}
but I couldn’t find such an option with rebar3. Have I missed something and, if not, what is the typical workaround/pattern for rebar projects?
Instead of having a literal dependency to any specific compression library you could allow users to configure a callback module for compression. That means you don’t have to worry about supporting building NIFs in various environments.
There isn’t anything to do in rebar.config. But you’ll want to include the brotli application in optional_applications of your .app.src file, Erlang -- app
The user then includes brotli in their rebar.config the same as the user would have to do in their mix.exs.
Indeed I misunderstood what an OTP application is. In my mind an OTP app was a program that implements the application callbacks but it is wrong:
mod
Specifies the application callback module and a start argument, see application(3).
Key mod is necessary for an application implemented as a supervision tree, otherwise the application controller does not know how to start it. mod can be omitted for applications without processes, typically code libraries, for example, STDLIB.