Skip to content

Commit 09b1517

Browse files
authored
refactor: append tags logic in applyHtmlTransforms (#15647)
1 parent 94cac0a commit 09b1517

File tree

1 file changed

+20
-16
lines changed
  • packages/vite/src/node/plugins

1 file changed

+20
-16
lines changed

packages/vite/src/node/plugins/html.ts

+20-16
Original file line numberDiff line numberDiff line change
@@ -1203,27 +1203,31 @@ export async function applyHtmlTransforms(
12031203
tags = res.tags
12041204
}
12051205

1206-
const headTags: HtmlTagDescriptor[] = []
1207-
const headPrependTags: HtmlTagDescriptor[] = []
1208-
const bodyTags: HtmlTagDescriptor[] = []
1209-
const bodyPrependTags: HtmlTagDescriptor[] = []
1206+
let headTags: HtmlTagDescriptor[] | undefined
1207+
let headPrependTags: HtmlTagDescriptor[] | undefined
1208+
let bodyTags: HtmlTagDescriptor[] | undefined
1209+
let bodyPrependTags: HtmlTagDescriptor[] | undefined
12101210

12111211
for (const tag of tags) {
1212-
if (tag.injectTo === 'body') {
1213-
bodyTags.push(tag)
1214-
} else if (tag.injectTo === 'body-prepend') {
1215-
bodyPrependTags.push(tag)
1216-
} else if (tag.injectTo === 'head') {
1217-
headTags.push(tag)
1218-
} else {
1219-
headPrependTags.push(tag)
1212+
switch (tag.injectTo) {
1213+
case 'body':
1214+
;(bodyTags ??= []).push(tag)
1215+
break
1216+
case 'body-prepend':
1217+
;(bodyPrependTags ??= []).push(tag)
1218+
break
1219+
case 'head':
1220+
;(headTags ??= []).push(tag)
1221+
break
1222+
default:
1223+
;(headPrependTags ??= []).push(tag)
12201224
}
12211225
}
12221226

1223-
html = injectToHead(html, headPrependTags, true)
1224-
html = injectToHead(html, headTags)
1225-
html = injectToBody(html, bodyPrependTags, true)
1226-
html = injectToBody(html, bodyTags)
1227+
if (headPrependTags) html = injectToHead(html, headPrependTags, true)
1228+
if (headTags) html = injectToHead(html, headTags)
1229+
if (bodyPrependTags) html = injectToBody(html, bodyPrependTags, true)
1230+
if (bodyTags) html = injectToBody(html, bodyTags)
12271231
}
12281232
}
12291233

0 commit comments

Comments
 (0)