diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 780eb19175f97f..f4fe5bfee4089a 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -814,6 +814,51 @@ export type IndexHtmlTransform = transform: IndexHtmlTransformHook } +export function preImportMapHook( + config: ResolvedConfig +): IndexHtmlTransformHook { + return (html, ctx) => { + const importMapIndex = html.match(importMapRE)?.index + if (importMapIndex === undefined) return + + const moduleScriptIndex = html.match(moduleScriptRE)?.index + if (moduleScriptIndex === undefined) return + + if (moduleScriptIndex < importMapIndex) { + const relativeHtml = normalizePath( + path.relative(config.root, ctx.filename) + ) + config.logger.warnOnce( + colors.yellow( + colors.bold( + `(!)