CSS Import does not result in injected style when referencing the built versions in the dist directory #13421
Replies: 3 comments 4 replies
This comment has been hidden.
This comment has been hidden.
-
I don't think Vite is doing something wrong or odd here. Maybe you are looking for the backend integration? |
Beta Was this translation helpful? Give feedback.
-
I've run into a similar problem in the past and so far have found two vite plugins that could help solve this. The more popular one of those would be The other plugin you could try out is Hope this helps in case the backend intergations recommended in the comment above is not what you're looking for 😄 |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
When you reference a javascript file that's generated in the dist folder, Vite ignores any CSS imports in the graph. The CSS file does get processed and generated, but Vite does not inject a
<link>
reference tag in the DOM. Neither astyle
tag.Why do I need this?
I'm creating a Vite implementation to provide source file bundling in a .NET Core (Razor Pages or MVC) project. In Razor Pages / MVC it's common practice to place your processed files in the wwwroot directory (dist), and leave your source files somewhere else. Only files inside the wwwroot directory can be accessed by the browser.
So in our projects, the pages always reference javascript files or stylesheets that are processed to the wwwroot directory. We don't reference our original files.
I had this working with snowpack. But since snowpack is deprecated, I tasked myself with moving over to Vite. Everything is working besides this odd behavior.
Reproduction
https://stackblitz.com/edit/vitejs-css-import-issue?file=index.html
Steps to reproduce
npm create vite@latest vite-bug-demo -- --template vanilla-ts
.npm i
.npm run build
.index.html
file, replace<script type="module" src="/src/main.ts"></script>
with<script type="module" src="/dist/assets/index-[copy-the-hash].js"></script>
.npm run dev
.So basically swap out the source file for the dist file.
As you can see, the scripting still works, but the styles are missing. Vite did not inject the tag. This is the same behaviour I have in my .NET 7 Razor Pages project.
System Info
Win10 + Chrome (latest). Bug also occurs on the repro case on StackBlitz.
Used Package Manager
npm
Logs
No response
Validations
Beta Was this translation helpful? Give feedback.
All reactions