-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow plugins to hook into preprocessCSS()
#18030
Comments
What about writing a postcss plugin? preprocessCSS is currently only about Vite's internal CSS handling and isn't meant to be a general "run plugins to process CSS" utility. |
We thought about this, but this won't work if
Yeah, this makes sense. In the case of Svelte, defining the custom Svelte preprocessor is definitly the better approach conceptually. It just means that we will have to hope that other users of |
Lightningcss also supports the visitor API which you could, though it looks like Vite's types may not be updated for it yet, and you'd have to maintain two implementations. Is this only for vite-plugin-svelte? A Svelte preprocessor is definitely the better choice then, and if you already have a Vite plugin, you can add the preproceesor automatically like this: https://github.com/sveltejs/vite-plugin-svelte/blob/f06beb3836774b4a033a780712c7e84011698668/docs/faq.md#how-do-i-add-a-svelte-preprocessor-from-a-vite-plugin |
If I may tag along, I have a plugin I am trying to write to minify css template literals. Being able to hook into the same minification as standalone files would be a boon. Would this be a case you would consider @bluwy ? |
Do you mean |
The same issue exits with other users of the I think what is confusing here most is that we add a |
@bluwy Yes that is my intention. I have code that can parse out the template literal content and put in placeholders for the dynamic bits. I would like to use preprocessCSS, or similar, to minify that string. Willing to move to a new issue so I dont derail this issue further. |
@Lalem001 I'd suggest opening a GitHub discussion instead, I don't think
@philipp-spiess I think Astro and Svelte should handle CSS the same, and both should go through Vite's transform hook at the end. Unless you mean Svelte is stricter than it'll strip out unused CSS?
Yeah the intention here is to process them twice. On the original topic, I think I understand the usecase, but it would be hard to implement it. We need the Given this, I still think the most viable choice is with a postcss plugin or the lightningcss visitor API. Or if Svelte's custom preprocess hook works for you, then I think Astro could also consider adopting something similar. |
@bluwy Yeah, that makes sense. I'll give the postcss plugin/lightningcss visitor approach a try or if there are similar features to the svelte preprocess hook on Astro etc. Update: It looks like trying to use the visitor API will crash lightningcss for our setup. I have to try a few things to rule out it's because of our setup:
|
Hey! I believe the conclusion here is that this will be quite hard to support as the two concepts are kinda separate and merging them would cause |
Description
Vite has an API that can be used to process CSS files outside of the regular module graph: #10429 Integrations like the
vite-plugin-svelte
use these to process styles inside<style>
blocks.However, when this API is used, no hook is called inside Vite extensions before the CSS preprocessing is done and the processed styles is used by the respective users of this API.
This means that eventual CSS pre processor extensions (like Tailwind CSS) can not access the styles before the CSS pipeline (with either postcss or lightningcss) or before the styles are passed through the Svelte extension.
Suggested solution
Adding an ability to hook into
preprocessCSS()
calls, including support forenforce: 'pre'
so that we can transform CSS before it goes through postcss/lightningcss.Alternative
Alternatives right now require plugin-specific workarounds. In the case of
vite-plugin-svelte
, our Vite extension can register a hook into the Svelte CSS pre-processor which will run before the svelte Vite plugin handles things. This has to be done for all possible users of thepreprocessCSS()
API.Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: