Skip to content

feat(html): add html.additionalAssetSources option#21412

Open
monam2 wants to merge 7 commits intovitejs:mainfrom
monam2:feat/html-additional-asset
Open

feat(html): add html.additionalAssetSources option#21412
monam2 wants to merge 7 commits intovitejs:mainfrom
monam2:feat/html-additional-asset

Conversation

@monam2
Copy link
Contributor

@monam2 monam2 commented Jan 14, 2026

Issue

close #21182

Description

This PR adds the html.additionalAssetSources option to allow users to configure custom HTML elements and attributes as asset sources.

Currently, Vite only processes assets in hardcoded HTML elements and attributes (<img src>, <video src>, etc.). Users cannot configure Vite to handle:

  • Custom web components (e.g., <html-import src="...">)
  • Non-standard attributes (e.g., data-src-dark, data-src-light)
    This causes asset URLs to not be transformed during build, resulting in broken paths.

Solution

Add a new html.additionalAssetSources configuration option that merges with the built-in asset sources list:

// vite.config.ts
export default defineConfig({
  html: {
    additionalAssetSources: {
      'html-import': { srcAttributes: ['src'] },
      img: { srcAttributes: ['data-src-dark', 'data-src-light'] },
    },
  },
})

For existing elements like img, additional attributes are merged with the defaults. In the example above, both the default src attribute and the custom data-src-* attributes will be processed.

Note

  • Documentation: Added html.additionalAssetSources section in docs/config/shared-options.md

@monam2
Copy link
Contributor Author

monam2 commented Jan 17, 2026

@bluwy I've updated the implementation to use the merge approach (instead of override) and also added documentation.
The CI is currently failing, but the failing tests (hmr-ssr.spec.ts on Windows) appear to be unrelated to my changes — they seem to be pre-existing flaky tests. Could you please take a look?

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

Perfect 👍

@bluwy bluwy added this to Team Board Jan 28, 2026
@github-project-automation github-project-automation bot moved this to Discussing in Team Board Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Discussing

Development

Successfully merging this pull request may close these issues.

Asset discovery for custom HTML elements and/or attributes

2 participants