-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvite.config.js
More file actions
113 lines (103 loc) · 3.31 KB
/
Copy pathvite.config.js
File metadata and controls
113 lines (103 loc) · 3.31 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
import { defineConfig } from 'vite'
import laravel from 'laravel-vite-plugin'
import { VitePWA } from 'vite-plugin-pwa'
import { copyFile, access, unlink, readFile, writeFile } from 'node:fs/promises'
import { resolve } from 'node:path'
function minifyHtml(html) {
return html
.replace(/<!--[\s\S]*?-->/g, '')
.replace(/>\s+</g, '><')
.replace(/\s{2,}/g, ' ')
.trim()
}
function generateOfflineHtml() {
return {
name: 'kurozora:offline-html',
apply: 'build',
async closeBundle() {
const manifestPath = resolve('public/build/manifest.json')
const sourcePath = resolve('resources/offline.html')
const outputPath = resolve('public/offline.html')
try {
await access(manifestPath)
} catch {
return
}
const manifest = JSON.parse(await readFile(manifestPath, 'utf8'))
const appCss = manifest['resources/css/app.css']?.file
if (!appCss) return
const source = await readFile(sourcePath, 'utf8')
const output = minifyHtml(source.replaceAll('__APP_CSS__', `/build/${appCss}`))
await writeFile(outputPath, output)
},
}
}
function relocateServiceWorker() {
const files = ['service-worker.js', 'service-worker.js.map']
return {
name: 'kurozora:relocate-sw',
apply: 'build',
enforce: 'post',
async closeBundle() {
for (const name of files) {
const from = resolve('public/build', name)
const to = resolve('public', name)
try {
await access(from)
} catch {
continue
}
await copyFile(from, to)
await unlink(from)
}
},
}
}
export default defineConfig({
plugins: [
laravel({
input: [
'resources/css/app.css',
'resources/css/chat.css',
'resources/css/watch.css',
'resources/js/app.js',
'resources/js/chat.js',
'resources/js/db.js',
'resources/js/debug.js',
'resources/js/gif.js',
'resources/js/history.js',
'resources/js/listen.js',
'resources/js/markdown.js',
'resources/js/settings.js',
'resources/js/watch.js',
'resources/js/worker.js',
],
refresh: true,
}),
generateOfflineHtml(),
VitePWA({
strategies: 'injectManifest',
srcDir: 'resources/js',
filename: 'service-worker.js',
injectRegister: false,
manifest: false,
injectManifest: {
globDirectory: 'public',
globPatterns: [
'offline.html',
'images/static/icon/no_signal.webp',
'build/assets/*.css',
],
maximumFileSizeToCacheInBytes: 5 * 1024 * 1024,
},
devOptions: {
enabled: false,
type: 'module',
},
}),
relocateServiceWorker(),
],
build: {
sourcemap: true,
},
})