Skip to content

Commented-out importmap is uncommented during build #20340

@rgov

Description

@rgov

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:

const importMapRE =
/[ \t]*<script[^>]*type\s*=\s*(?:"importmap"|'importmap'|importmap)[^>]*>.*?<\/script>/is

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions