-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Description
Description
Many people use vite as a simple static site generator without any JS framework and would like to transform static assets. Currently plugins can only transform assets when they are requested from JS files (or probably also CSS).
In the context of sites without any frameworks, however, it is common to reference the files simply from the HTML entrypoint.
Due to this, plugins like imagetools cannot apply transformation to these assets (JonasKruckenberg/imagetools#193) like resizing.
Suggested solution
Vite should also discover assets referenced from HTML files and invoke plugins for those. It seems like vite has to parse or at least have a concept of those anyhow for URL resolution:
vite/packages/vite/src/node/plugins/html.ts
Lines 124 to 131 in 073a4bf
export const assetAttrsConfig: Record<string, string[]> = { | |
link: ['href'], | |
video: ['src', 'poster'], | |
source: ['src', 'srcset'], | |
img: ['src', 'srcset'], | |
image: ['xlink:href', 'href'], | |
use: ['xlink:href', 'href'] | |
} |
Alternative
Some plugins seem to emulate this function by parsing the HTML using a regex to discover those assets. This is rather hacky and also way less reliable. Furthermore, as vite seems to parse the HTML anyhow, it would be more performant to handle this correctly.
Additional context
I am not quite sure but while looking for similar issues I found #5098. I do not think it is about the same problem but they might share some common aspect(?)
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.