Open lukethacoder opened 1 year ago
It looks like Vite assumes that when you do import './foo.css'
from JavaScript, you want it to create a <style>
tag and hoist it into the <head>
. This is not at all what LWC CSS imports do, so yeah, I'm guessing someone would need to create a custom Vite CSS plugin to handle LWC's treatment of CSS imports.
I was interested in running Vite+LWC since I've been using Vite for Lit (it's really amazingly fast) and encountered this topic. I've run into the exact same problem (and other problems when using Typescript). You can use vite-plugin-inspector to debug how transformations are performed. I didn't delve into details (yet), but the "culprit" is that vite plugin: post-css (screenshot attached):
I cannot give you more details as I'm new in this world of Vite-plugins (I've using the official plugins only), but these days i will give it a try to see how far can i go.
My guess is that we will need some kind of plugin derived from the lwc rollup plugin (or asaddition) to play nice with the Vite stack
Description
When attempting to spin up a Vite project using the
@lwc/rollup-plugin
, the CSS imports in the html template are not resolved as imports, but imported as strings (causing the@lwc/compiler
to fall over).Steps to Reproduce
https://stackblitz.com/edit/salesforce-lwc-vite-css-error?file=vite.config.js
https://github.com/salesforce/lwc/blob/8735fb96c27890fb56c7d8d5371a24f743e37cfa/packages/%40lwc/compiler/src/transformers/template.ts#L95-L106
Minor semi-related bug is Vite complaining about default CSS imports:
Expected Results
CSS files are imported correctly as functions so the template can render the actual CSS for the LWC.
Actual Results
Version
lwc: 2.41.0 Node: v16.14.2
Possible Solution
May require some sort of Vite/Rollup plugin to transform the imports? Not too sure how tightly coupled the issue is to the lwc compiler.
Additional context/Screenshots