fix: don't invalidate framework chunk when a new asset is added

pull/5297/head
Divyansh Singh 2 months ago
parent 1ade7bd422
commit c0e2e18094

@ -16,7 +16,7 @@ import { escapeRegExp, sanitizeFileName, slash } from '../shared'
import { task } from '../utils/task' import { task } from '../utils/task'
import { buildMPAClient } from './buildMPAClient' 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 = const CSS_LANGS_RE =
/\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/ /\.(css|less|sass|scss|styl|stylus|pcss|postcss|sss)(?:$|\?)/
@ -125,35 +125,24 @@ export async function bundle(
codeSplitting: { codeSplitting: {
groups: [ groups: [
{ {
name( name(id, ctx) {
id: string, const getModuleInfo = ctx.getModuleInfo.bind(ctx)
ctx: Pick<Rolldown.PluginContext, 'getModuleInfo'>
) { // avoid emitting multiple files for assets
// ctx.getModuleInfo must not be called detached from ctx // see: https://github.com/rolldown/rolldown/issues/4246
const getModuleInfo: Rolldown.GetModuleInfo = ( if (getModuleInfo(id)?.meta['vite:asset']) {
moduleId return 'assets'
) => ctx.getModuleInfo(moduleId) }
// move known framework code into a stable chunk so that // move known framework code into a stable chunk so that
// custom theme changes do not invalidate hash for all pages // custom theme changes do not invalidate hash for all pages
if ( if (
id.startsWith('\0vite') || id.startsWith('\0vite') ||
getModuleInfo(id)?.meta['vite:asset'] id.includes('plugin-vue:export-helper') ||
) { (id.includes(`${clientDir}/app`) &&
return 'framework' id !== `${clientDir}/app/index.js`) ||
} (isEagerChunk(id, getModuleInfo) &&
if (id.includes('plugin-vue:export-helper')) { /@vue\/(runtime|shared|reactivity)/.test(id))
return 'framework'
}
if (
id.includes(`${clientDir}/app`) &&
id !== `${clientDir}/app/index.js`
) {
return 'framework'
}
if (
isEagerChunk(id, getModuleInfo) &&
/@vue\/(runtime|shared|reactivity)/.test(id)
) { ) {
return 'framework' return 'framework'
} }

@ -2,7 +2,7 @@ import { isBooleanAttr } from '@vue/shared'
import fs from 'node:fs' import fs from 'node:fs'
import { mkdir, writeFile } from 'node:fs/promises' import { mkdir, writeFile } from 'node:fs/promises'
import path from 'node:path' 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 { version } from '../../../package.json'
import type { SiteConfig } from '../config' import type { SiteConfig } from '../config'
import { import {
@ -246,7 +246,7 @@ async function renderHead(head: HeadConfig[]): Promise<string> {
tag === 'script' && tag === 'script' &&
(attrs.type === undefined || attrs.type.includes('javascript')) (attrs.type === undefined || attrs.type.includes('javascript'))
) { ) {
innerHTML = await minifyScript(innerHTML, 'inline-script.js') innerHTML = minifySync('inline-script.js', innerHTML).code
} }
return `${openTag}${innerHTML}</${tag}>` return `${openTag}${innerHTML}</${tag}>`
} else { } else {
@ -266,10 +266,6 @@ function renderAttrs(attrs: Record<string, string>): string {
.join('') .join('')
} }
async function minifyScript(code: string, filename: string): Promise<string> {
return (await minify(filename, code)).code.trim()
}
function filterOutHeadDescription(head: HeadConfig[] = []) { function filterOutHeadDescription(head: HeadConfig[] = []) {
return head.filter(([type, attrs]) => { return head.filter(([type, attrs]) => {
return !(type === 'meta' && attrs?.name === 'description') return !(type === 'meta' && attrs?.name === 'description')

Loading…
Cancel
Save