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

Support "?url" for js/css files #9028

Closed
4 tasks done
xuld opened this issue Jul 11, 2022 · 3 comments
Closed
4 tasks done

Support "?url" for js/css files #9028

xuld opened this issue Jul 11, 2022 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@xuld
Copy link

xuld commented Jul 11, 2022

Description

Currently , vite will replace ?url to base64 url in production mode. If the resource is js or css file, it's better to return the url itself.

Use case:

import css from "./style.scss?url"

document.querySelector('link').href = css

Suggested solution

?url is not only for binary resources.

Alternative

No response

Additional context

No response

Validations

@sapphi-red
Copy link
Member

it's better to return the url itself

It works even if it is a dataurl. https://stackblitz.com/edit/vitejs-vite-7qms34?terminal=dev
What is the reason here? Is it for file size? or you want to prevent inlining for other reason?

@xuld
Copy link
Author

xuld commented Jul 13, 2022

Firstly, when importing css files as url, the returning url should not be the raw source code of that file, but the compiled code.
Secondly, when importing css files, currently the output data uri is data:undefined;base64,... .

For example:

// a.scss
a { &:hover { color: red; } }

// a.ts
import a from "./a.css?url"
console.log(a); 

// a should be `a:hover { color: red; }`
// not `a { &:hover { color: red; } }`

I want to import a css file in an iframe:

import css from "./a.css?url";

iframe.contentDocument.querySelector('link').href = css

There is no solution currently because normally import css will inject a style tag in current document, which is not configuable.

@sapphi-red
Copy link
Member

The first one it is a duplicate of #2522.
The second one is caused by the first one.

About .js?url, it is a duplicate of #6757.

For a workaround, this might work.

import styleText from './a.css?inline'

iframe.contentDocument.querySelector('#style').textContent = styleText

@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Jul 13, 2022
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed enhancement: pending triage labels Jul 13, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jul 31, 2022
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