From 8a9cac6ea3e93bdd0c1ef0733e26588df001da68 Mon Sep 17 00:00:00 2001 From: Innei Date: Fri, 22 Nov 2024 20:18:48 +0800 Subject: [PATCH] chore: merge some chunk Signed-off-by: Innei --- plugins/vite/deps.ts | 39 +++++++++++++++++++++++++++------------ vite.config.ts | 2 +- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/plugins/vite/deps.ts b/plugins/vite/deps.ts index a152f20a2..da2c254a8 100644 --- a/plugins/vite/deps.ts +++ b/plugins/vite/deps.ts @@ -10,24 +10,39 @@ export function createDependencyChunksPlugin(dependencies: string[][]): Plugin { const { output } = config.build.rollupOptions const outputConfig = Array.isArray(output) ? output[0] : output - outputConfig.manualChunks = outputConfig.manualChunks || {} outputConfig.assetFileNames = "assets/[name].[hash:6][extname]" outputConfig.chunkFileNames = (chunkInfo) => { return chunkInfo.name.startsWith("vendor/") ? "[name]-[hash].js" : "assets/[name]-[hash].js" } - const manualChunks = Array.isArray(output) ? output[0].manualChunks : output.manualChunks - - if (typeof manualChunks !== "object") return - - dependencies.forEach((dep, index) => { - if (Array.isArray(dep)) { - const chunkName = `vendor/${index}` - manualChunks[chunkName] = dep - } else { - manualChunks[`vendor/${dep}`] = [dep] + outputConfig.manualChunks = (id: string) => { + const matchedDep = dependencies.findIndex((dep) => + dep.some((d) => id.includes(`/node_modules/${d}`)), + ) + if (matchedDep !== -1) { + return `vendor/${matchedDep}` } - }) + const chunkMap: Record = { + modules: ["/src/modules"], + infra: [ + "/src/store", + "/src/atoms", + "/src/database", + "/src/services", + "/src/initialize", + "/src/lib", + "/src/queries", + ], + components: ["/src/components", "/src/hooks"], + packages: ["@follow/"], + app: ["/src/pages", "/src/providers", "/src/constants", "/src/App", "/src/main"], + } + for (const [chunk, paths] of Object.entries(chunkMap)) { + if (paths.some((path) => id.includes(path))) { + return chunk + } + } + } }, } } diff --git a/vite.config.ts b/vite.config.ts index 9248ef8c3..0304d2bdf 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -222,7 +222,7 @@ export default ({ mode }) => { "@tanstack/query-sync-storage-persister", ], ["tldts"], - ["shiki", "@shikijs/transformers"], + ["@sentry/react", "@openpanel/web"], ["zod", "react-hook-form", "@hookform/resolvers"], ]),