Skip to content

Commit

Permalink
feat: manual chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
logustra committed Dec 25, 2022
1 parent 6701e0b commit 99b477d
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ import ViteIconsResolver from 'unplugin-icons/resolver'
import ViteFonts from 'vite-plugin-fonts'
import ViteI18n from '@intlify/unplugin-vue-i18n/vite'
import ViteVisualizer from 'rollup-plugin-visualizer'
import { dependencies } from './package.json'

export default defineConfig(({ mode }) => {
const isProd = mode === 'prod'
const isDev = mode === 'dev'
const isReport = mode === 'report'

const PRESETS = [
'vue',
'pinia',
'vue-router',
'vue-i18n',
]

const plugins = [
Vue(),

/**
* DESC:
* auto import APIs
Expand All @@ -38,12 +48,7 @@ export default defineConfig(({ mode }) => {
addons: {
vueTemplate: true,
},
presets: [
'vue',
'pinia',
'vue-router',
'vue-i18n',
],
presets: PRESETS,
}),

/**
Expand Down Expand Up @@ -106,17 +111,39 @@ export default defineConfig(({ mode }) => {
}),
]

if (
isProd
|| isDev
|| isReport
) plugins.push(Vue())
const VENDORS = [
'@vuelidate/core',
'@vuelidate/validators',
'element-plus',
]

function vendorChunks(deps: Record<string, string>) {
const chunks = {}

Object.keys(deps).forEach((dep) => {
if (VENDORS.includes(dep))
chunks[`vendor.${dep}`] = [dep]
})

return chunks
}

let build = {}
if (isProd) {
build = {
target: 'es2015',
manifest: true,
rollupOptions: {
output: {
manualChunks: {
/**
* DESC:
* split 3rd party libraries
*/
...vendorChunks(dependencies),
},
},
},
}
}

Expand All @@ -127,12 +154,7 @@ export default defineConfig(({ mode }) => {
* dependency pre-bundling
*/
optimizeDeps = {
include: [
'vue',
'pinia',
'vue-router',
'vue-i18n',
],
include: PRESETS,
}
}

Expand Down

0 comments on commit 99b477d

Please sign in to comment.