Skip to content

<script type=importmap> is always wrongly placed closely before <script type=module> when building #20979

Description

@Xli33

Describe the bug

when using transformIndexHtml to move the <script type=module> to body, vite always place the importmap closely before the "module" on build, but all the generated <link rel=modulepreload> still be in <head> and be resolved before importmap, which result in an error An import map is added after module script load was triggered , the modulepreload scripts fail too

the index.html template
Image

after vite build the final index.html
Image

Reproduction

https://stackblitz.com/edit/vitejs-vite-kp7bg1e8?file=index.html,vite.config.ts,src%2Fsum.ts,dist%2Findex.html&terminal=dev

Steps to reproduce

run npm run build or yarn build and the dist/index.html be like the pic below.
when it runs, the modulepreload be resolved before the importmap , so we get an error An import map is added after module script load was triggered, the sum.js also fails

Image

System Info

System:
    OS: Windows 11 10.0.26100
    CPU: (18) x64 Intel(R) Core(TM) Ultra 5 125H
    Memory: 19.33 GB / 31.61 GB
 Binaries:
    Node: 24.5.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.22 - C:\Program Files\nodejs\yarn.CMD
    npm: 11.5.1 - C:\Program Files\nodejs\npm.CMD
 Browsers:
    Internet Explorer: 11.0.26100.1882

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: htmlp3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions