[Proposal] support directories in `external_resource`

26 views
Skip to first unread message

Alexander Steppke

unread,
Oct 19, 2023, 5:01:21 AM10/19/23
to elixir-lang-core
We have a need to trigger recompilation of a module when any file inside a directory is added or removed since we add the list of files at compilation time as a module attribute which is used in guard statements.

Something like

```
defmodule Example do
  @files File.ls!("some-path")
end
```

I think Phoenix's `embed_templates` would have to use a similar feature if Phoenix didn't take care of triggering recompilation itself.

Implementation wise I'd think it might probably suffice to set the `digest` as the hash of the list of files (probably recursively?) in the directory, but there might be better ways to do this.

If there is another already existing way to achieve the same please let me know, maybe I'm just missing something!

Best regards, Alex.

José Valim

unread,
Oct 19, 2023, 5:04:00 AM10/19/23
to elixir-l...@googlegroups.com
There is a callback that you implement for your modules called __mix_recompile__? Which you can implement with your own logic to detect how files and directories change. You can See how embed_templates does it for examples. :)

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/743e4012-af1b-437d-8277-97cd5bb302aan%40googlegroups.com.

Andrew Timberlake

unread,
Oct 19, 2023, 5:07:41 AM10/19/23
to elixir-lang-core
You can do this by creating a hash of the files and putting that in a module attribute.
Then use __mix_recompile__?/0 to hash the files again and compare the hashes.

—Andrew

Alexander Steppke

unread,
Oct 23, 2023, 1:48:39 AM10/23/23
to elixir-lang-core
Thanks for the feedback, I've completely forgotten about `__mix_recompile__?`! @Jose: I couldn't find anything implementing it in LiveView or Phoenix though, is it implemented differently for `embed_templates`?

José Valim

unread,
Oct 23, 2023, 2:37:02 AM10/23/23
to elixir-l...@googlegroups.com
It delegates to Phoenix.Template.

Reply all
Reply to author
Forward
0 new messages