-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
197 lines (193 loc) · 6.14 KB
/
Copy pathvite.config.js
File metadata and controls
197 lines (193 loc) · 6.14 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { execSync } from 'node:child_process'
import preact from '@preact/preset-vite'
import autoprefixer from 'autoprefixer'
import csso from 'postcss-csso'
import { defineConfig } from 'vite'
import { VitePWA } from 'vite-plugin-pwa'
import { soundPreloadPlugin } from './build-tools/sound-preloader.js'
// Build identity — shown in the build-info tooltip so the running build can be
// told apart from a newer deploy without opening devtools.
const buildCommit = (() => {
try {
return execSync('git rev-parse --short HEAD', { encoding: 'utf8' }).trim()
} catch {
return 'nogit'
}
})()
// UTC — builds run on CI, so a local timezone would be misleading
const buildTime = `${new Date().toISOString().slice(0, 16).replace('T', ' ')} UTC`
// FNV-1a — tiny, dependency-free 32-bit hash, good enough to spread short
// commit SHAs across the avatar ranges below without pulling in node:crypto.
const fnv1a = str => {
let hash = 0x811c9dc5
for (let i = 0; i < str.length; i++) {
hash ^= str.charCodeAt(i)
hash = Math.imul(hash, 0x01000193)
}
return hash >>> 0
}
// Derived from buildCommit (not random) so the same commit always produces
// the same avatar — two builds of the same source are then reproducible.
const buildAvatar = (() => {
const ranges = [
[0x1f32a, 0x1f50d],
[0x1f56f, 0x1f5fa],
[0x1f687, 0x1f6f3],
[0x1f300, 0x1f31f],
[0x1f330, 0x1f393],
[0x1f400, 0x1f4ff],
]
const hash = fnv1a(buildCommit)
// Split the hash into two independent halves so the range and the offset
// within it vary independently across commits.
const rangeIndex = (hash >>> 16) % ranges.length
const [start, end] = ranges[rangeIndex]
const offset = (hash & 0xffff) % (end - start + 1)
return String.fromCodePoint(start + offset)
})()
// https://vitejs.dev/config/
export default defineConfig({
define: {
__BUILD_AVATAR__: JSON.stringify(buildAvatar),
__BUILD_COMMIT__: JSON.stringify(buildCommit),
__BUILD_TIME__: JSON.stringify(buildTime),
},
plugins: [
preact(),
soundPreloadPlugin(),
VitePWA({
registerType: 'autoUpdate',
// src/app/register-sw.js registers via `virtual:pwa-register` instead of
// the plugin's injected registerSW.js — the injected script never reloads
// the page after a new build activates, which left deployed changes
// invisible until a manual cache-disabled refresh.
injectRegister: null,
includeAssets: [
'favicon.ico',
'apple-touch-icon.png',
'masked-icon.svg',
'icon-192x192.png',
'icon-512x512.png',
// Recursive: sounds live one level deeper than they used to. Events are
// directories of interchangeable takes (sounds/elapsed/006/*.webm), and
// break overtime is nested (sounds/overtime/break/*.webm) — a single-*
// glob silently precached neither, so those sounds never worked offline.
'sounds/**/*.webm',
'sounds/**/*.ogg',
],
workbox: {
// Clean up old caches from previous versions
cleanupOutdatedCaches: true,
// Skip waiting to activate new SW immediately
skipWaiting: true,
// Take control of all clients immediately
clientsClaim: true,
// Runtime caching for assets not in precache
runtimeCaching: [
{
// Cache audio files with StaleWhileRevalidate
urlPattern: /\.(?:webm|wav|mp3|ogg)$/i,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'audio-cache',
expiration: {
maxEntries: 50,
maxAgeSeconds: 60 * 60 * 24 * 30, // 30 days
},
},
},
{
// Images: served from cache, refreshed in the background. CacheFirst
// pinned unhashed icons for up to 30 days with no way to bust them.
urlPattern: /\.(?:png|jpg|jpeg|svg|gif|ico|webp)$/i,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'image-cache',
expiration: {
maxEntries: 30,
maxAgeSeconds: 60 * 60 * 24 * 30, // 30 days
},
},
},
{
// Cache fonts with CacheFirst
urlPattern: /^https:\/\/fonts\.bunny\.net\/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'font-cache',
expiration: {
maxEntries: 10,
maxAgeSeconds: 60 * 60 * 24 * 365, // 1 year
},
cacheableResponse: {
statuses: [0, 200],
},
},
},
],
},
manifest: {
name: 'Linear Pomodoro Timer',
short_name: 'Pomodoro',
description: 'A simple Pomodoro timer application',
theme_color: '#ffffff',
icons: [
{
src: 'icon-192x192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: 'icon-512x512.png',
sizes: '512x512',
type: 'image/png',
},
],
},
}),
],
test: {
globals: true,
environment: 'jsdom',
setupFiles: './src/test/setup.ts',
coverage: {
reporter: ['text', 'json', 'html'],
provider: 'v8',
},
},
server: {
host: true,
port: 5050,
strictPort: true,
cors: true,
},
base: '/tymer/',
build: {
rollupOptions: {
// Two pages: the app, and the sound-preview page at /tymer/sounds/.
// The second entry emits dist/sounds/index.html, which coexists with the
// audio files copied there from public/sounds/ — GitHub Pages then serves
// the directory URL with no SPA fallback.
input: {
main: 'index.html',
sounds: 'sounds/index.html',
},
},
},
css: {
preprocessorOptions: {
sass: {
// Add any Sass-specific options here if needed
},
},
postcss: {
plugins: [
autoprefixer(),
csso({
sourceMap: process.env.NODE_ENV !== 'production',
}),
],
},
devSourcemap: process.env.NODE_ENV !== 'production',
},
})