forked from YuehaiTeam/cocogoat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.mjs
130 lines (129 loc) · 4.97 KB
/
vite.config.mjs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { defineConfig, splitVendorChunkPlugin, loadEnv } from 'vite'
import { createHtmlPlugin } from 'vite-plugin-html'
import ifdefPlugin from './scripts/vite-ifdef'
import importExternal from './scripts/vite-external'
import { viteWorkerMacro } from './scripts/workermacro'
import ignoreNolocal from './scripts/vite-ignore-nolocal'
import gzipAssets from './scripts/vite-gzip-assets'
import inlineFavicon from './scripts/vite-inline-favicon'
import { visualizer } from 'rollup-plugin-visualizer'
import { viteSingleFile } from 'vite-plugin-singlefile'
import { singleFileHTML } from './scripts/vite-singlefile'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import checker from 'vite-plugin-checker'
import vue from '@vitejs/plugin-vue'
import GitInfo from 'git-repo-info'
import { resolve } from 'path'
import dayjs from 'dayjs'
const gitInfo = GitInfo()
const isCI = !!process.env.SENTRY_KEY
const useCDN = process.argv.includes('--cdn') || isCI
const singleFile = process.argv.includes('--singlefile')
const isTest = process.argv.includes('--test') || process.env.CF_PAGES === '1'
const useSentry =
!process.argv.includes('--no-sentry') && process.env.NODE_ENV === 'production' && !!process.env.SENTRY_KEY
process.env.VUE_APP_BUILD = dayjs().format('YYMMDDHHmm')
process.env.VUE_APP_TEST = isTest ? 'true' : 'false'
process.env.VUE_APP_ROUTER_HASH = singleFile ? 'true' : 'false'
process.env.VUE_APP_SINGLEFILE = singleFile ? 'true' : 'false'
process.env.VUE_APP_LOCALRES = singleFile || process.env.NODE_ENV === 'development' ? 'true' : 'false'
process.env.VUE_APP_TIMESTAMP = Date.now().toString()
process.env.VUE_APP_GIT_SHA = (gitInfo.abbreviatedSha || '').substring(0, 8)
process.env.VUE_APP_GIT_BRC = gitInfo.branch || 'unknown'
process.env.VUE_APP_GIT_MSG =
((gitInfo.commitMessage || '').split('-----END PGP SIGNATURE-----')[1] || '').trim() || gitInfo.commitMessage || ''
console.log(
`[cocogoat-web][vite] Build ${process.env.NODE_ENV} ${process.env.VUE_APP_GIT_BRC}#${process.env.VUE_APP_GIT_SHA}/${process.env.VUE_APP_BUILD}`,
)
console.log(`SingleFile: ${singleFile}, CDN: ${useCDN}, Sentry: ${useSentry}\n`)
console.log(process.env.VUE_APP_GIT_MSG)
console.log('')
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, __dirname, 'VUE_APP_')
const base = singleFile ? './' : process.env.NODE_ENV === 'production' && useCDN ? 'https://gi.cocogoat.cn/' : '/'
return {
base: base,
define: Object.keys(env).reduce(
(acc, key) => {
acc[`process.env.${key}`] = JSON.stringify(env[key])
return acc
},
{
'process.env.BASE_URL': JSON.stringify(base),
},
),
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
lodash: 'lodash-es',
},
},
build: {
reportCompressedSize: false,
},
esbuild: {
charset: 'utf8',
legalComments: 'external',
},
css: {
modules: {
localsConvention: 'camelCaseOnly',
generateScopedName: '[local]-[hash:6]',
},
},
plugins: [
checker({
typescript: true,
vueTsc: true,
eslint:
command === 'build'
? undefined
: {
lintCommand: 'eslint ./**/*.{js,ts,vue} --ext .js,.ts,.vue',
},
}),
vue(),
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
dirs: [],
resolvers: [ElementPlusResolver()],
}),
viteWorkerMacro(command === 'serve' ? 'vite-dev' : singleFile ? 'vite-singlefile' : 'vite'),
ifdefPlugin({
option: { verbose: false },
data: {
SINGLEFILE: singleFile,
VITE: true,
WEBPACK: false,
VITE_DEV: command === 'serve',
},
}),
importExternal({
'monaco-editor': 'monaco',
}),
visualizer({
emitFile: true,
filename: 'dist/report.html',
}),
createHtmlPlugin({
minify: true,
entry: 'src/main.ts',
}),
...(singleFile
? [
ignoreNolocal(),
viteSingleFile({
removeViteModuleLoader: true,
}),
gzipAssets(),
singleFileHTML(),
inlineFavicon(),
]
: [splitVendorChunkPlugin()]),
],
}
})