Closed spaceemotion closed 8 hours ago
From digging through the source code a bit, it seems that the module resolution is actually done by this bit - if I understand correctly:
Does that mean, custom virtual modules are not supported in CSS files?
Hey! @spaceemotion. Yeah that's right we don't consult the Vite module resolution when resolving imports right now. Two things you can try to work around it:
@import "fontawesome/fontawesome.css" layer(base)
or are they not providing any static files for you?postcss-import
before the Tailwind plugin as a workaround.I agree with you that this is unexpected though and that the Vite plugin should use the Vite module resolution system and not a custom one.
The SVG-Core version of FontAwesome does not provide a static CSS, as they generate it on the fly based on the config. They usually add the styles when FontAwesome loads, but since we want to build a (mostly) static site, this means that icons would look weird if the JavaScript hasn't loaded yet.
For the time being I had it generate a static version, and "hard-coded" an import to that one. Not ideal, but...
Thanks for the tip with the postcss alternative, but I think the current workaround will work until the vite plugin supports the additional module resolution :)
@spaceemotion I was trying to create a reproduction with your code above but it seems that Vite, even without @tailwindcss/vite
, will not be able to import virtual CSS modules. Check this out: https://github.com/philipp-spiess/vite-virtual-css-modules
This is using only Vite 6.
Were you able to use this feature before you upgraded to Tailwind 4?
There is still the need to use the Vite resolver for css modules for issues like #15159 though, but it won't resolve your concrete issue I'm afraid.
@philipp-spiess thanks for the repo. That is indeed weird. When the virtual module ID is being used from the index.html
it all works, but not from within the CSS file.
I have not tested this with a TailwindCSS 3 setup yet, the virtual module is new for this project as we're upgrading the tech in general. I'll do a bit more digging, whether it might be something with the way vite handles CSS files + postcss internals.
I also just checked and the issue is also there with Vite v5, so it's not something with the latest v6 beta.
Okay, so here's what I found:
postcss-import
, which in turn has custom resolve behavior as well: https://github.com/vitejs/vite/blob/cc55e36dd39fef134568f53acc66514cbb7175ea/packages/vite/src/node/plugins/css.ts#L1313-L1344index.html
, it will resolve correctly, and even merge the files during build, which would be a possible workaround.
What version of Tailwind CSS are you using?
What build tool (or framework if it abstracts the build tool) are you using?
astro 5.0.0-beta.8
, usingvite 6.0.0-beta.10
What version of Node.js are you using? v20
What browser are you using? Chrome/Edge/Brave
What operating system are you using? Linux/Windows
Reproduction URL We got the following stylesheet:
The custom vite plugin is quite simple:
We need the fontawesome CSS to be included in the base layer of our CSS, since we want to be able to overwrite some things (like icon size) via the utility classes. We also don't want to include the CSS on every page, and instead have it in the global stylesheet.
Sadly, FontAwesome does not offer a simple static CSS we can use, since the classes are built at run-/build-time.
Describe your issue I ran
astro dev
with verbose enabled:From what I can tell, the resolving is being handled by tailwind, and not Astro/Vite? I tried to see if there is a way to still make this worth, but not sure why the virtual module isn't being picked up - the same approach (using virtual modules) works without issue in other parts of the pipeline where we just import things into Vue/Astro/JS.
https://stackblitz.com/edit/vite-u8kysb?file=vite.config.mjs (doesn't load inside of stackblitz due to oxide, but has a minimal example)