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

Function generateBundle

packages/vite/src/node/plugins/manifest.ts:113–172  ·  view source on GitHub ↗
(_, bundle)

Source from the content-addressed store, hash-verified

111 {
112 name: 'native:manifest-compatible',
113 generateBundle(_, bundle) {
114 const asset = bundle[outPath]
115 if (asset.type === 'asset') {
116 let manifest: Manifest | undefined
117 for (const output of Object.values(bundle)) {
118 const importedCss = output.viteMetadata?.importedCss
119 const importedAssets = output.viteMetadata?.importedAssets
120 if (!importedCss?.size && !importedAssets?.size) continue
121 manifest ??= JSON.parse(asset.source.toString()) as Manifest
122 if (output.type === 'chunk') {
123 const item = manifest[getChunkName(output)]
124 if (!item) continue
125 if (importedCss?.size) {
126 item.css = [...importedCss]
127 }
128 if (importedAssets?.size) {
129 item.assets = [...importedAssets]
130 }
131 } else if (output.type === 'asset' && output.names.length > 0) {
132 // Add every unique asset to the manifest, keyed by its original name
133 const keys =
134 output.originalFileNames.length > 0
135 ? output.originalFileNames
136 : [`_${path.basename(output.fileName)}`]
137
138 for (const key of keys) {
139 const item = manifest[key]
140 if (!item) continue
141 if (!(item.file && endsWithJSRE.test(item.file))) {
142 if (importedCss?.size) {
143 item.css = [...importedCss]
144 }
145 if (importedAssets?.size) {
146 item.assets = [...importedAssets]
147 }
148 }
149 }
150 }
151 }
152 const output = this.environment.config.build.rolldownOptions.output
153 const outputLength = Array.isArray(output) ? output.length : 1
154 if (manifest && outputLength === 1) {
155 asset.source = JSON.stringify(manifest, undefined, 2)
156 return
157 }
158
159 const state = getState(this)
160 state.outputCount++
161 state.manifest = Object.assign(
162 state.manifest,
163 manifest ?? JSON.parse(asset.source.toString()),
164 )
165 if (state.outputCount >= outputLength) {
166 asset.source = JSON.stringify(state.manifest, undefined, 2)
167 state.reset()
168 } else {
169 delete bundle[outPath]
170 }

Callers

nothing calls this directly

Calls 3

getChunkNameFunction · 0.85
parseMethod · 0.80
resetMethod · 0.80

Tested by

no test coverage detected