vite: url with relative path in sass/scss is broken when main.js and assets are in subdirectory
Describe the bug
Following on from my comment in #7651 - the linked PR (#10741) doesn’t solve the issue when main.js and assets are stored in a top level assets directory, e.g.:
├── assets
│ ├── images
│ │ └── vite.svg
│ ├── main.js
│ └── styles
│ ├── pages
│ │ └── home.scss
│ └── style.scss
├── index.html
├── package.json
├── package-lock.json
├── public
│ └── vite.svg
└── README.md
You can see in the linked reproducer that url('../../images/vite.svg') does not resolve the image correctly, even though that is the correct relative path to the file from assets/styles/pages/home.scss. When the path is changed to url('../images/vite.svg') it resolves correctly.
Reproduction
https://github.com/andyexeter/vitejs-vite-2btrkm
Steps to reproduce
Run npm install followed by npm run dev
System Info
System:
OS: Linux 5.15 Ubuntu 20.04.5 LTS (Focal Fossa)
CPU: (12) x64 11th Gen Intel(R) Core(TM) i5-11600 @ 2.80GHz
Memory: 6.97 GB / 15.47 GB
Container: Yes
Shell: 5.8 - /usr/bin/zsh
Binaries:
Node: 16.18.1 - /usr/bin/node
Yarn: 1.22.17 - /usr/bin/yarn
npm: 8.19.2 - /usr/bin/npm
Browsers:
Chrome: 107.0.5304.110
Firefox: 107.0
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 44
- Comments: 21 (1 by maintainers)
Also encountering this problem. It does not seem to require that “main.js and assets are in subdirectory”, as per title.
Given this directory structure
Simply using
url('./path/to/font.ttf')fromstyles/base/_fonts.scsswhich is then@imported fromstyles/main.scssgivesSo I find it quite strange that the documentation still states:
I think I ran into the same issue:
created a new vite-vanilla-typescript project, added
npm i bootstrap-iconsand have these files:In main.ts I import the bootstrap-icons via
import "./style.scss";In style.scss I import the bootstrap-icons.scss via
@use "../node_modules/bootstrap/scss/bootstrap";And it fails with:But when using
@use "../node_modules/bootstrap/scss/bootstrap.css";it works as expected.(It also fails with the same error when importing *.scss)
Came here after a long time of finding out what might be wrong while learning web dev with vite/scss from here: https://github.com/twbs/icons/issues/1381
(vite 4.1.0)
@frederikbosch you add it to the alias section of the vite.config.js see https://vitejs.dev/config/shared-options.html#resolve-alias
facing the same issue here. relative paths in scss files aren’t handled correctly.
Thanks for creating this. The reason why this is happening is explained in #7651:
But I forgot about this while creating #10741.