From 1a368dc5fff388d6ee02b9c7eb7de17c49583df4 Mon Sep 17 00:00:00 2001 From: Arianrhod Sandlot Date: Sat, 21 Oct 2023 11:56:45 +0800 Subject: [PATCH] build: use manualChunks --- vite.config.ts | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index 442ddb4..b129cd4 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,7 +3,7 @@ import { promisify } from 'node:util' import htmlMinimize from '@sergeymakinen/vite-plugin-html-minimize' import react from '@vitejs/plugin-react-swc' import { formatISO } from 'date-fns' -import { defineConfig, splitVendorChunkPlugin } from 'vite' +import { defineConfig } from 'vite' import { VitePWA } from 'vite-plugin-pwa' export default defineConfig(async () => { @@ -16,10 +16,32 @@ export default defineConfig(async () => { GIT_VERSION: JSON.stringify(shortVersion), BUILD_TIME: JSON.stringify(formatISO(new Date())), }, - build: { rollupOptions: { input: ['index.html', 'privacy-policy.html'] } }, + build: { + rollupOptions: { + input: ['index.html', 'privacy-policy.html'], + output: { + manualChunks(id) { + if (id.includes('node_modules')) { + const namedChunks = ['nostalgist', 'jquery', 'dropbox', '@microsoft/microsoft-graph-client'] + for (const namedChunk of namedChunks) { + if (id.includes(`/${namedChunk}/`)) { + return namedChunk.replace('@', '').replace('/', '-') + } + } + const frameworkPackages = ['react', 'react-dom', '@radix-ui', 'framer-motion', 'react-window', 'wouter'] + for (const frameworkPackage of frameworkPackages) { + if (id.includes(`/${frameworkPackage}/`)) { + return 'framework' + } + } + return 'library' + } + }, + }, + }, + }, plugins: [ react(), - splitVendorChunkPlugin(), // @ts-expect-error the package provided a wrong type decleration htmlMinimize.default(), VitePWA({