cvkmohan
Phoenix Asset Management - Watchers
I am finding it a bit difficult to understand asset management in Phoenix - Asset Management — Phoenix v1.6.6 (hexdocs.pm) - I was going through the watchers part.
I am using vitejs to bundle assets. I have a vite watch task defined in package.json
So, in my config/dev.exs I have a watcher defined
npm: ["run", "watch", cd: Path.expand("../assets", __DIR__)]
I understood the above like, keep watching the assets directory - any changes there, run the vite watch again, so that assets get rebuilt. In fact, it is happening exactly like that.
Similarly, in the same config/dev.exs we have live_reload which watches the phoenix/elixir part of files and reloads the browser. So, any changes I make to the markup etc. this reload does take care.
Now, if we are using tailwindcss, a change in the heex files, should also trigger the vite watch task because, if we add any new styles in the templates, the new styles have to be added by the vite parser - mainly because of JIT nature of tailwindcss.
Now, my question is - how do we tell the watcher in the config/dev.exs that it has to rebuild or call the vite watch task even when we change the template files?
Presently, if I end the mix phx.server task and restart again, the styles are getting reflected. Otherwise, it is not working well.
Most Liked
soup
I think that’s out-of-scope for the dev watchers config, per the docs
:watchers- a set of watchers to run alongside your server. It expects a list of tuples containing the executable and its arguments. Watchers are guaranteed to run in the application directory
Basically the :watchers option just says “hey also run these things”, but it doesn’t actually “watch” for changes and then run them.
So what you want to do is configure vite watch to also track *.heex file changes in lib/ (or the priv/static/assets/**.css or whatever is the actual change you want to get picked up).
I think the term might be a bit confusingly labelled but there probably also isn’t a better choice.
josevalim
soup
The default webpack generator would have been pre-configured with the right paths probably.
- I tried specifying the template files as a glob
lib/**/*.*ex- and - the live reload is taking close to 10 seconds or more to reload the page - with unpredictable results. Sometimes, the new styles are getting picked up and sometimes, only cached styles are working.
Sometimes I see this with tailwind because the JIT used to write its results out in parts, you can configure the live reloader to wait an interval before it messages phoenix: GitHub - phoenixframework/phoenix_live_reload: Provides live-reload functionality for Phoenix · GitHub
- With tailwind standalone we were putting
tailwind: {Tailwind, :install_and_run, [:default, ~w(--watch)]}in theconfig/dev.exs- that~w(--watch)watches which folders? I guess, I need to specify something similar.
Tailwind will be using tailwind.conf.js’s content: key to know what to look for. I think it’s content, it used to be purge until 3.0. That file may not exist, tailwind might use a default one if it is missing.
Probably your best bet is to remove dev.ex and the live reloader from the equation first. Get your tailwind and vite builders and watchers working manually from the command line, in a way that doesn’t fight each other, then just copy the setup into dev.ex, and where ever you are also running them for prod builds.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










