-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Using rollup-plugin-import-assert Vite still imports CSS as a string rather than a CSSStyleSheet object #10633
Comments
This is likely caused by vite/packages/vite/src/node/plugins/importAnalysis.ts Lines 434 to 436 in d5fe92c
where we strip import assertions by default as in most cases, we can handle the assets ourselves ootb. I'm also not sure if CSS import assertions are to spec? Ideally Vite should align to import assertion behaviour by default so a separate plugin isn't needed, which is the case for JSON import assertions today. |
CSS import assertions is added in html spec. HTML Standard Chrome and Edge support since v93. Firefox and Webkit were involved in the development of import assertions as a result of working out CSS modules, and are both currently implementing the prerequisites for CSS modules (constructible stylesheets and adopedStyleSheets). Rollup v3.3.0 add support for import assertions. |
That's the spec for CSS module scripts, but I tested out CSS import assertions in Chrome and it does work, but it's not spec-approved yet. The proposal:
We likely want to do this with first-class support but we'll have to see how it plays out first. |
Just commenting to follow this thread. I'd love to be able to get rid of my workarounds to import CSSStyleSheets. Got excited when I saw #8937 was closed |
@jrj2211 What workaround do you use? |
The workaround I use is using a different file extension for css files I want to import as a constructible style sheet and rollup-plugin-cssimport:
I then can import .ccss as as constructible style sheets:
I went with .ccss for "constructible cascading style sheets". Not a fan of this solution, but it was the only thing I could figure out on how to make it work without having to use JS in CSS. Cant wait for the day I can rename all my ccss files to .css and use assert instead! |
Edit: It looks like Import Assertions will be deprecated in favor of Import Attributes. I'm not using any plugin but I can confirm that Vite 4.3 still has this issue. import sheet from './styles.css' assert { type: 'css' }; // ❌ vite error
document.adoptedStyleSheets = [sheet]; I also tried this: import sheet from './styles.css?inline' assert { type: 'css' };
document.adoptedStyleSheets = [sheet]; // ❌ error: sheet is not a CSSStyleSheet. It's a string. |
Looks like I was mistaken, and CSS module scripts has been merge into the HTML spec. But given import assertions is being phased out, I'm not sure if it's worth pursuing that again. Similar: #12140 But re the rollup-plugin-import-assert plugin, the problem is that Vite appends I've made a Stackblitz to further analyze how the plugin transforms code. |
Describe the bug
I am importing a CSS module script using the new import assertions feature:
I am using rollup-plugin-import-assert to get a
CSSStyleSheet
object from that which later can directly be pushed into a web component'sshadowRoot.adoptedStyleSheets
.Unfortunately, despite having configured this rollup plugin, Vite still imports
sheet
as astring
.To see what exactly I'm trying to do, see https://stackblitz.com/edit/vitejs-vite-evu9dn?file=FooBar.js.
Reproduction
https://stackblitz.com/edit/vitejs-vite-evu9dn?file=FooBar.js
Steps to reproduce
No response
System Info
Used Package Manager
npm
Logs
Vite log using `vite build --debug`, click to expand
Validations
The text was updated successfully, but these errors were encountered: