-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild
executable file
·125 lines (108 loc) · 4.03 KB
/
build
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
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env node
'use strict';
const child_process = require('child_process');
const crypto = require('crypto');
const fs = require('fs');
const path = require('path');
const Terser = require('terser');
const canonical = process.argv[2]; // e.g. 'https://scheibo.github.io/boggle/'
const root = process.argv[3] || '/'; // e.g. '/boggle/'
const shell = cmd => child_process.execSync(cmd, {stdio: 'inherit', cwd: __dirname});
const hash = files => {
const h = crypto.createHash('md5');
for (const f of files) {
h.update(fs.readFileSync(path.join(__dirname, f)));
}
return h.digest('hex').substr(0, 8);
}
try {
require.resolve('./data/freqs.txt');
} catch (e) {
shell('./tools/fix.js');
}
try {
require.resolve('./data/dict.json');
require.resolve('./data/stats.json');
} catch (e) {
shell('./tools/dict.js');
}
shell('npx parcel build --public-url ./ index.html');
const DATA = ['/data/dict.json', '/data/stats.json'];
const dist = path.join(__dirname, 'dist');
const distdata = path.join(dist, 'data');
// Rewrite the compiled index.html to fix the sw.js location
const META = '<meta name="description" content="Boggle">';
const SCRIPT = '<script type="application/javascript"';
const index = path.join(dist, 'index.html');
let contents = fs.readFileSync(index, 'utf8');
if (canonical) contents = contents.replace(META, `${META}<link rel="canonical" href="${canonical}" />`);
contents = contents.replace(SCRIPT, `<script>"serviceWorker"in navigator&&window.addEventListener("load",function(){return navigator.serviceWorker.register("${root}sw.js",{scope: "${root}"})});</script> ${SCRIPT}`);
fs.writeFileSync(index, contents);
// All the files parcel created, excluding images
const FILES = fs.readdirSync(dist).filter(f => f !== 'data' && !f.endsWith('.png'));
// Mirror the data directory to dist
if (!fs.existsSync(distdata)) fs.mkdirSync(distdata);
for (const file of DATA) {
fs.copyFileSync(path.join(__dirname, file), path.join(dist, file));
}
(async () => {
const NAMESPACE = 'boggle';
const {error, code, map} = await Terser.minify(`'use strict';
const version = '${hash(FILES.map(f => path.join('/dist', f)))}';
const CACHE = '${NAMESPACE}:cache:' + version;
const DATA = '${NAMESPACE}:data:${hash(DATA)}';
self.addEventListener('install', e => {
console.log('[ServiceWorker:'+version+'] installed');
e.waitUntil(
caches.open(CACHE).then(cache => {
console.log('[ServiceWorker:'+version+'] cache.addAll', ${JSON.stringify(FILES)});
return cache.addAll(${JSON.stringify(FILES)});
}).then(self.skipWaiting()));
});
self.addEventListener('activate', e => {
console.log('[ServiceWorker:'+version+'] activate');
e.waitUntil(
caches.keys().then(keys =>
Promise.all(keys.map(key => {
if (key.startsWith('${NAMESPACE}') && ![CACHE, DATA].includes(key)) {
console.log('[ServiceWorker:'+version+'] deleting', key);
return caches.delete(key);
}
}))))
});
const writeBack = (e, which) =>
caches.open(which).then(async cache => {
const cached = await cache.match(e.request);
if (cached) {
console.log('[ServiceWorker:'+version+'] returning cached result for', e.request.url);
return cached;
}
console.log('[ServiceWorker:'+version+'] fetching', e.request.url);
const response = await fetch(e.request);
if (response.status === 200) {
console.log('[ServiceWorker:'+version+'] caching', e.request.url, 'to', which);
cache.put(e.request.url, response.clone());
}
return response;
});
self.addEventListener('fetch', e => {
if (e.request.url.includes('/data/dict.json') ||
e.request.url.includes('/data/stats.json')) {
e.respondWith(writeBack(e, DATA));
} else {
e.respondWith(writeBack(e, CACHE));
}
});
`, {
sourceMap: {
filename: 'sw.js',
url: 'sw.js.map',
}
});
if (error) {
console.error(error);
process.exit(1);
}
fs.writeFileSync(path.join(dist, 'sw.js'), code);
fs.writeFileSync(path.join(dist, 'sw.js.map'), map);
})();