Skip to content

Commit

Permalink
chore: recover conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 23, 2022
1 parent 92fcfb5 commit 103826d
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/vite/src/node/plugins/html.ts
Expand Up @@ -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(
`(!) <script type="importmap"> should come before <script type="module"> in /${relativeHtml}`
)
)
)
}
}
}

/**
* Move importmap before the first module script
*/
export function postImportMapHook(): IndexHtmlTransformHook {
return (html) => {
if (!moduleScriptRE.test(html)) return

let importMap: string | undefined
html = html.replace(importMapRE, (match) => {
importMap = match
return ''
})
if (importMap) {
html = html.replace(moduleScriptRE, (match) => `${importMap}\n${match}`)
}

return html
}
}

export function resolveHtmlTransforms(
plugins: readonly Plugin[]
): [
Expand Down

0 comments on commit 103826d

Please sign in to comment.