Repository navigation
Expand file tree
/
Copy pathvite.config.js
More file actions
61 lines (59 loc) · 2.04 KB
/
Copy pathvite.config.js
File metadata and controls
61 lines (59 loc) · 2.04 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
import { createHash } from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { defineConfig } from 'vite';
import puterLocalhost from '../../dev/puter-localhost.mjs';
/**
* Emit the service worker with its precache list and a version baked in.
*
* The list has to be generated: the built asset names are hashed, so a
* hand-written service worker cannot know them. Baking them in also means
* sw.js changes bytes on every build that changes an asset, which is what
* makes the browser notice an update at all — a static service worker
* would keep serving the old bundle forever.
*/
function serviceWorker() {
return {
name: 'assetdesk-service-worker',
apply: 'build',
async generateBundle(_options, bundle) {
const built = Object.keys(bundle)
.filter((name) => !name.endsWith('.map'))
.map((name) => `./${name}`);
// The shell, the built assets, and the files the manifest points at
// (copied from public/, so they are not in the bundle).
const precache = [
'./',
'./index.html',
'./manifest.json',
'./icons/icon-192.png',
'./icons/icon-512.png',
'./icons/icon-maskable-512.png',
'./icons/apple-touch-icon.png',
// The tab icons index.html links to: without them an offline open
// showed the browser's blank placeholder in the tab.
'./icons/favicon-32.png',
'./icons/favicon-16.png',
...built,
];
const template = await readFile(new URL('./src/sw.js', import.meta.url), 'utf8');
const version = createHash('sha256').update(precache.join('|')).digest('hex').slice(0, 12);
this.emitFile({
type: 'asset',
fileName: 'sw.js',
source: template
.replace('__PRECACHE__', JSON.stringify(precache, null, 2))
.replaceAll('__VERSION__', version),
});
},
};
}
export default defineConfig({
base: './',
build: {
target: 'es2022',
},
plugins: [puterLocalhost('assets'), serviceWorker()],
test: {
environment: 'node',
},
});