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

Function injectToHead

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

Source from the content-addressed store, hash-verified

1524const doctypePrependInjectRE = /<!doctype html>/i
1525
1526function injectToHead(
1527 html: string,
1528 tags: HtmlTagDescriptor[],
1529 prepend = false,
1530) {
1531 if (tags.length === 0) return html
1532
1533 if (prepend) {
1534 // inject as the first element of head
1535 if (headPrependInjectRE.test(html)) {
1536 return html.replace(
1537 headPrependInjectRE,
1538 (match, p1) => `${match}\n${serializeTags(tags, incrementIndent(p1))}`,
1539 )
1540 }
1541 } else {
1542 // inject before head close
1543 if (headInjectRE.test(html)) {
1544 // respect indentation of head tag
1545 return html.replace(
1546 headInjectRE,
1547 (match, p1) => `${serializeTags(tags, incrementIndent(p1))}${match}`,
1548 )
1549 }
1550 // try to inject before the body tag
1551 if (bodyPrependInjectRE.test(html)) {
1552 return html.replace(
1553 bodyPrependInjectRE,
1554 (match, p1) => `${serializeTags(tags, p1)}\n${match}`,
1555 )
1556 }
1557 }
1558 // if no head tag is present, we prepend the tag for both prepend and append
1559 return prependInjectFallback(html, tags)
1560}
1561
1562function injectToBody(
1563 html: string,

Callers 2

generateBundleFunction · 0.85
applyHtmlTransformsFunction · 0.85

Calls 3

serializeTagsFunction · 0.85
incrementIndentFunction · 0.85
prependInjectFallbackFunction · 0.85

Tested by

no test coverage detected