davanstrien HF Staff commited on
Commit
0bbfe41
·
verified ·
1 Parent(s): 5f44be0

Add build script

Browse files
Files changed (1) hide show
  1. scripts/build.mjs +43 -0
scripts/build.mjs ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // No bundler: copy the app and the phonon-tts package into dist/, then write
2
+ // the service worker's precache list. phonon-tts is plain ES modules.
3
+ import { cpSync, rmSync, mkdirSync, readdirSync, statSync, readFileSync, writeFileSync, existsSync } from 'node:fs';
4
+ import { join, relative } from 'node:path';
5
+ import { createHash } from 'node:crypto';
6
+ import { execSync } from 'node:child_process';
7
+
8
+ const root = new URL('..', import.meta.url).pathname;
9
+ const dist = join(root, 'dist');
10
+ const pkg = join(root, 'node_modules', 'phonon-tts');
11
+ if (!existsSync(pkg)) execSync('npm install --no-audit --no-fund', { cwd: root, stdio: 'inherit' });
12
+
13
+ rmSync(dist, { recursive: true, force: true });
14
+ mkdirSync(dist);
15
+ cpSync(join(root, 'index.html'), join(dist, 'index.html'));
16
+ cpSync(join(root, 'src'), join(dist, 'src'), { recursive: true });
17
+ cpSync(join(root, 'public'), dist, { recursive: true });
18
+
19
+ const vendor = join(dist, 'vendor', 'phonon-tts');
20
+ mkdirSync(vendor, { recursive: true });
21
+ for (const f of readdirSync(pkg)) {
22
+ if (f.endsWith('.js') || f === 'wasm' || f === 'wasm-threads' || f.startsWith('LICENSE') || f === 'THIRD_PARTY_LICENSES.md') {
23
+ cpSync(join(pkg, f), join(vendor, f), { recursive: true, filter: (s) => !s.endsWith('.d.ts') });
24
+ }
25
+ }
26
+
27
+ const files = [];
28
+ const walk = (d) => readdirSync(d).forEach((n) => { const p = join(d, n); statSync(p).isDirectory() ? walk(p) : files.push(p); });
29
+ walk(dist);
30
+ const hash = createHash('sha256');
31
+ const urls = [];
32
+ for (const f of files.sort()) {
33
+ const rel = relative(dist, f).split('\\').join('/');
34
+ if (rel === 'sw.js' || rel.endsWith('.md') || rel.includes('LICENSE')) continue;
35
+ hash.update(rel).update(readFileSync(f));
36
+ urls.push('./' + rel);
37
+ }
38
+ urls.push('./');
39
+ const swPath = join(dist, 'sw.js');
40
+ writeFileSync(swPath, readFileSync(swPath, 'utf8')
41
+ .replace('__PRECACHE__', JSON.stringify(urls))
42
+ .replace('__VERSION__', hash.digest('hex').slice(0, 12)));
43
+ console.log(`built dist/ — precaching ${urls.length} files`);