build: optimize Rolldown chunk splitting

This commit is contained in:
shuaiplus
2026-07-25 22:49:02 +08:00
parent df493b4697
commit 34fd2f0259
+22 -61
View File
@@ -258,39 +258,12 @@ function searchIndexPolicyPlugin(isDemo: boolean): Plugin {
}; };
} }
function resourcePriorityPlugin(isDemo: boolean): Plugin {
return {
name: 'nodewarden-resource-priority',
enforce: 'post' as const,
transformIndexHtml(html: string) {
if (isDemo || !html.includes('/assets/app-suite-')) return html;
const scriptMatch = html.match(/^\s*<script type="module" crossorigin src="\/assets\/index-[^"]+\.js"><\/script>\s*$/m);
const appSuiteMatch = html.match(/^\s*<link rel="modulepreload" crossorigin href="\/assets\/app-suite-[^"]+\.js">\s*$/m);
const stylesheetMatch = html.match(/^\s*<link rel="stylesheet" crossorigin href="\/assets\/index-[^"]+\.css">\s*$/m);
if (!scriptMatch || !appSuiteMatch || !stylesheetMatch) return html;
const prioritizedTags = [
stylesheetMatch[0].replace('rel="stylesheet"', 'rel="stylesheet" fetchpriority="high"'),
appSuiteMatch[0].replace('rel="modulepreload"', 'rel="modulepreload" fetchpriority="high"'),
scriptMatch[0].replace('type="module"', 'type="module" fetchpriority="high"'),
].join('\n');
return html
.replace(scriptMatch[0], '')
.replace(appSuiteMatch[0], '')
.replace(stylesheetMatch[0], prioritizedTags);
},
};
}
export default defineConfig(({ mode }) => { export default defineConfig(({ mode }) => {
const isDemo = mode === 'demo'; const isDemo = mode === 'demo';
return { return {
root: rootDir, root: rootDir,
plugins: [preact(), searchIndexPolicyPlugin(isDemo), resourcePriorityPlugin(isDemo), pwaServiceWorkerPlugin(isDemo)], plugins: [preact(), searchIndexPolicyPlugin(isDemo), pwaServiceWorkerPlugin(isDemo)],
define: { define: {
__NODEWARDEN_DEMO__: JSON.stringify(isDemo), __NODEWARDEN_DEMO__: JSON.stringify(isDemo),
}, },
@@ -311,41 +284,29 @@ export default defineConfig(({ mode }) => {
sourcemap: false, sourcemap: false,
target: 'esnext', target: 'esnext',
chunkSizeWarningLimit: 800, chunkSizeWarningLimit: 800,
rollupOptions: { rolldownOptions: {
treeshake: { checks: {
preset: 'smallest', pluginTimings: false,
}, },
output: { output: {
manualChunks(id) { codeSplitting: {
const normalized = id.replace(/\\/g, '/'); groups: [
{
const localeMatch = normalized.match(/\/src\/lib\/i18n\/locales\/(.+)\.ts$/); name: 'shared',
if (localeMatch) { minShareCount: 2,
if (localeMatch[1] === 'en') return undefined; minSize: 50 * 1024,
return `i18n-${localeMatch[1]}`; priority: 10,
} },
{
if ( name(id) {
!isDemo && const normalized = id.replace(/\\/g, '/');
( const localeMatch = normalized.match(/\/src\/lib\/i18n\/locales\/(.+)\.ts$/);
normalized.includes('/src/components/VaultPage.tsx') || return localeMatch && localeMatch[1] !== 'en' ? `i18n-${localeMatch[1]}` : null;
normalized.includes('/src/components/ImportPage.tsx') || },
normalized.includes('/src/lib/import-') || test: /[\\/]src[\\/]lib[\\/]i18n[\\/]locales[\\/]/,
normalized.includes('/src/lib/export-formats.ts') || priority: 20,
normalized.includes('/src/components/SendsPage.tsx') || },
normalized.includes('/src/components/TotpCodesPage.tsx') || ],
normalized.includes('/src/components/DomainRulesPage.tsx') ||
normalized.includes('/src/components/BackupCenterPage.tsx') ||
normalized.includes('/src/components/backup-center/') ||
normalized.includes('/src/components/SettingsPage.tsx') ||
normalized.includes('/src/components/SecurityDevicesPage.tsx') ||
normalized.includes('/src/components/AdminPage.tsx')
)
) {
return 'app-suite';
}
return undefined;
}, },
}, },
}, },