I’ve got some external data that I was hoping to embed in my Application as a compile time string.
I got it working by making a module attribute (evaluated at compile time) that does a
File.read of the
My issue is that it seems to be difficult to get the compiler to notice changes to this file as even
mix compile --force doesn’t always update things.
Any other techniques for this that might work?
Have you tried using the
@external_resource module attribute?
Awesome thanks! I will check that out!
You can even watch a whole folder if you’d like. There’s a good example of that in the docs:
In case it helps anyone else, I ended up using the directory watcher version to automatically embed a directory of lua scripts into my app like this:
defmodule MyApp.Examples do
paths = Path.join([:code.priv_dir(:my_app), "lua", "*.lua"]) |> Path.wildcard()
for path <- paths do
script = File.read!(path)
name = Path.basename(path)
def get(name), do: @scripts_by_name[name]
def __mix_recompile__?() do
Path.join([:code.priv_dir(:my_app), "lua", "*.lua"]) |> Path.wildcard() |> :erlang.md5() != @paths_hash