Skip to content

Commit

Permalink
chore: merge some chunk
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <tukon479@gmail.com>
  • Loading branch information
Innei committed Nov 22, 2024
1 parent 86caeb6 commit 8a9cac6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
39 changes: 27 additions & 12 deletions plugins/vite/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string[]> = {
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
}
}
}
},
}
}
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
]),
Expand Down

0 comments on commit 8a9cac6

Please sign in to comment.