MCPcopy
hub / github.com/vitejs/vite / injectToBody

Function injectToBody

packages/vite/src/node/plugins/html.ts:1562–1599  ·  view source on GitHub ↗
(
  html: string,
  tags: HtmlTagDescriptor[],
  prepend = false,
)

Source from the content-addressed store, hash-verified

1560}
1561
1562function injectToBody(
1563 html: string,
1564 tags: HtmlTagDescriptor[],
1565 prepend = false,
1566) {
1567 if (tags.length === 0) return html
1568
1569 if (prepend) {
1570 // inject after body open
1571 if (bodyPrependInjectRE.test(html)) {
1572 return html.replace(
1573 bodyPrependInjectRE,
1574 (match, p1) => `${match}\n${serializeTags(tags, incrementIndent(p1))}`,
1575 )
1576 }
1577 // if no there is no body tag, inject after head or fallback to prepend in html
1578 if (headInjectRE.test(html)) {
1579 return html.replace(
1580 headInjectRE,
1581 (match, p1) => `${match}\n${serializeTags(tags, p1)}`,
1582 )
1583 }
1584 return prependInjectFallback(html, tags)
1585 } else {
1586 // inject before body close
1587 if (bodyInjectRE.test(html)) {
1588 return html.replace(
1589 bodyInjectRE,
1590 (match, p1) => `${serializeTags(tags, incrementIndent(p1))}${match}`,
1591 )
1592 }
1593 // if no body tag is present, append to the html tag, or at the end of the file
1594 if (htmlInjectRE.test(html)) {
1595 return html.replace(htmlInjectRE, `${serializeTags(tags)}\n$&`)
1596 }
1597 return html + `\n` + serializeTags(tags)
1598 }
1599}
1600
1601function prependInjectFallback(html: string, tags: HtmlTagDescriptor[]) {
1602 // prepend to the html tag, append after doctype, or the document start

Callers 1

applyHtmlTransformsFunction · 0.85

Calls 3

serializeTagsFunction · 0.85
incrementIndentFunction · 0.85
prependInjectFallbackFunction · 0.85

Tested by

no test coverage detected