Skip to content
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

Generate CSS after Vite handles imports during prod builds #14850

Closed
wants to merge 6 commits into from

Conversation

thecrypticace
Copy link
Contributor

@thecrypticace thecrypticace commented Oct 31, 2024

When we started handling @import ourselves in #14446 we inadvertently broke Vite's URL asset url rewriting inside of some imported CSS files.

For example, given the following files:

/* ./main.css */
@import "./dir/imported.css";

/* ./dir/imported.css */
.foo {
  background: url(../image.png);
}

/* ./image.png */
/* … */

We'd flatten the import CSS and you'd end up with the following:

/* main.css */
.foo {
  background: url(../image.png);
}

/* ./image.png */
/* … */

And, as you can see, ../image.png does not exist relative to the "final" CSS. It should've been rewritten to ./image.png but was not. Letting Vite handle the imports will rewrite these urls properly — which is what this PR does. It achieves this by running our CSS generation when typtical Vite plugins do rather than before (via enforce: 'pre').

Additionally, because we were running the generation step early, Vue had not handled :deep(…) selectors in <style scoped> blocks yet and we were caching the unprocessed CSS. This is also fixed by this PR.

Ideally both of these things would be fixable by changing how we handle imports but that will require additional APIs so this is a reasonable stopgap measure until we can do that.

Fixes #14839

@thecrypticace thecrypticace requested a review from a team as a code owner October 31, 2024 19:53
@thecrypticace thecrypticace marked this pull request as draft October 31, 2024 19:54
@thecrypticace thecrypticace marked this pull request as ready for review November 1, 2024 14:43
@philipp-spiess
Copy link
Member

We decided to keep the pre processing phase for now and fixed the issues with the following PRs:

@philipp-spiess philipp-spiess deleted the fix/v4-vite-url-rewriting branch November 7, 2024 11:42
adamwathan pushed a commit that referenced this pull request Nov 7, 2024
Fixes #14784

This is an alternative to #14850 in which we actually perform url
rewriting / rebasing ourselves. We ported a large portion of the
URL-rewriting code from Vite (with attribution) to use here with some
minor modifications. We've added test cases for the url rewriting so
verifying individual cases is easy. We also wrote integration tests for
Vite that use PostCSS and Lightning CSS that verify that files are found
and inlined or relocated/renamed as necessary.

We also did some manual testing in the Playground to verify that this
works as expected across several CSS files and directories which you can
see a screenshot from here:

<img width="1344" alt="Screenshot 2024-11-05 at 10 25 16"
src="https://github.com/user-attachments/assets/ff0b3ac8-cdc9-4e26-af79-36396a5b77b9">

---------

Co-authored-by: Philipp Spiess <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@tailwindcss/vite@alpha used in vite vue, :deep() selector can not use in build mode, dev mode is ok
2 participants