Skip to content

fix: skip lit-css transformation for CSS files requested with ?url - #7

Merged
redfox-mx merged 1 commit into
redfox-mx:mainfrom
julienw:fix/transform-only-query-parameter
Nov 1, 2025
Merged

redfox-mx merged 1 commit into
redfox-mx:mainfrom
julienw:fix/transform-only-query-parameter

Conversation

@julienw

@julienw julienw commented Aug 26, 2025

Copy link
Copy Markdown
Contributor

Add support for detecting CSS imports with special query parameters (like ?url) that should bypass lit-css transformation and be handled by Vite's default CSS processing instead.

Without this change, the build would fail when a CSS file is requested with ?url.

A workaround I'm using for now is adding /[?&]transform-only\b/ in my exclude configuration, but that shouldn't be needed. It's not 100% clear to me what is ?transform-only, I didn't see any information about it on vitejs documentation so I hope this is somewhat future-proof.

Tell me what you think!

Add support for detecting CSS imports with special query parameters
(like ?url) that should bypass lit-css transformation and be handled
by Vite's default CSS processing instead.

Without this change, the build would fail when a CSS file is requested
with ?url.
@redfox-mx
redfox-mx self-requested a review September 23, 2025 07:05
@redfox-mx

Copy link
Copy Markdown
Owner

hummm interesting... So, you use ?url query and vite rewrites to ?transform-only right?

FWIW: Vite sometimes rewrite some imports when it made an import analysis.... but I dont figure out why this is happening with the url query.

@@ -0,0 +1,3 @@
import cssUrl from './styles.css?url'

@redfox-mx redfox-mx Sep 23, 2025 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some doubts about if only .css files should be preserved, or we need to support .scss too. Could you please test how this import works in "normal" vite with .scss?

maybe context in: vitejs/vite#15259

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some doubts about if only .css files should be preserved, or we need to support .scss too. Could you please test how this import works in "normal" vite with .scss?

In a normal vite without vite-lit-css, with a .scss file (with sass also installed, and after checking that vite runs it by adding some sass variables) I get the URL as expected.
I think vite doesn't make a difference between these... should we?

Now I have a doubt about the ?raw query... if it should also be special-cased in this code.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! You are right. would you matter add the code for the raw query?

@julienw

julienw commented Sep 23, 2025

Copy link
Copy Markdown
Contributor Author

hummm interesting... So, you use ?url query and vite rewrites to ?transform-only right?

yeah, I've found references to that in
https://github.com/vitejs/vite/blob/0021527ccffcbf9baf447e6d111a8a7210750ebe/packages/vite/src/node/plugins/css.ts#L349-L359
and
https://github.com/vitejs/vite/blob/0021527ccffcbf9baf447e6d111a8a7210750ebe/packages/vite/src/node/plugins/css.ts#L655-L656

I think they replace ?url only in build mode, so that it's not handled by the assets plugin later (as they mention in the comment, the assets plugin handles this for dev):
https://github.com/vitejs/vite/blob/0021527ccffcbf9baf447e6d111a8a7210750ebe/packages/vite/src/node/plugins/asset.ts#L191-L192

Although it's not 100% clear to me what happens in this line 655 above when in build.

@@ -0,0 +1,3 @@
import cssUrl from './styles.css?url'

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! You are right. would you matter add the code for the raw query?

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.

2 participants