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

Function transformIndexHtml

packages/plugin-legacy/src/index.ts:635–791  ·  view source on GitHub ↗
(html, { chunk, bundle })

Source from the content-addressed store, hash-verified

633 },
634
635 transformIndexHtml(html, { chunk, bundle }) {
636 if (config.build.ssr) return
637 if (!chunk) return
638 if (chunk.fileName.includes('-legacy')) {
639 // The legacy bundle is built first, and its index.html isn't actually emitted if
640 // modern bundle will be generated. Here we simply record its corresponding legacy chunk.
641 facadeToLegacyChunkMap.set(chunk.facadeModuleId, chunk.fileName)
642 if (config.build.chunkImportMap) {
643 facadeToLegacyImportMap.set(
644 chunk.facadeModuleId,
645 bundle![getImportMapFilename(config)]! as Rollup.OutputAsset,
646 )
647 }
648 if (genModern) {
649 return
650 }
651 }
652 if (!genModern) {
653 html = html
654 .replace(/<script type="module".*?<\/script>/g, '')
655 .replace(modulePreloadLinkRE, '')
656 }
657
658 const tags: HtmlTagDescriptor[] = []
659 const htmlFilename = chunk.facadeModuleId?.replace(/\?.*$/, '')
660
661 // 1. inject modern polyfills
662 if (genModern) {
663 const modernPolyfillFilename = facadeToModernPolyfillMap.get(
664 chunk.facadeModuleId,
665 )
666
667 if (modernPolyfillFilename) {
668 tags.push({
669 tag: 'script',
670 attrs: {
671 type: 'module',
672 crossorigin: true,
673 src: toAssetPathFromHtml(
674 modernPolyfillFilename,
675 chunk.facadeModuleId!,
676 config,
677 ),
678 },
679 })
680 } else if (modernPolyfills.size) {
681 throw new Error(
682 `No corresponding modern polyfill chunk found for ${htmlFilename}`,
683 )
684 }
685 }
686
687 if (!genLegacy) {
688 return { html, tags }
689 }
690
691 // 2. inject importmaps
692 if (config.build.chunkImportMap) {

Callers

nothing calls this directly

Calls 4

toAssetPathFromHtmlFunction · 0.85
getImportMapFilenameFunction · 0.70
setMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected