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

Function generateBundle

packages/vite/src/node/plugins/importAnalysisBuild.ts:279–645  ·  view source on GitHub ↗
(opts, bundle)

Source from the content-addressed store, hash-verified

277 },
278
279 async generateBundle(opts, bundle) {
280 const { format } = opts
281 if (format !== 'es') {
282 return
283 }
284
285 await init
286
287 // If preload is not enabled, we parse through each imports and remove any imports to pure CSS chunks
288 // as they are removed from the bundle
289 if (!getInsertPreload(this.environment)) {
290 const removedPureCssFiles = removedPureCssFilesCache.get(config)
291 if (removedPureCssFiles && removedPureCssFiles.size > 0) {
292 for (const file in bundle) {
293 const chunk = bundle[file]
294 if (chunk.type === 'chunk' && chunk.code.includes('import')) {
295 const code = chunk.code
296 let imports!: ImportSpecifier[]
297 try {
298 imports = parseImports(code)[0].filter((i) => i.d > -1)
299 } catch (e: any) {
300 const loc = numberToPos(code, e.idx)
301 this.error({
302 name: e.name,
303 message: e.message,
304 stack: e.stack,
305 cause: e.cause,
306 pos: e.idx,
307 loc: { ...loc, file: chunk.fileName },
308 frame: generateCodeFrame(code, loc),
309 })
310 }
311
312 for (const imp of imports) {
313 const {
314 n: name,
315 s: start,
316 e: end,
317 ss: expStart,
318 se: expEnd,
319 } = imp
320 let url = name
321 if (!url) {
322 const rawUrl = code.slice(start, end)
323 if (
324 (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`) ||
325 (rawUrl[0] === '`' && rawUrl[rawUrl.length - 1] === '`')
326 )
327 url = rawUrl.slice(1, -1)
328 }
329 if (!url) continue
330
331 const normalizedFile = path.posix.join(
332 path.posix.dirname(chunk.fileName),
333 url,
334 )
335 if (removedPureCssFiles.has(normalizedFile)) {
336 // remove with Promise.resolve({}) while preserving source map location

Callers

nothing calls this directly

Calls 15

numberToPosFunction · 0.90
generateCodeFrameFunction · 0.90
getImportMapFilenameFunction · 0.90
toOutputFilePathInJSFunction · 0.90
combineSourcemapsFunction · 0.90
genSourceMapUrlFunction · 0.90
getInsertPreloadFunction · 0.85
resolveDependenciesFunction · 0.85
addFileDepFunction · 0.85
toRelativePathFunction · 0.85
findPreloadMarkerFunction · 0.85

Tested by

no test coverage detected