Commented-out importmap is uncommented during build
Describe the bug
Vite promotes import maps from the body of an HTML document to the head. However, it does so by using a regular expression to find import maps:
https://github.com/vitejs/vite/blob/d62dc3321db05d91e74facff51799496ce8601f3/packages/vite/src/node/plugins/html.ts#L63-L64
This causes unexpected issues, like when the import map is commented out; Vite still extracts it and puts it in the head, effectively uncommenting the user's code.
Check out these tips for parsing HTML with regex for a suggestion on making this more robust.
Steps to reproduce
Just add this to index.html and observe the resulting <head> tag or look for a console error about the import map being invalid JSON.
<!-- <script type="importmap">
this should be ignored
</script> -->
System Info
Binaries:
Node: 24.3.0
Yarn: 1.22.22
npm: 11.4.2
Browsers:
Chrome: 138.0.7204.92
Safari: 18.5
npmPackages:
vite: ^7.0.0 => 7.0.0
Used Package Manager
npm
Logs
No response
Validations
- [x] Follow our Code of Conduct
- [x] Read the Contributing Guidelines.
- [x] Read the docs.
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [x] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to vuejs/core instead.
- [x] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [x] The provided reproduction is a minimal reproducible example of the bug.
still exists in the latest vite😞