My M.O. for using images in CSS is this:
- All images are somewhere in
/public
(typically/public/img
) in order to be easily loadable by any style, script or view with an absolute path/
- In my SCSS files I refer to the images with an absolute path starting with
/
:
body {
background: url('/img/body-bg.png');
}
I am migrating from Mix to ViteJS and all of my old code is written like this. It makes total sense and the images can be just as well be loaded by anything else, for example my blade files:
<img src="/img/logo.png" />
Now ViteJS somehow does not like it and according to other answers wants me to refer to images like this in CSS:
body {
background: url('/public/img/body-bg.png');
}
or like this in Blade files:
<img src="{{ Vite::asset('public/img/logo.png') }}" />
According to ViteJS documentation I should be able to say what /
means with either the base
or the publicDir
settings but they don't seem to have any effect in vite.config.js
file.
Is there a way to make ViteJS work the way Mix did? I don't care about image versioning and frankly don't want the bundling process to copy all my images from somewhere else into the /public/build
directory. I want to keep them where they are and just load them normally with /img/image.png
.