Skip to content

fix(css): .css?url support - #15259

Merged
patak-cat merged 6 commits into
vitejs:mainfrom
sapphi-red:feat/css-url
Jan 12, 2024
Merged

patak-cat merged 6 commits into
vitejs:mainfrom
sapphi-red:feat/css-url

Conversation

@sapphi-red

@sapphi-red sapphi-red commented Dec 6, 2023 •

Copy link
Copy Markdown
Member

Description

  • .css?url now returns a URL to transformed CSS
    • before, it was returning a URL to transformed CSS in dev, and to raw CSS after build
  • .css?raw&url now returns a URL to raw CSS
    • note that .css?raw returns a JS module (export default ".foo {}") but .css?raw&url returns a URL to CSS file (.foo {})
  • .module.css?url now throws an error
    • this was never useful because there's no way to get the class name map ({ 'foo': 'foo_hash' }) without injecting the actual CSS

fixes #2522
fixes #7842
fixes #13416

Additional context


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines, especially the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Update the corresponding documentation if needed.
  • Ideally, include relevant tests that fail without this PR but pass with it.

@sapphi-red sapphi-red added feat: css p3-minor-bug An edge case that only affects very specific usage (priority) labels Dec 6, 2023
@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@bluwy

bluwy commented Dec 9, 2023

Copy link
Copy Markdown
Member
  • .css?raw&url now returns a URL to raw CSS

Is there a reason this is supported? I can't quite think of a case where a user would use this, using plain .css?raw and maybe manualChunks should be good enough. Was it for compat for .css?url in prod before?

@sapphi-red

Copy link
Copy Markdown
Member Author
  • .css?raw&url now returns a URL to raw CSS

Is there a reason this is supported? I can't quite think of a case where a user would use this, using plain .css?raw and maybe manualChunks should be good enough. Was it for compat for .css?url in prod before?

I added it for compat for .css?url in prod before. I'm fine removing it.

@sapphi-red sapphi-red changed the title fix(css): .css?url/.css?raw&url support fix(css): .css?url support Jan 10, 2024
@sapphi-red

Copy link
Copy Markdown
Member Author

In the last meeting, we discussed that we should add ?raw&url after we find a use case.


I removed the ?raw&url feature and rebased on main branch.

@patak-cat

Copy link
Copy Markdown
Member

/ecosystem-ci run

@vite-ecosystem-ci

Copy link
Copy Markdown

📝 Ran ecosystem CI on f5106ac: Open

suite result latest scheduled
analogjs ✅ success ✅ success
astro ✅ success ✅ success
histoire ❌ failure ❌ failure
ladle ✅ success ✅ success
laravel ❌ failure ❌ failure
marko ✅ success ✅ success
nuxt ✅ success ❌ failure
nx ✅ success ✅ success
previewjs ✅ success ✅ success
qwik ✅ success ✅ success
rakkas ✅ success ✅ success
sveltekit ✅ success ✅ success
unocss ❌ failure ❌ failure
vike ❌ failure ❌ failure
vite-plugin-pwa ✅ success ✅ success
vite-plugin-react ✅ success ✅ success
vite-plugin-react-pages ✅ success ✅ success
vite-plugin-react-swc ✅ success ✅ success
vite-plugin-svelte ❌ failure ❌ failure
vite-plugin-vue ✅ success ✅ success
vite-setup-catalogue ✅ success ✅ success
vitepress ❌ failure ❌ failure
vitest ✅ success ✅ success

patak-cat
patak-cat previously approved these changes Jan 10, 2024

@patak-cat patak-cat left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nuxt fail seems unrelated. Awesome work @sapphi-red 💚

@patak-cat
patak-cat enabled auto-merge (squash) January 12, 2024 09:05
@patak-cat
patak-cat merged commit ed56d96 into vitejs:main Jan 12, 2024
@sapphi-red
sapphi-red deleted the feat/css-url branch January 12, 2024 10:42
@josephmarkus

Copy link
Copy Markdown

There appears to be a similar issue with SCSS. Say I have:

import selectStyles from "somePackage/Select.scss?inline";

export const Theme = () => <style>{selectStyles}</style>;

And that Select.scss has

:root,
:host {
  --select-icon: url("./chevron.svg");
}

.selectStyles {
  background-image: var(--select-icon);
}

Vite thinks that ./chevron.svg is relative to the app and not node_modules and so tries to resolve it by looking up /assets/chevron.svg

@patak-cat

Copy link
Copy Markdown
Member

Hey @josephmarkus! Your comment in this merged PR will be quickly lost in the repo activity. Would you create a new issue with a minimal reproduction against the latest Vite so we can properly track the problem you are reporting?

@josephmarkus

Copy link
Copy Markdown

Was just filling in a bug report and whilst reproducing the error realised that my SCSS file imports another SCSS file, but if I target the end file directly, Vite resolves everything fine, so maybe not an issue 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat: css p3-minor-bug An edge case that only affects very specific usage (priority)

Projects

Archived in project

4 participants