-
-
Notifications
You must be signed in to change notification settings - Fork 918
Description
Hi, I'm one of the core team means of Astro and we're in the process of converting our backend to use Vite. I'm trying to get Astro to support unocss and could use some help understanding how your Vite plugins work.
In development mode there is a request for /__uno.css
which is correctly handled by your plugin and everything works beautifully. When the build is ran that same /__uno.css
request goes through but another set of plugins are run which do not resolve this id and therefore the build breaks as no plugin claims this id.
I would expect that resolution to happen here but it does not. I'm thinking that build must work a bit differently and on the Astro side we are not sending through what unocss expects to receive.
Can you explain what type of id the plugin is expecting here, or any other guidance. Thank you!
Activity
antfu commentedon Nov 12, 2021
I am not very familiar with Astro, do you mind sharing a minimal setup so I can test it out? Thanks.
hans-d commentedon Nov 12, 2021
https://stackblitz.com/edit/github-d4rp6r?file=package.json
by default it will be in dev mode. On the terminal you can do eg
npm run build
matthewp commentedon Nov 12, 2021
@antfu, yes, the template shared by @hans-d is what i was working on. It's very possible that this a bug in Astro in and not in unocss, I was just hoping for information on what that plugin expects to receive so I can track down where things are going differently in our build vs. the vanilla Vite setup.
hans-d commentedon Nov 18, 2021
Hi @antfu, could you help out @matthewp by providing some more info re the inner details of your vite plugin ? That would help him figuring out where astro might need some nudges. Thanks in advance!
antfu commentedon Nov 19, 2021
I just pulled down your repo and played out. I can see the unresolved id error in the build.
In build, UnoCSS uses a virtual module to serve the CSS stub and replaced it in
generateBundle
hook. Can you help confirm first if the simple plugin example in Vite docs passed build on Astro? https://vitejs.dev/guide/api-plugin.html#simple-examplesmatthewp commentedon Nov 22, 2021
Yes, that does work, see here: https://stackblitz.com/edit/github-5hwrzj?file=dist%2Findex.html&on=stackblitz
What is the virtual module that UnoCSS uses? Astro is a static site generator so what the user imports in their code only goes through the pipeline in SSR. Like I said, what is going through the build pipeline is
/__uno.css
. Is that what SSR is resolvinguno.css
to?matthewp commentedon Nov 22, 2021
For clarity, how Astro generates pages statically is (in regards to CSS):
I'm now thinking the issue we are running into happens at step (2). Since the page has been loaded in SSR module we are getting the resolved ID and what we need to send through is the unresolved version.
Is the unresolved version
uno.css
?antfu commentedon Nov 22, 2021
uno.css
will be normalized to/__uno.css
, and bothuno.css
and/__uno.css
can be served. Can you share me the pointers of how 3 & 4 been implemented?matthewp commentedon Nov 22, 2021
Yeah, that happens in our CSS plugin: https://github.com/snowpackjs/astro/blob/main/packages/astro/src/vite-plugin-build-css/index.ts
So the mystery here is why
/__uno.css
is not picked up by your build plugins.antfu commentedon Nov 22, 2021
Tested a bit and made wip PR on #162
vite:css-post
- Ideally I think it's better to have the same name so plugins relying on it won't breakresolvedId
does not seem to called on Astro buildmatthewp commentedon Nov 23, 2021
An update on our end, we might be able to solve this on our end without you needing to do anything. Since the CSS is already loaded and JITed during the SSR load phase we should be able to fetch the code from that, just like what happens in dev.
matthewp commentedon Nov 23, 2021
I almost have a fix on our end. Since the CSS is loaded through SSR (the reason the plugin runs twice), it already exists and we can just request the
/__uno.css
file from Vite.It fails currently because of this though: https://github.com/antfu/unocss/blob/7481fc3f9d5c61c0aea33a8fa31caf341a749ceb/packages/vite/src/modes/global/build.ts#L119-L120
I think this throw is possibly wrong; it assumes that if a project has CSS it must be unocss usage, but Astro allows mixing CSS techniques. If the user has client-side usage of uno it will pass; if its only used in .astro files this will throw.
I think this throw should be removed.
14 remaining items
shiva-prasad-reddy commentedon Jun 15, 2022
Same issue with nuxt3 & unocss
I have no error, just works at first run, but the classes change doesn't update. The styles are reflected only by doing a page reload.
XiNiHa commentedon Jul 29, 2022
I just tried with Astro 1.0.0-rc.2. It works flawlessly in most cases. However, since all Vite plugins get applied AFTER the Astro framework integrations(which can contain JSX transforms), Attributify mode is currently broken. I wonder if we could bypass this by providing our own Astro integration.
XiNiHa commentedon Jul 29, 2022
edit: just tried building the production bundle and it's completely broken :D
I have tried several workarounds but all of them are not working.
uno.css
in.astro
file / Importinguno.css
from static framework components (framework components that are not used as an island)does not found CSS placeholder in the generated chunks
.bundle
parameter of the hookgenerateBundle()
only contained JS files and not CSS. I think that's the problem (since I was able to identify the hash placeholder in the built CSS)uno.css
in inline<script>
/ Importinguno.css
from an island (tried with SolidJS integration)empty css
comment is present in the hoisted JS file.Looks like we should definitely make an integration...
XiNiHa commentedon Aug 5, 2022
Update: things work flawlessly when I patch the
@unocss/vite
plugin'sunocss:global:build:generate
sub-plugin to run on SSR only. Although this sounds like a very Astro-specific tweak, I believe this is worth exposing as an option. (to pick between client bundle, SSR bundle, or both)@antfu What do you think about this? I can make a PR if you agree with the direction.
XiNiHa commentedon Aug 12, 2022
Update again: astro-uno does all the tricks to support Astro, like monkey-patching the Vite plugin to run on SSR mode. I ended up with using this without any patches and tricks.
antfu commentedon Aug 22, 2022
https://github.com/unocss/unocss/tree/main/packages/astro
antfu commentedon Aug 22, 2022
@XiNiHa Oh sorry I missed your comments. Thanks for making the integration! I have now implemented it directly in this repo https://github.com/unocss/unocss/tree/main/packages/astro, and enabled SSR to build for
unocss:global:build:generate
. I haven't dividing in deep about the HMR issue, but if you do, would you like to send a few PR to improve it? Thank you!XiNiHa commentedon Aug 22, 2022
I'm not the one who made the integration, @AllanChain did! 🚀 I guess they will know what should be done further :D
chore(storefront): Try to tailwind with unocss for icons and attributify
AllanChain commentedon Sep 4, 2022
Sorry for the late reply. I'm trying to solve some issue on Astro's side (withastro/astro#4621)
The fix for attributify mode (docs, code) should be included in UnoCSS. But the current fix is a bit hacky. Maybe we should support some custom preprocessing before extracting?
aspiiire commentedon Oct 7, 2022
Having this exact issue when doing build, I have solved with @AllanChain's package astro-uno, but If I am not wrong this should be fixed by Astro now... Does anyone have the same problem?
LorenzoRottigni commentedon Nov 13, 2022
I am also having this problem:
`ℹ Using Tailwind CSS from ~/assets/sass/tailwind.sass nuxt:tailwindcss 18:59:48
vite v3.2.3 building for production... 18:59:50
✓ 316 modules transformed. 19:00:00
rendering chunks (23)...
ERROR [unocss:global:build:generate] [unocss] does not found CSS placeholder in the generated chunks 19:00:00
This is likely an internal bug of unocss vite plugin
ERROR [unocss] does not found CSS placeholder in the generated chunks 19:00:00
This is likely an internal bug of unocss vite plugin
This is likely an internal bug of unocss vite plugin`
LorenzoRottigni commentedon Nov 13, 2022
I am using nuxt 3 with vite + tailwindCSS