vite icon indicating copy to clipboard operation
vite copied to clipboard

Commented-out importmap is uncommented during build

Open rgov opened this issue 5 months ago • 1 comments

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

rgov avatar Jul 03 '25 07:07 rgov

still exists in the latest vite😞

Xli33 avatar Nov 25 '25 07:11 Xli33