File size: 2,464 Bytes
0bbfe41
 
 
 
 
 
ce38ee1
0bbfe41
 
 
 
44acf59
0bbfe41
 
 
 
 
 
ce38ee1
0bbfe41
 
 
 
 
 
 
 
 
44acf59
 
 
 
 
0bbfe41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// No bundler: copy the app and the phonon-tts package into dist/, then write
// the service worker's precache list. phonon-tts is plain ES modules.
import { cpSync, rmSync, mkdirSync, readdirSync, statSync, readFileSync, writeFileSync, existsSync } from 'node:fs';
import { join, relative } from 'node:path';
import { createHash } from 'node:crypto';
import { execSync } from 'node:child_process';
import { writeIcons } from './icons.mjs';

const root = new URL('..', import.meta.url).pathname;
const dist = join(root, 'dist');
const pkg = join(root, 'node_modules', 'phonon-tts');
if (!existsSync(pkg) || !existsSync(join(root, 'node_modules', '@breezystack', 'lamejs'))) execSync('npm install --no-audit --no-fund', { cwd: root, stdio: 'inherit' });

rmSync(dist, { recursive: true, force: true });
mkdirSync(dist);
cpSync(join(root, 'index.html'), join(dist, 'index.html'));
cpSync(join(root, 'src'), join(dist, 'src'), { recursive: true });
cpSync(join(root, 'public'), dist, { recursive: true });
writeIcons(join(dist, 'icons'));

const vendor = join(dist, 'vendor', 'phonon-tts');
mkdirSync(vendor, { recursive: true });
for (const f of readdirSync(pkg)) {
  if (f.endsWith('.js') || f === 'wasm' || f === 'wasm-threads' || f.startsWith('LICENSE') || f === 'THIRD_PARTY_LICENSES.md') {
    cpSync(join(pkg, f), join(vendor, f), { recursive: true, filter: (s) => !s.endsWith('.d.ts') });
  }
}

const lame = join(root, 'node_modules', '@breezystack', 'lamejs');
mkdirSync(join(dist, 'vendor', 'lamejs'), { recursive: true });
cpSync(join(lame, 'dist', 'lamejs.js'), join(dist, 'vendor', 'lamejs', 'lamejs.js'));
if (existsSync(join(lame, 'LICENSE'))) cpSync(join(lame, 'LICENSE'), join(dist, 'vendor', 'lamejs', 'LICENSE'));

const files = [];
const walk = (d) => readdirSync(d).forEach((n) => { const p = join(d, n); statSync(p).isDirectory() ? walk(p) : files.push(p); });
walk(dist);
const hash = createHash('sha256');
const urls = [];
for (const f of files.sort()) {
  const rel = relative(dist, f).split('\\').join('/');
  if (rel === 'sw.js' || rel.endsWith('.md') || rel.includes('LICENSE')) continue;
  hash.update(rel).update(readFileSync(f));
  urls.push('./' + rel);
}
urls.push('./');
const swPath = join(dist, 'sw.js');
writeFileSync(swPath, readFileSync(swPath, 'utf8')
  .replace('__PRECACHE__', JSON.stringify(urls))
  .replace('__VERSION__', hash.digest('hex').slice(0, 12)));
console.log(`built dist/ — precaching ${urls.length} files`);