diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 2deeebfc9a42ab..e85cef2fe32070 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -50,6 +50,12 @@ const importMapRE = /[ \t]*]*type\s*=\s*(?:"importmap"|'importmap'|importmap)[^>]*>.*?<\/script>/is const moduleScriptRE = /[ \t]*]*type\s*=\s*(?:"module"|'module'|module)[^>]*>/i +const modulePreloadLinkRE = + /[ \t]*]*rel\s*=\s*(?:"modulepreload"|'modulepreload'|modulepreload)[\s\S]*?\/>/i +const importMapAppendRE = new RegExp( + [moduleScriptRE, modulePreloadLinkRE].map((r) => r.source).join('|'), + 'i', +) export const isHTMLProxy = (id: string): boolean => htmlProxyRE.test(id) @@ -891,17 +897,17 @@ export function preImportMapHook( const importMapIndex = html.match(importMapRE)?.index if (importMapIndex === undefined) return - const moduleScriptIndex = html.match(moduleScriptRE)?.index - if (moduleScriptIndex === undefined) return + const importMapAppendIndex = html.match(importMapAppendRE)?.index + if (importMapAppendIndex === undefined) return - if (moduleScriptIndex < importMapIndex) { + if (importMapAppendIndex < importMapIndex) { const relativeHtml = normalizePath( path.relative(config.root, ctx.filename), ) config.logger.warnOnce( colors.yellow( colors.bold( - `(!) + + + + diff --git a/playground/html/vite.config.js b/playground/html/vite.config.js index 4d029b1e835d26..f7eb421975ee00 100644 --- a/playground/html/vite.config.js +++ b/playground/html/vite.config.js @@ -28,6 +28,7 @@ module.exports = { ), linkProps: resolve(__dirname, 'link-props/index.html'), valid: resolve(__dirname, 'valid.html'), + importmapOrder: resolve(__dirname, 'importmapOrder.html'), }, }, }, @@ -168,7 +169,9 @@ ${ }, { name: 'head-prepend-importmap', - transformIndexHtml() { + transformIndexHtml(_, ctx) { + if (ctx.path.includes('importmapOrder')) return + return [ { tag: 'script',