-
Notifications
You must be signed in to change notification settings - Fork 23
/
vite.config.ts
114 lines (103 loc) · 2.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
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
import * as child_process from 'node:child_process'
import { join } from 'node:path'
import { promisify } from 'node:util'
import react from '@vitejs/plugin-react'
import unocss from 'unocss/vite'
import { defineConfig, splitVendorChunkPlugin } from 'vite'
import { VitePWA as pwa } from 'vite-plugin-pwa'
import { description, name, version } from './package.json'
interface ImportMetaEnvDefinition {
[key: `import.meta.env.${string}`]: string | boolean
}
const importMetaEnv: ImportMetaEnvDefinition = {
'import.meta.env.NEVER': false,
}
const exec = promisify(child_process.exec)
const getCommitHash = async () => {
const { stdout } = await exec('git rev-parse --short HEAD')
return stdout.trim()
}
const getCommitDate = async () => {
const { stdout } = await exec('git log -1 --format=%cd')
return new Date(stdout).toISOString()
}
const globalConstants = {
__VERSION__: JSON.stringify(version),
__COMMIT_HASH__: getCommitHash().then((hash) => JSON.stringify(hash)),
__COMMIT_DATE__: getCommitDate().then((date) => JSON.stringify(date)),
}
export default defineConfig(async () => ({
base: './',
build: {
chunkSizeWarningLimit: 1000,
},
define: {
...importMetaEnv,
...(await forkJoin(globalConstants)),
},
plugins: [
unocss(),
react(),
splitVendorChunkPlugin(),
pwa({
manifestFilename: 'app.webmanifest',
registerType: 'prompt',
includeAssets: ['favicon.ico', 'apple-touch-icon.png'],
manifest: {
name: 'Assembler Simulator',
short_name: 'AssemblerSimulator',
description,
id: `/${name}/`,
theme_color: '#ffffff',
icons: [
{
src: 'pwa-192x192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: 'pwa-512x512.png',
sizes: '512x512',
type: 'image/png',
},
],
},
workbox: {
runtimeCaching: [
{
urlPattern: ({ url }) => url.origin === 'https://fonts.gstatic.com',
handler: 'CacheFirst',
options: {
cacheName: 'google-fonts-webfonts',
cacheableResponse: {
statuses: [0, 200],
},
expiration: {
maxAgeSeconds: 60 * 60 * 24 * 365,
maxEntries: 10,
},
},
},
],
},
}),
],
resolve: {
alias: {
'@': join(__dirname, 'src'),
},
},
server: {
watch: {
ignored: [/coverage/, /dist/],
},
},
}))
type Joined<T extends Record<string, unknown>> = {
[K in keyof T]: T[K] extends Promise<infer U> ? U : T[K]
}
async function forkJoin<T extends Record<string, unknown>>(promises: T): Promise<Joined<T>> {
const keys = Object.keys(promises)
const values = await Promise.all(Object.values(promises))
return Object.fromEntries(keys.map((key, index) => [key, values[index]])) as Joined<T>
}