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
Cannot dynamically import css in a static build #4237
Comments
Some more investigation - if these dynamically loaded css files are CSS Modules (with the filename of If you then switch css modules to use the global behaviour in the css: {
modules: {
scopeBehaviour: 'global'
}
} You get the behaviour that I'm expecting in the ticket. I believe there is still an issue and that regular dynamic CSS imports should work like this |
I am experiencing the same (or very similar) issue when migrating a Vue-app to Vite.
This work fine in dev, but in a production build I can see that the .css-files are generated but the application attempts to load a non-exsisting .js-file. Thus the styles are not loaded. EDIT: The work-around posted above worked, but I also believe that this is still an issue. |
This now works without the module workaround for me as of vite 2.5.10 (Looks like it was fixed in 2.51: https://github.com/vitejs/vite/blob/main/packages/vite/CHANGELOG.md#251-2021-08-24 ) Import with if/else does still not work, but it works with inlining the variable in the string |
Agreed - fixed by #3333 |
Describe the bug
When importing css or scss files (doesn't matter which format) in a vite application (framework does not matter here) you can import successfully in multiple different ways:
In the sample application, these three files all add styles to a single element. If all of these load, the element is blue. If only the first import loads, it is red.
When run locally with
npm run dev
you get a blue background. When you runnpm run build
and start a server in thedist
folder you get the red backgroundThe issue appears to be that, these files are compiled as pure
.css
files, but the application is looking for.js
files for each of these hashes.I've not found a suitable rollup plugin to correct this behaviour, and this is something that worked just fine with the Vue CLI and webpack, so I think it's a bug.
Reproduction
https://github.com/steveworkman/vite-scss-dynamic-test
System Info
The text was updated successfully, but these errors were encountered: