Skip to content
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

css url() assets in html not handled in build process #3980

Closed
6 tasks done
zoubingwu opened this issue Jun 27, 2021 · 4 comments
Closed
6 tasks done

css url() assets in html not handled in build process #3980

zoubingwu opened this issue Jun 27, 2021 · 4 comments
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority) pending triage

Comments

@zoubingwu
Copy link

zoubingwu commented Jun 27, 2021

Describe the bug

writing css style in html entry file with followings:

<style>
	#app {
        background-image: url("./bg-texture.jpeg");
    }
</style>

Only works in dev mode.
The bg-texture.jpg won't be hashed and copied to the build output dir. Run yarn serve then you can see a 404 error complaining about can't find bg-texture.jpeg.

image

Reproduction

https://github.com/zoubingwu/vite-css-url-bug-repro

System Info

System:
    OS: macOS 11.4
    CPU: (8) arm64 Apple M1
    Memory: 109.94 MB / 8.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.12.0 - /opt/homebrew/bin/node
    Yarn: 1.22.10 - /opt/homebrew/bin/yarn
    npm: 7.6.3 - /opt/homebrew/bin/npm
  Browsers:
    Chrome: 91.0.4472.114
    Safari: 14.1.1
  npmPackages:
    vite: ^2.3.8 => 2.3.8

Used Package Manager

yarn

Logs

No response

Validations

@ygj6
Copy link
Member

ygj6 commented Jun 29, 2021

You can place bg-texture.jpeg in <root>/public dir. It will be served at root path / during dev, and copied to the root of the dist directory as-is. Here is a detailed explanation https://vitejs.dev/guide/assets.html#the-public-directory .

<style>
	#app {
        background-image: url("/bg-texture.jpeg");
    }
</style>

@zoubingwu
Copy link
Author

Yeah writing this in a separated css file and import it in js could workaround it, thought it's still a bug should be fixed so I opened a new issue here.

@Shinigami92 Shinigami92 added the p2-edge-case Bug, but has workaround or limited in scope (priority) label Jul 3, 2021
@BLumia
Copy link

BLumia commented Aug 5, 2021

+1 for this issue. The current workaround requires manually import the file from .js file or writing this in a separated CSS file. It can be redundance for a vanilla site or for a site using TailwindCSS that suggested to use style="background-image: url(...)" inline CSS for background image.

It could be useful if CSS url() inside HTML can be handled by default or configurable by editing vite.config.js or somewhere else.

@bluwy
Copy link
Member

bluwy commented Mar 12, 2022

Looks like this is fixed now in Vite 2.8.6, and I believe it was fixed by #6321

@bluwy bluwy closed this as completed Mar 12, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p2-edge-case Bug, but has workaround or limited in scope (priority) pending triage
Projects
None yet
Development

No branches or pull requests

5 participants