-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
71 lines (68 loc) · 1.93 KB
/
vite.config.ts
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
import react from '@vitejs/plugin-react';
import { defineConfig } from 'vite';
import { resolve } from 'path';
import { exec } from 'child_process';
import { formatDate } from '@tool-pack/basic';
import pkg from './package.json';
import data from './src/data';
const strf = JSON.stringify;
/**
* Vite configuration
* https://vitejs.dev/config/
*/
export default defineConfig(async (env) => {
const lastModified = await getLatestCommitTime('2024-03-12 00:00:00');
const isDev = env.mode === 'development';
return {
base: isDev ? undefined : pkg.name,
cacheDir: `./.cache`,
plugins: [
// https://github.com/vitejs/vite/tree/main/packages/plugin-react
react(),
],
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
},
extensions: ['.ts', '.tsx'],
},
css: {
// dev时生成sourcemap
devSourcemap: true,
},
// 环境变量配置
define: {
'import.meta.env.APP_LAST_MODIFIED': strf(lastModified),
'import.meta.env.APP_AUTHOR': strf(pkg.author),
'import.meta.env.APP_TITLE': strf(
`${data.info.name} - ${data.info.job}简历`,
),
'import.meta.env.APP_ENTRY': strf(
isDev ? 'src/index.dev.tsx' : 'src/index.tsx',
),
},
};
});
// 获取 git 最新的代码提交时间
export function getLatestCommitTime(defValue?: string): Promise<string> {
return new Promise((resolve, reject): void => {
exec('git log -1 --format=%cd', (error, stdout, stderr) => {
if ((error || stderr) && defValue) {
resolve(defValue);
return;
}
if (error) {
reject(error);
return;
}
if (stderr) {
reject(new Error(stderr));
return;
}
const commitTime = new Date(stdout.trim());
const offset = commitTime.getTimezoneOffset() / 60 + 8;
commitTime.setHours(commitTime.getHours() + offset);
resolve(formatDate(commitTime));
});
});
}