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

Function generateBundle

packages/vite/src/node/plugins/html.ts:824–1081  ·  view source on GitHub ↗
(options, bundle)

Source from the content-addressed store, hash-verified

822 },
823
824 async generateBundle(options, bundle) {
825 const analyzedImportedCssFiles = new Map<OutputChunk, string[]>()
826 const inlineEntryChunk = new Set<string>()
827 const getImportedChunks = (
828 chunk: OutputChunk,
829 seen: Set<string> = new Set(),
830 ): (OutputChunk | string)[] => {
831 const chunks: (OutputChunk | string)[] = []
832 chunk.imports.forEach((file) => {
833 const importee = bundle[file]
834 if (importee) {
835 if (importee.type === 'chunk' && !seen.has(file)) {
836 seen.add(file)
837
838 // post-order traversal
839 chunks.push(...getImportedChunks(importee, seen))
840 chunks.push(importee)
841 }
842 } else {
843 // external imports
844 chunks.push(file)
845 }
846 })
847 return chunks
848 }
849
850 const toScriptTag = (
851 chunkOrUrl: OutputChunk | string,
852 toOutputPath: (filename: string) => string,
853 isAsync: boolean,
854 ): HtmlTagDescriptor => ({
855 tag: 'script',
856 attrs: {
857 ...(isAsync ? { async: true } : {}),
858 type: 'module',
859 // crossorigin must be set not only for serving assets in a different origin
860 // but also to make it possible to preload the script using `<link rel="preload">`.
861 // `<script type="module">` used to fetch the script with credential mode `omit`,
862 // however `crossorigin` attribute cannot specify that value.
863 // https://developer.chrome.com/blog/modulepreload/#ok-so-why-doesnt-link-relpreload-work-for-modules:~:text=For%20%3Cscript%3E,of%20other%20modules.
864 // Now `<script type="module">` uses `same origin`: https://github.com/whatwg/html/pull/3656#:~:text=Module%20scripts%20are%20always%20fetched%20with%20credentials%20mode%20%22same%2Dorigin%22%20by%20default%20and%20can%20no%20longer%0Ause%20%22omit%22
865 crossorigin: true,
866 src:
867 typeof chunkOrUrl === 'string'
868 ? chunkOrUrl
869 : toOutputPath(chunkOrUrl.fileName),
870 },
871 })
872
873 const toPreloadTag = (
874 filename: string,
875 toOutputPath: (filename: string) => string,
876 ): HtmlTagDescriptor => ({
877 tag: 'link',
878 attrs: {
879 rel: 'modulepreload',
880 crossorigin: true,
881 href: toOutputPath(filename),

Callers

nothing calls this directly

Calls 15

normalizePathFunction · 0.90
cleanUrlFunction · 0.90
encodeURIPathFunction · 0.90
getPublicAssetFilenameFunction · 0.90
isEntirelyImportFunction · 0.85
getImportedChunksFunction · 0.85
toScriptTagFunction · 0.85
resolveDependenciesFunction · 0.85
toPreloadTagFunction · 0.85
getCssTagsForChunkFunction · 0.85
injectToHeadFunction · 0.85
toOutputAssetFilePathFunction · 0.85

Tested by

no test coverage detected