Skip to content

Commit

Permalink
refactor: append tags logic in applyHtmlTransforms (#15647)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored Jan 22, 2024
1 parent 94cac0a commit 09b1517
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1203,27 +1203,31 @@ export async function applyHtmlTransforms(
tags = res.tags
}

const headTags: HtmlTagDescriptor[] = []
const headPrependTags: HtmlTagDescriptor[] = []
const bodyTags: HtmlTagDescriptor[] = []
const bodyPrependTags: HtmlTagDescriptor[] = []
let headTags: HtmlTagDescriptor[] | undefined
let headPrependTags: HtmlTagDescriptor[] | undefined
let bodyTags: HtmlTagDescriptor[] | undefined
let bodyPrependTags: HtmlTagDescriptor[] | undefined

for (const tag of tags) {
if (tag.injectTo === 'body') {
bodyTags.push(tag)
} else if (tag.injectTo === 'body-prepend') {
bodyPrependTags.push(tag)
} else if (tag.injectTo === 'head') {
headTags.push(tag)
} else {
headPrependTags.push(tag)
switch (tag.injectTo) {
case 'body':
;(bodyTags ??= []).push(tag)
break
case 'body-prepend':
;(bodyPrependTags ??= []).push(tag)
break
case 'head':
;(headTags ??= []).push(tag)
break
default:
;(headPrependTags ??= []).push(tag)
}
}

html = injectToHead(html, headPrependTags, true)
html = injectToHead(html, headTags)
html = injectToBody(html, bodyPrependTags, true)
html = injectToBody(html, bodyTags)
if (headPrependTags) html = injectToHead(html, headPrependTags, true)
if (headTags) html = injectToHead(html, headTags)
if (bodyPrependTags) html = injectToBody(html, bodyPrependTags, true)
if (bodyTags) html = injectToBody(html, bodyTags)
}
}

Expand Down

0 comments on commit 09b1517

Please sign in to comment.