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

after vite build the final index.html

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
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
Describe the bug
when using
transformIndexHtmlto move the<script type=module>to body, vite always place theimportmapclosely 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 , themodulepreloadscripts fail toothe index.html template

after

vite buildthe final index.htmlReproduction
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 buildoryarn buildand thedist/index.htmlbe like the pic below.when it runs, the
modulepreloadbe resolved before theimportmap, so we get an error An import map is added after module script load was triggered, the sum.js also failsSystem 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.1882Used Package Manager
npm
Logs
No response
Validations