cvkmohan
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
soup
I think that’s out-of-scope for the dev watchers config, per the docs
Basically the
:watchersoption 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 watchto also track*.heexfile changes inlib/(or thepriv/static/assets/**.cssor 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.
cvkmohan
Could I explain my problem clearly?
josevalim
My understanding is that @soup’s answer is correct. You have to ask Vite JS to watch the template files too.
cvkmohan
Sorry, if this is looking down right simple and silly.
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.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.soup
The default webpack generator would have been pre-configured with the right paths probably.
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
Tailwind will be using
tailwind.conf.js’scontent: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.exand the live reloader from the equation first. Get yourtailwindandvitebuilders and watchers working manually from the command line, in a way that doesn’t fight each other, then just copy the setup intodev.ex, and where ever you are also running them for prod builds.derek-zhou
My advice may not solve your problem. I don’t use tailwind’s utility classes in my templates directly; I always wrap them in my custom css file into synmatic classes using
@apply. This way you would have a cleaner template file, also the watcher problem just disappearscvkmohan
Thanks for the detailed explanations @soup !

I think, I got a lot of new knowledge irrespective of what happens to my problem.
I was actually trying parcel-bundler/parcel-css: A CSS parser, transformer, and minifier written in Rust. (github.com) to process the CSS part of my Phoenix application.
I was looking for such an alternative because - tailwind standalone does not honor the postcss.config.js - and - it looks like the standalone is not supporting nested css.
To cut long things short - I am running
parcel watchas a npm task - and - added the npm watch as a watcher inconfig/dev.exs- This setup is working well and fast when we start the server - however,parcelis not recompiling when I change the template files, even thoughtailwind.config.jsis present and contains thecontentclause.Yeah - looks like too many tools clashing with each other.
On a side note - probably, we need a nice CSS framework which does not depend on Javascript. Though
tailwindcssis a CSS only framework without any JS - it relies so heavily on JS toolchain that, it is a big put-off.May be there is an inherent dichotomy - Phoenix works great without node and the partnering tailwindcss is heavily reliant on JS toolchain.
Hope a better solution is around the horizon.
cvkmohan
I fully agree. May be we do need a library with sane
@applystatements - something similar to daisyUI — Tailwind CSS ComponentsBut, the problem here is not the watcher on the
assetsfolder. It is more on the Phoenix Lib folder.derek-zhou
If you have 100% synmatic classes in your template, you don’t need to watch the lib dir from the tailwind’s side. The lib and the cas would’ve been decoupled.
cvkmohan
Beautiful. Did not think that way. I will try that. Unfortunately Reusing Styles - Tailwind CSS Tailwind authors do not believe in it.
Looks like for Phoenix LiveView Bulma: Free, open source, and modern CSS framework based on Flexbox is the best suited CSS framework. No JS, minimal tooling - modern - can be processed with DartSass.