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
Vite: is there a way to import files in .css.ts
files?
#665
Comments
export default defineConfig({
assetsInclude: ['**/*.png'],
...
}) |
This is probably because Vanilla Extract runs |
@wobsoriano Unfortunately, this doesn't help, here's a repro. @AndrewLeedham Pretty basic, I would say, mostly linking to static resources: fonts, images for backgrounds/pseudo-selectors. |
Yeah I'm trying use static assets in my CSS, such as for a custom
Is this something we can get working with some tweaking of esbuild? |
The font is handled by a plain CSS files because Vanilla Extract cannot do it at the moment See vanilla-extract-css/vanilla-extract#665
Is there something the community can help with here? It's not possible to use a file loader right now because it requires defining the |
Made a simple plugin that fixes this https://github.com/wobsoriano/esbuild-vanilla-image-loader import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin'
import { ImageLoader } from 'esbuild-vanilla-image-loader'
export default defineConfig({
plugins: [
vanillaExtractPlugin({
esbuildOptions: {
plugins: [ImageLoader()],
}
}),
],
}) // App.css.ts
import { style } from '@vanilla-extract/css'
import Icon from '../assets/icons/Icon.svg'
export const myListItem = style({
listStyle: `url(${myIcon})`,
}) The plugin transforms the imported image to a JS file that default exports the file path so that Vite can read it. |
@wobsoriano Works perfectly! Awesome job |
I ran into this same issue as well (using Vite) - I have a bunch of custom cursors (pngs). What I ended up doing is putting all the cursors in their own stylesheet that uses CSS modules rather than vanilla extract. It's an ugly hack but at least I'm unblocked. I tried the plugin and couldn't get it to work - the url that it produced had the wrong path. |
I'm using Solid-Start, If I directly write relative paths in Run The issue is that vanilla-extract inserts a wrong style sheet in development mode: It overrides the correct property below |
@dkzlv This issues seems solved by the addition of |
Why would we not want this to be dealt with automatically in all vanilla extract integrations? Referencing assets from css files is a very normal workflow. Specially since type safety is one of the primary goals of vanilla extract, I expect to be able to do something like this: import { style } from "@vanilla-extract/css";
import imageUrl from "./image.png";
export const safeImage = style({
background: `url(${imageUrl})`
}); Rather than having to work around like this: export const unsafeImage = style({
background: `url(<path-from-project-root>/image.png)` // Not only is it unsafe, I also need an absolute reference
}); |
EDIT: My comment that was previously posted here has been turned into a discussion thread instead: #1208 |
Currently importing an image fails with this error:
, even though it works fine in a regular
.ts
file.The text was updated successfully, but these errors were encountered: