vanilla-extract icon indicating copy to clipboard operation
vanilla-extract copied to clipboard

Vite: is there a way to import files in `.css.ts` files?

Open dkzlv opened this issue 3 years ago • 5 comments

Currently importing an image fails with this error:

[plugin:vanilla-extract] Build failed with 1 error:
Check.css.ts:6:16: error: No loader is configured for ".png" files: pic.png

, even though it works fine in a regular .ts file.

dkzlv avatar Apr 22 '22 15:04 dkzlv

.css.ts is working fine on my end with latest vite version. Try this in your vite config

export default defineConfig({
  assetsInclude: ['**/*.png'],
  ...
})

wobsoriano avatar Apr 28 '22 06:04 wobsoriano

This is probably because Vanilla Extract runs .css.ts files in esbuild to generate the stylesheets, regardless of which plugin you use. There is currently no way to add loaders or configure esbuild here. Curious what your use case is?

AndrewLeedham avatar Apr 28 '22 23:04 AndrewLeedham

@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.

dkzlv avatar Apr 29 '22 13:04 dkzlv

@dkzlv Makes sense. I think having the ability to configure esbuild would be a good addition, it would also be a better way of doing this: https://github.com/seek-oss/vanilla-extract/pull/661

AndrewLeedham avatar Apr 29 '22 13:04 AndrewLeedham

Yeah I'm trying use static assets in my CSS, such as for a custom <li> bullet style, but we're facing the same limitation.

import { style } from "@vanilla-extract/css";

import myIcon from '../assets/icons/MyIcon.svg'

export const myListItem = style({
    listStyle: `url(${myIcon})`,
})
[plugin:vanilla-extract] Build failed with 1 error:
src/components/MyComponent.css.ts:6:25: error: No loader is configured for ".svg" files: src/assets/icons/MyIcon.svg

Is this something we can get working with some tweaking of esbuild?

willbattel avatar Aug 04 '22 02:08 willbattel

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 outDir for esbuild, which isn't exposed in the current esbuild options.

johanneslumpe avatar Dec 06 '22 20:12 johanneslumpe

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 avatar Dec 08 '22 05:12 wobsoriano

@wobsoriano Works perfectly! Awesome job

MananTank avatar Jan 15 '23 14:01 MananTank

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.

viridia avatar Jan 22 '23 21:01 viridia

I'm using Solid-Start, If I directly write relative paths in url(), it's not handled by vanilla-extract, but will be handled by vite itself! (I guess when vanilla-extract gives the generated CSS to vite)

image

Run npm run build:

image

The issue is that vanilla-extract inserts a wrong style sheet in development mode:

image

It overrides the correct property below

yume-chan avatar Jun 05 '23 15:06 yume-chan

@dkzlv This issues seems solved by the addition of esbuildOptions to the esbuild, vite and rollup plugins. The docs were missing for vite and rollup, but I've added that in #1120. Please re-open the issue, or raise a new issue with a reproduction if there is still a problem.

askoufis avatar Jun 12 '23 04:06 askoufis

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 
});

kasper573 avatar Oct 15 '23 23:10 kasper573

EDIT: My comment that was previously posted here has been turned into a discussion thread instead: https://github.com/vanilla-extract-css/vanilla-extract/discussions/1208

kasper573 avatar Oct 22 '23 17:10 kasper573