Repository navigation
Expand file tree
/
Copy pathvite.worker.config.js
More file actions
52 lines (49 loc) · 1.59 KB
/
Copy pathvite.worker.config.js
File metadata and controls
52 lines (49 loc) · 1.59 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
import { createHash } from 'node:crypto';
import { readdirSync, readFileSync } from 'node:fs';
import { join } from 'node:path';
import { defineConfig } from 'vite';
/**
* Bundles the Puter worker into one plain script. The worker runtime provides
* `router` and `me` as globals, so the output is an IIFE that uses them
* without importing anything.
*
* The bundle is written to public/worker/ and shipped as a static file. When
* a help desk owner turns on team access, their browser writes it to their
* Puter drive and deploys it from there (see src/client/deploy.js). The build
* id is a hash of the sources, so the app can tell when a deployed worker is
* out of date and quietly redeploy it.
*/
function sourceHash() {
const h = createHash('sha256');
for (const dir of ['worker', 'src/server', 'src/shared']) {
for (const f of readdirSync(dir).filter((x) => x.endsWith('.js')).sort()) {
h.update(`${dir}/${f}\n`);
h.update(readFileSync(join(dir, f)));
}
}
return h.digest('hex').slice(0, 12);
}
const build = sourceHash();
const builtAt = Date.now();
export default defineConfig({
publicDir: false,
define: {
__WORKER_BUILD__: JSON.stringify(build),
__WORKER_BUILT_AT__: JSON.stringify(builtAt),
},
build: {
target: 'es2022',
outDir: 'public/worker',
emptyOutDir: true,
minify: false,
lib: {
entry: 'worker/index.js',
formats: ['iife'],
name: 'helpDeskWorker',
fileName: () => 'help-desk-worker.js',
},
rollupOptions: {
output: { banner: `/* help-desk-worker build=${build} at=${builtAt} */` },
},
},
});