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

Documenting __VITE_ASSET__ or exposing a function to build asset url. #13459

Open
1 of 4 tasks
cijiugechu opened this issue Jun 8, 2023 · 3 comments
Open
1 of 4 tasks
Labels
enhancement New feature or request

Comments

@cijiugechu
Copy link

Documentation is

  • Missing
  • Outdated
  • Confusing
  • Not sure?

Explain in Detail

Currently in plugin development, if you want to emit a file and then reference it, you need to use the __VITE_ASSET__${fileHandle}__ pattern to get the url , there are many examples like this, such as imagetools.

The problem is : this pattern is undocumented, and subsequent plugin developers need to view the source code to continue development.

Your Suggestion for Changes

  1. Document this section in the same way as Rollup's File URLs.
  2. Expose a new function like buildAssetUrl : The function receives fileHandle as an argument and returns the corresponding asset url.

Reproduction

No response

Steps to reproduce

No response

@cijiugechu cijiugechu added the documentation Improvements or additions to documentation label Jun 8, 2023
@cijiugechu cijiugechu changed the title Document __VITE_ASSET__ or expose a function to build asset url. Documenting __VITE_ASSET__ or exposing a function to build asset url. Jun 8, 2023
@HexaCubist
Copy link

Can confirm this stumped me for a while until I found others using __VITE_ASSET__, then spent a long while convincing myself this was the best option. It'd be great to see official documentation of this as an alternative to import.meta.ROLLUP_FILE_URL_referenceId in JS files and Rollup's this.getFileName, which only works after generate is called in the lifecycle.

It seems clear to me some effort has been put into this internal system for referencing assets, and the need for it is discussed here: rollup/rollup#4713

@patak-dev
Copy link
Member

I think we should have this feature, but I'm not sure the current API should be exposed in its current form. Let's change the issue to be a feature request. Related #7162

@patak-dev patak-dev added enhancement New feature or request and removed documentation Improvements or additions to documentation labels Jan 9, 2024
@leoj3n
Copy link
Contributor

leoj3n commented Mar 23, 2024

I have a preprocessing plugin that leverages imagetools to transform the local image pointed to by src from an <img> tag, and what I get back from imagetools is a __VITE_ASSET__ like __VITE_ASSET__Ce96qHrT__... I wish to resolve the asset ID into the actual relative asset URL so it can be encoded with encodeURIComponent and put on the query string to Vercel image optimization CDN like /_vercel/image?url=%2F_app%2Fimmutable%2Fassets%2F14.Ce96qHrT.jpg&w=80&q=99

Where 14.Ce96qHrT.jpg is the image that was transformed by imagetools (primarily it is a cropping transform as Vercel CDN only supports resizing).

Can I do this today by extracting out from Vite some currently unexposed function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants