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

Function renderChunk

packages/vite/src/node/plugins/css.ts:651–978  ·  view source on GitHub ↗
(code, chunk, opts, meta)

Source from the content-addressed store, hash-verified

649 ...(config.command === 'build'
650 ? {
651 async renderChunk(code, chunk, opts, meta) {
652 let chunkCSS: string | undefined
653 const renderedModules = new Proxy(
654 {} as Record<string, RenderedModule | undefined>,
655 {
656 get(_target, p) {
657 for (const name in meta.chunks) {
658 const modules = meta.chunks[name].modules
659 const module = modules[p as string]
660 if (module) {
661 return module
662 }
663 }
664 },
665 },
666 )
667 // the chunk is empty if it's a dynamic entry chunk that only contains a CSS import
668 const isJsChunkEmpty = code === '' && !chunk.isEntry
669 let isPureCssChunk = chunk.exports.length === 0
670 const ids = Object.keys(chunk.modules)
671 for (const id of ids) {
672 if (styles.has(id)) {
673 // ?transform-only is used for ?url and shouldn't be included in normal CSS chunks
674 if (transformOnlyRE.test(id)) {
675 continue
676 }
677
678 // If this CSS is scoped to its importers exports, check if those importers exports
679 // are rendered in the chunks. If they are not, we can skip bundling this CSS.
680 const cssScopeTo =
681 this.getModuleInfo(id)?.meta?.vite?.cssScopeTo
682 if (
683 cssScopeTo &&
684 !isCssScopeToRendered(cssScopeTo, renderedModules)
685 ) {
686 continue
687 }
688
689 // a css module contains JS, so it makes this not a pure css chunk
690 if (cssModuleRE.test(id)) {
691 isPureCssChunk = false
692 }
693
694 chunkCSS = (chunkCSS || '') + styles.get(id)
695 } else if (!isJsChunkEmpty) {
696 // if the module does not have a style, then it's not a pure css chunk.
697 // this is true because in the `transform` hook above, only modules
698 // that are css gets added to the `styles` map.
699 isPureCssChunk = false
700 }
701 }
702
703 const publicAssetUrlMap = publicAssetUrlCache.get(config)!
704
705 // resolve asset URL placeholders to their built file URLs
706 const resolveAssetUrlsInCss = (
707 chunkCSS: string,
708 cssAssetName: string,

Callers

nothing calls this directly

Calls 15

cleanUrlFunction · 0.90
toOutputFilePathInJSFunction · 0.90
encodeURIPathFunction · 0.90
isCSSRequestFunction · 0.90
getChunkOriginalFileNameFunction · 0.90
renderAssetUrlInJSFunction · 0.90
isCssScopeToRenderedFunction · 0.85
ensureFileExtFunction · 0.85
resolveAssetUrlsInCssFunction · 0.85
finalizeCssFunction · 0.85
injectInlinedCSSFunction · 0.85

Tested by

no test coverage detected