refactor(build): extract chunk group naming from the bundle config

Hoist the groups[].name body out of resolveViteConfig into a
module-level function partially applied with themeEntryRE via bind,
instead of re-creating a closure over bundle() locals per config.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
pull/5341/head
Divyansh Singh 1 week ago
parent 9d00630cce
commit 310a8e5015

@ -122,45 +122,7 @@ export async function bundle(
: `${config.assetsDir}/chunks/[name].[hash].js`
},
codeSplitting: {
groups: [
{
name(id, ctx) {
const getModuleInfo = ctx.getModuleInfo.bind(ctx)
// avoid emitting multiple files for assets
// see: https://github.com/rolldown/rolldown/issues/4246
if (getModuleInfo(id)?.meta['vite:asset']) {
return 'assets'
}
// move known framework code into a stable chunk so that
// custom theme changes do not invalidate hash for all pages
if (
id.startsWith('\0vite') ||
id.includes('plugin-vue:export-helper') ||
(id.includes(`${clientDir}/app`) &&
id !== `${clientDir}/app/index.js`) ||
(isEagerChunk(id, getModuleInfo) &&
/@vue\/(runtime|shared|reactivity)/.test(id))
) {
return 'framework'
}
if (
(id.startsWith(`${clientDir}/theme-default`) ||
!excludedModules.some((i) => id.includes(i))) &&
staticImportedByEntry(
id,
getModuleInfo,
cacheTheme,
themeEntryRE
)
) {
return 'theme'
}
}
}
]
groups: [{ name: chunkName.bind(null, themeEntryRE) }]
}
})
},
@ -228,6 +190,40 @@ export async function bundle(
const cache = new Map<string, boolean>()
const cacheTheme = new Map<string, boolean>()
function chunkName(
themeEntryRE: RegExp,
id: string,
ctx: { getModuleInfo: Rolldown.GetModuleInfo }
): string | undefined {
const getModuleInfo = ctx.getModuleInfo.bind(ctx)
// avoid emitting multiple files for assets
// see: https://github.com/rolldown/rolldown/issues/4246
if (getModuleInfo(id)?.meta['vite:asset']) {
return 'assets'
}
// move known framework code into a stable chunk so that
// custom theme changes do not invalidate hash for all pages
if (
id.startsWith('\0vite') ||
id.includes('plugin-vue:export-helper') ||
(id.includes(`${clientDir}/app`) && id !== `${clientDir}/app/index.js`) ||
(isEagerChunk(id, getModuleInfo) &&
/@vue\/(runtime|shared|reactivity)/.test(id))
) {
return 'framework'
}
if (
(id.startsWith(`${clientDir}/theme-default`) ||
!excludedModules.some((i) => id.includes(i))) &&
staticImportedByEntry(id, getModuleInfo, cacheTheme, themeEntryRE)
) {
return 'theme'
}
}
/**
* Check if a module is statically imported by at least one entry.
*/

Loading…
Cancel
Save