Skip to content

Support in Astro #105

@matthewp

Description

@matthewp

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

antfu commented on Nov 12, 2021

@antfu
Member

I am not very familiar with Astro, do you mind sharing a minimal setup so I can test it out? Thanks.

hans-d

hans-d commented on Nov 12, 2021

@hans-d

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

matthewp commented on Nov 12, 2021

@matthewp
Author

@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

hans-d commented on Nov 18, 2021

@hans-d

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

antfu commented on Nov 19, 2021

@antfu
Member

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-examples

matthewp

matthewp commented on Nov 22, 2021

@matthewp
Author

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 resolving uno.css to?

matthewp

matthewp commented on Nov 22, 2021

@matthewp
Author

For clarity, how Astro generates pages statically is (in regards to CSS):

  1. Render a page.
  2. Examine Vite's module graph for what CSS is imported.
  3. Create our own virtual module when imports all of those CSS.
  4. Create an asset chunk at the end that concats all of the CSS together.

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

antfu commented on Nov 22, 2021

@antfu
Member

uno.css will be normalized to /__uno.css, and both uno.css and /__uno.css can be served. Can you share me the pointers of how 3 & 4 been implemented?

matthewp

matthewp commented on Nov 22, 2021

@matthewp
Author

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

antfu commented on Nov 22, 2021

@antfu
Member

Tested a bit and made wip PR on #162

  • Astro replaced vite:css-post- Ideally I think it's better to have the same name so plugins relying on it won't break
    plugin resolvedId does not seem to called on Astro build
matthewp

matthewp commented on Nov 23, 2021

@matthewp
Author

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

matthewp commented on Nov 23, 2021

@matthewp
Author

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

shiva-prasad-reddy commented on Jun 15, 2022

@shiva-prasad-reddy

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

XiNiHa commented on Jul 29, 2022

@XiNiHa
Contributor

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

XiNiHa commented on Jul 29, 2022

@XiNiHa
Contributor

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.

  • Directly importing uno.css in .astro file / Importing uno.css from static framework components (framework components that are not used as an island)
    • Breaks with an error does not found CSS placeholder in the generated chunks.
      • The bundle parameter of the hook generateBundle() 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)
  • Importing uno.css in inline <script> / Importing uno.css from an island (tried with SolidJS integration)
    • Generates a correct CSS file. However it's not imported. only empty css comment is present in the hoisted JS file.

Looks like we should definitely make an integration...

XiNiHa

XiNiHa commented on Aug 5, 2022

@XiNiHa
Contributor

Update: things work flawlessly when I patch the @unocss/vite plugin's unocss: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

XiNiHa commented on Aug 12, 2022

@XiNiHa
Contributor

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

antfu commented on Aug 22, 2022

@antfu
Member
antfu

antfu commented on Aug 22, 2022

@antfu
Member

@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

XiNiHa commented on Aug 22, 2022

@XiNiHa
Contributor

I'm not the one who made the integration, @AllanChain did! 🚀 I guess they will know what should be done further :D

AllanChain

AllanChain commented on Sep 4, 2022

@AllanChain

I guess they will know what should be done further :D

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

aspiiire commented on Oct 7, 2022

@aspiiire

The same issue occurs when using UnoCSS with îles. When building, especially with hydration, îles will throw an error as well:

[unocss:global:build:generate] [unocss] does not found CSS placeholder in the generated chunks,
this is likely an internal bug of unocss vite plugin

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

LorenzoRottigni commented on Nov 13, 2022

@LorenzoRottigni

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

LorenzoRottigni commented on Nov 13, 2022

@LorenzoRottigni

I am using nuxt 3 with vite + tailwindCSS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @hans-d@matthewp@djmtype@electroheadfx@NexZhu

      Issue actions

        Support in Astro · Issue #105 · unocss/unocss