diff --git a/src/node/build/bundle.ts b/src/node/build/bundle.ts index c98eaa1a..ad25459b 100644 --- a/src/node/build/bundle.ts +++ b/src/node/build/bundle.ts @@ -16,7 +16,7 @@ import { escapeRegExp, sanitizeFileName, slash } from '../shared' import { task } from '../utils/task' import { buildMPAClient } from './buildMPAClient' -// https://github.com/vitejs/vite/blob/d2aa0969ee316000d3b957d7e879f001e85e369e/packages/vite/src/node/plugins/splitVendorChunk.ts#L14 +// https://github.com/vitejs/vite/blob/a55d0b34400e3360c4100d05e422ae9cf10fa07b/packages/vite/src/node/constants.ts#L50 const CSS_LANGS_RE = /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/ @@ -125,35 +125,24 @@ export async function bundle( codeSplitting: { groups: [ { - name( - id: string, - ctx: Pick - ) { - // ctx.getModuleInfo must not be called detached from ctx - const getModuleInfo: Rolldown.GetModuleInfo = ( - moduleId - ) => ctx.getModuleInfo(moduleId) + 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') || - getModuleInfo(id)?.meta['vite:asset'] - ) { - return 'framework' - } - if (id.includes('plugin-vue:export-helper')) { - return 'framework' - } - if ( - id.includes(`${clientDir}/app`) && - id !== `${clientDir}/app/index.js` - ) { - return 'framework' - } - if ( - isEagerChunk(id, getModuleInfo) && - /@vue\/(runtime|shared|reactivity)/.test(id) + 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' } diff --git a/src/node/build/render.ts b/src/node/build/render.ts index 14410fe5..570418ff 100644 --- a/src/node/build/render.ts +++ b/src/node/build/render.ts @@ -2,7 +2,7 @@ import { isBooleanAttr } from '@vue/shared' import fs from 'node:fs' import { mkdir, writeFile } from 'node:fs/promises' import path from 'node:path' -import { minify, normalizePath, type Rolldown } from 'vite' +import { minifySync, normalizePath, type Rolldown } from 'vite' import { version } from '../../../package.json' import type { SiteConfig } from '../config' import { @@ -246,7 +246,7 @@ async function renderHead(head: HeadConfig[]): Promise { tag === 'script' && (attrs.type === undefined || attrs.type.includes('javascript')) ) { - innerHTML = await minifyScript(innerHTML, 'inline-script.js') + innerHTML = minifySync('inline-script.js', innerHTML).code } return `${openTag}${innerHTML}` } else { @@ -266,10 +266,6 @@ function renderAttrs(attrs: Record): string { .join('') } -async function minifyScript(code: string, filename: string): Promise { - return (await minify(filename, code)).code.trim() -} - function filterOutHeadDescription(head: HeadConfig[] = []) { return head.filter(([type, attrs]) => { return !(type === 'meta' && attrs?.name === 'description')