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

Force direct linking with explicit ?url import #12385

Closed
7 tasks done
Boldewyn opened this issue Mar 12, 2023 · 3 comments
Closed
7 tasks done

Force direct linking with explicit ?url import #12385

Boldewyn opened this issue Mar 12, 2023 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@Boldewyn
Copy link

Describe the bug

I encountered a problem with a discrepancy between the dev and build states of my app with regards to linking to stylesheets from within JS. Use case: Using style sheet URLs inside web components to link to shared styles.

The problem is the conversion to a data: URI for a small stylesheet in this case:

// main.js
import SmallCSS from './small.css?url';

document.head.insertAdjacentHTML('beforeend', `<link rel="stylesheet" href="${SmallCSS}">`);
/* small.css */
@import "./very_large.css";

With small.css being under the threshold of assetsInlineLimit it will be included as a data: URI. However, without any additional tooling, very_large.css will not be present in the final build. This leads to a reference error.

In my concrete case I handle the CSS import with postcss-import and mention small.css explicitly in vite.config.js. However, this file will not get picked up because of the data: URI conversion that is not aware of the final small.css being actually quite large.

My suggested solution is: Ignore assetsInlineLimit if the ?url specifier is set for a given import, or introduce a new specifier that allows for this behaviour.

Reproduction

https://stackblitz.com/edit/vitejs-vite-qdaqvk?file=small.css,referenced.css,main.js&terminal=dev

Steps to reproduce

No response

System Info

Vite 4.1.0.

Used Package Manager

npm

Logs

No response

Validations

@stackblitz
Copy link

stackblitz bot commented Mar 12, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@Boldewyn
Copy link
Author

Small addendum: When assetsInlineLimit is set to 0, the current behaviour is to handle small.css twice, once running it through the postcss pipeline (due to being referenced in vite.config.js) and once unchanged with the @import still intact, ending up with two files like small-571f6575.css and small-ce6b3471.css.

The unchanged one is linked to via the import code in JS.

That means, the actual bug is, that the CSS file referenced via import ?url in a JS file is not run through postcss at all and handled twice in that case. Vite should detect that this is the same file as referenced on other places and either way give it the same postcss treatment.

Boldewyn added a commit to Codepoints/Codepoints.net that referenced this issue Mar 13, 2023
See vitejs/vite#12385 for the concrete problem.
We bypass it by loading the main CSS file in any component that needs
global styles.
@sapphi-red
Copy link
Member

Duplicate of #2522, #7842

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2023
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Mar 14, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Mar 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants