Skip to content

Commit

Permalink
fix: manualChunks
Browse files Browse the repository at this point in the history
  • Loading branch information
cole committed Jul 26, 2024
1 parent 0acd11c commit 22f097b
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'

import { defineConfig, loadEnv } from 'vite'
import { defineConfig, isCSSRequest, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { reduce } from 'lodash-es'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

const vendors = reduce({
'vue-vendor': ['vue', 'vue-router', 'pinia'],
'antd-vendor': ['ant-design-vue', '@ant-design/icons-vue'],
'utils-vendor': ['dayjs', 'lodash-es'],
'common-vendor': ['axios', 'vue-i18n', 'js-cookie', 'nprogress']
}, (result, modules, key) => {
const needVendors = modules.map((name) => {
return { name: name, vendor: key }
})
return [...result, ...needVendors]
}, [])

function manualChunks (id) {
if (id.includes('/node_modules/') && !isCSSRequest(id)) {
const result = vendors.find((item) => {
return id.includes(`/node_modules/${item.name}/`)
})
return result ? result.vendor : undefined
}
return undefined
}

export default defineConfig((config) => {
const env = loadEnv(config.mode, __dirname, ['VITE_', 'ENV_'])

Expand All @@ -23,12 +46,7 @@ export default defineConfig((config) => {
build: {
rollupOptions: {
output: {
manualChunks: {
'vue-vendor': ['vue', 'vue-router', 'pinia'],
'antd-vendor': ['ant-design-vue', '@ant-design/icons-vue'],
'utils-vendor': ['dayjs', 'lodash-es'],
'common-vendor': ['axios', 'vue-i18n', 'js-cookie', 'nprogress']
}
manualChunks: manualChunks
}
}
},
Expand Down

0 comments on commit 22f097b

Please sign in to comment.