forked from haishanh/yacd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
84 lines (77 loc) · 1.96 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
72
73
74
75
76
77
78
79
80
81
82
83
84
import react from '@vitejs/plugin-react'
import * as path from 'path';
import { defineConfig } from 'vite';
import { VitePWA } from 'vite-plugin-pwa';
import * as pkg from './package.json';
// https://vitejs.dev/config/
export default defineConfig(async ({ mode }) => {
let hash = process.env.COMMIT_HASH;
if (!hash) {
try {
hash = await gitHash();
hash = hash.trim();
} catch (e) { }
}
if (!hash) hash = '';
console.log('commit hash', hash);
return {
define: {
__VERSION__: JSON.stringify(pkg.version),
__COMMIT_HASH__: JSON.stringify(hash),
'process.env.NODE_ENV': JSON.stringify(mode),
'process.env.PUBLIC_URL': JSON.stringify(''),
},
base: './',
resolve: {
alias: {
$src: path.resolve(__dirname, './src'),
src: path.resolve(__dirname, './src'),
},
},
publicDir: 'assets',
build: {
// sourcemap: true,
// the default value is 'dist'
// which make more sense
// but change this may break other people's tools
outDir: 'public',
},
plugins: [
react(),
VitePWA({
srcDir: 'src',
outDir: 'public',
filename: 'sw.ts',
strategies: 'injectManifest',
base: './',
}),
],
}
});
// non vite stuff
async function gitHash() {
try {
const mod = await import('node:child_process');
return await run(mod.spawn, 'git', ['rev-parse', '--short', 'HEAD']);
} catch (e) {
return;
}
}
function run(spawn: typeof import('node:child_process').spawn, cmd0: string, args0: string[]): Promise<string> {
const cmd = cmd0;
const args = args0;
return new Promise((resolve, reject) => {
const proc = spawn(cmd, args);
let out = Buffer.from('');
proc.stdout.on('data', (data) => {
out += data;
});
proc.on('error', (err) => {
reject(err);
});
proc.on('exit', (code) => {
if (code !== 0) reject(code);
resolve(out.toString());
});
});
}