Skip to content

Commit 55576dd

Browse files
committed
chore: organizing vite configuration files
1 parent 602144d commit 55576dd

File tree

19 files changed

+251
-393
lines changed

19 files changed

+251
-393
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"cSpell.language": "en,en-US",
9797
"cSpell.words": [
9898
"antfu",
99+
"brotli",
99100
"browserslist",
100101
"coderwyd",
101102
"commitlint",

build/constant.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

build/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './plugins'
2+
export * from './utils'

build/plugins/compress.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import compressPlugin from 'vite-plugin-compression'
2+
import type { PluginOption } from 'vite'
3+
4+
export function configCompressPlugin({
5+
compress,
6+
deleteOriginFile = false,
7+
}: {
8+
compress: string
9+
deleteOriginFile?: boolean
10+
}): PluginOption[] {
11+
const compressList = compress.split(',')
12+
13+
const plugins: PluginOption[] = []
14+
15+
if (compressList.includes('gzip')) {
16+
plugins.push(
17+
compressPlugin({
18+
ext: '.gz',
19+
deleteOriginFile,
20+
}),
21+
)
22+
}
23+
24+
if (compressList.includes('brotli')) {
25+
plugins.push(
26+
compressPlugin({
27+
ext: '.br',
28+
algorithm: 'brotliCompress',
29+
deleteOriginFile,
30+
}),
31+
)
32+
}
33+
return plugins
34+
}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
import { createHtmlPlugin } from 'vite-plugin-html'
22
import type { PluginOption } from 'vite'
33

4-
export function configHtmlPlugin(env: ViteEnv, isBuild: boolean) {
5-
const { VITE_GLOB_APP_TITLE } = env
6-
4+
export function configHtmlPlugin({ isBuild }: { isBuild: boolean }) {
75
const htmlPlugin: PluginOption[] = createHtmlPlugin({
86
minify: isBuild,
9-
inject: {
10-
data: {
11-
title: VITE_GLOB_APP_TITLE,
12-
},
13-
},
147
})
158
return htmlPlugin
169
}
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import vue from '@vitejs/plugin-vue'
22
import vueJsx from '@vitejs/plugin-vue-jsx'
3-
import UnoCSS from 'unocss/vite'
4-
import VueDevTools from 'vite-plugin-vue-devtools'
5-
3+
import unocss from 'unocss/vite'
4+
import vueDevTools from 'vite-plugin-vue-devtools'
65
import { configCompressPlugin } from './compress'
76
import { configHtmlPlugin } from './html'
87
import { viteBuildInfo } from './info'
9-
108
import unplugin from './unplugin'
119
import type { PluginOption } from 'vite'
1210

@@ -16,20 +14,20 @@ export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) {
1614
} = viteEnv
1715

1816
const vitePlugins: (PluginOption | PluginOption[])[] = [
19-
VueDevTools(),
2017
vue(),
2118
vueJsx(),
22-
UnoCSS(),
19+
vueDevTools(),
20+
unocss(),
2321
...unplugin,
2422
]
2523

2624
// vite-plugin-html
27-
vitePlugins.push(configHtmlPlugin(viteEnv, isBuild))
25+
vitePlugins.push(configHtmlPlugin({ isBuild }))
2826

2927
// The following plugins only work in the production environment
3028
if (isBuild) {
3129
// rollup-plugin-gzip
32-
vitePlugins.push(configCompressPlugin(VITE_BUILD_COMPRESS))
30+
vitePlugins.push(configCompressPlugin({ compress: VITE_BUILD_COMPRESS }))
3331
}
3432

3533
// build info

build/plugins/info.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import dayjs from 'dayjs'
2+
import duration from 'dayjs/plugin/duration'
3+
import { bold, green } from 'picocolors'
4+
import { getPackageSize } from '../utils'
5+
import type { Plugin } from 'vite'
6+
import type { Dayjs } from 'dayjs'
7+
8+
dayjs.extend(duration)
9+
10+
export function viteBuildInfo(): Plugin {
11+
let startTime: Dayjs
12+
let endTime: Dayjs
13+
let outDir: string
14+
let config: { command: string }
15+
return {
16+
name: 'vite:buildInfo',
17+
configResolved(resolvedConfig) {
18+
config = resolvedConfig
19+
outDir = resolvedConfig.build?.outDir ?? 'dist'
20+
},
21+
buildStart() {
22+
if (config.command === 'build')
23+
startTime = dayjs(new Date())
24+
},
25+
closeBundle() {
26+
if (config.command === 'build') {
27+
endTime = dayjs(new Date())
28+
getPackageSize(outDir, (size: string) => {
29+
console.log(
30+
bold(
31+
green(
32+
`🎉恭喜打包完成(总用时${dayjs
33+
.duration(endTime.diff(startTime))
34+
.format('mm分ss秒')},打包后的大小为${
35+
size
36+
})`,
37+
),
38+
),
39+
)
40+
})
41+
}
42+
},
43+
}
44+
}

build/utils.ts

Lines changed: 0 additions & 77 deletions
This file was deleted.

build/utils/index.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { readdir, stat } from 'node:fs'
2+
import process from 'node:process'
3+
import path from 'node:path'
4+
5+
// Read all environment variable configuration files to process.env
6+
export function wrapperEnv(envConf: Recordable): ViteEnv {
7+
const ret: any = {}
8+
9+
for (const envName of Object.keys(envConf)) {
10+
let realName = envConf[envName].replace(/\\n/g, '\n')
11+
realName = realName === 'true' ? true : realName === 'false' ? false : realName
12+
13+
if (envName === 'VITE_PORT')
14+
realName = Number(realName)
15+
16+
ret[envName] = realName
17+
if (typeof realName === 'string')
18+
process.env[envName] = realName
19+
else if (typeof realName === 'object')
20+
process.env[envName] = JSON.stringify(realName)
21+
}
22+
return ret
23+
}
24+
25+
function sum(arr: number[]) {
26+
return arr.reduce((t: number, c: number) => {
27+
return t + c
28+
}
29+
, 0)
30+
}
31+
32+
function formatBytes(bytes: number, decimals = 2) {
33+
if (!+bytes)
34+
return '0 Bytes'
35+
36+
const k = 1024
37+
const dm = decimals < 0 ? 0 : decimals
38+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
39+
40+
const i = Math.floor(Math.log(bytes) / Math.log(k))
41+
42+
return `${Number.parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`
43+
}
44+
45+
// eslint-disable-next-line @typescript-eslint/ban-types
46+
export function getPackageSize(folder: string, callback: Function, fileListTotal: number[] = []): void {
47+
readdir(folder, (err, files: string[]) => {
48+
if (err)
49+
throw err
50+
let count = 0
51+
const checkEnd = () => {
52+
++count === files.length && callback(formatBytes(sum(fileListTotal)))
53+
}
54+
files.forEach((item: string) => {
55+
stat(`${folder}/${item}`, async (err, stats) => {
56+
if (err)
57+
throw err
58+
if (stats.isFile()) {
59+
fileListTotal.push(stats.size)
60+
checkEnd()
61+
}
62+
else if (stats.isDirectory()) {
63+
getPackageSize(`${folder}/${item}/`, checkEnd, fileListTotal)
64+
}
65+
})
66+
})
67+
files.length === 0 && callback()
68+
})
69+
}
70+
71+
/**
72+
* 获取项目根路径
73+
* @descrition 末尾不带斜杠
74+
*/
75+
export function getRootPath() {
76+
return path.resolve(process.cwd())
77+
}
78+
79+
/**
80+
* 获取项目src路径
81+
* @param srcName - src目录名称(默认: "src")
82+
* @descrition 末尾不带斜杠
83+
*/
84+
export function getSrcPath(srcName = 'src') {
85+
const rootPath = getRootPath()
86+
87+
return `${rootPath}/${srcName}`
88+
}

0 commit comments

Comments
 (0)