mirror of
https://github.com/Afilmory/afilmory
synced 2026-05-01 18:26:41 +00:00
Signed-off-by: Innei <tukon479@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import type { Plugin, UserConfig } from 'vite'
|
|
|
|
export function createDependencyChunksPlugin(dependencies: string[][]): Plugin {
|
|
return {
|
|
name: 'dependency-chunks',
|
|
config(config: UserConfig) {
|
|
config.build = config.build || {}
|
|
config.build.rollupOptions = config.build.rollupOptions || {}
|
|
config.build.rollupOptions.output = config.build.rollupOptions.output || {}
|
|
|
|
const { output } = config.build.rollupOptions
|
|
const outputConfig = Array.isArray(output) ? output[0] : output
|
|
outputConfig.assetFileNames = 'assets/[name].[hash:6][extname]'
|
|
outputConfig.chunkFileNames = (chunkInfo) => {
|
|
return chunkInfo.name.startsWith('vendor/') ? '[name]-[hash].js' : 'assets/[name]-[hash].js'
|
|
}
|
|
|
|
outputConfig.manualChunks = (id: string, { getModuleInfo }) => {
|
|
const moduleInfo = getModuleInfo(id)
|
|
if (moduleInfo?.dynamicImporters?.length && moduleInfo?.importers?.length) {
|
|
return null
|
|
}
|
|
|
|
const matchedDep = dependencies.findIndex((dep) => {
|
|
return dep.some((d) => {
|
|
const pattern = `/node_modules/${d}/`
|
|
return id.includes(pattern) && !id.includes(`${pattern}node_modules/`)
|
|
})
|
|
})
|
|
|
|
if (matchedDep !== -1) {
|
|
return `vendor/${matchedDep}`
|
|
}
|
|
|
|
return null
|
|
}
|
|
},
|
|
}
|
|
}
|