-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvite.config.js
115 lines (105 loc) · 3.1 KB
/
vite.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const TITLE = 'Vite ElementPlus Vue Template'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import Components from 'unplugin-vue-components/vite'
import ElementPlus from 'unplugin-element-plus/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { visualizer } from 'rollup-plugin-visualizer'
import externalGlobals from 'rollup-plugin-external-globals'
import viteCompression from 'vite-plugin-compression'
import { createHtmlPlugin } from 'vite-plugin-html'
let cdn = ''
// const cdnDomain = 'https://cdn.jsdelivr.net/npm/'
// // 須注意版號是否有跟 package.json 一致
// const cdnData = [
// '[email protected]/dist/vue.runtime.global.prod.js',
// '[email protected]/lib/index.iife.min.js',
// '[email protected]/dist/pinia.iife.min.js',
// '[email protected]/dist/vue-router.global.min.js',
// '[email protected]/dist/index.full.min.js',
// ]
// cdnData.forEach((str) => (cdn += `<script src="${cdnDomain}/${str}"></script>`))
export default () => {
return defineConfig({
server: {
host: '0.0.0.0'
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src')
}
},
css: {
preprocessorOptions: {
scss: {
additionalData: `@use "@/assets/styles/element-variables.scss" as *;`
}
}
},
plugins: [
vue(),
Components({
dts: true,
resolvers: [ElementPlusResolver({
importStyle: 'sass'
})],
}),
ElementPlus({
useSource: true,
}),
// 打包產生 stats.html 解析圖表
visualizer({ open: true }),
// gzip 壓包
viteCompression({
verbose: true,
disable: false,
threshold: 10240,
algorithm: 'gzip',
ext: '.gz'
}),
// EJS 模板注入 html 功能
createHtmlPlugin({
minify: true,
inject: {
data: {
title: TITLE,
injectScriptCss: process.env.NODE_ENV === 'production' ? cdn : ''
}
}
})
],
build: {
minify: 'terser',
// 生產環境移除 Log
terserOptions: {
compress: {
drop_console: true,
drop_debugger: true
}
},
rollupOptions: {
output: {
// 指定打包 file name
chunkFileNames: 'static/js/[name]-[hash].js',
entryFileNames: 'static/js/[name]-[hash].js',
assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
},
// 全局引入時配置 CDN
// 略過打包項目
// external: ['vue-demi', 'pinia', 'vue', 'vue-router', 'element-plus'],
// plugins: [
// // 暴露參數供外部 CDN 連結調用
// externalGlobals({
// // vue-demi 由於 element-plue、pinia 都要調用此依賴項所以也得優先引入,否則會 Error
// ['vue-demi']: 'VueDemi',
// pinia: 'Pinia',
// vue: 'Vue',
// ['vue-router']: 'VueRouter',
// ['element-plus']: 'ElementPlus',
// })
// ]
}
}
})
}