Repository navigation
Expand file tree
/
Copy pathbuild.mjs
More file actions
137 lines (119 loc) · 5.95 KB
/
Copy pathbuild.mjs
File metadata and controls
137 lines (119 loc) · 5.95 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
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
126
127
128
129
130
131
132
133
134
135
136
137
// Replaces the old gulpfile. Gulp 3 cannot run on Node 12+, and the project is
// on Node 22, so the previous build was simply dead.
//
// Same contract as before: read src/, write the bundles app/ that app.js serves.
//
// node build.mjs build once
// node build.mjs --watch rebuild on change
import { rm, mkdir, cp, readFile, writeFile } from 'node:fs/promises';
import { existsSync, watch } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { transform } from 'esbuild';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const SRC = path.join(__dirname, 'src');
const OUT = path.join(__dirname, 'app');
const NODE_MODULES = path.join(__dirname, 'node_modules');
// Baked into the client bundle at build time. Must be https:// -- the page is
// served over TLS, so http:// stream URLs get blocked as mixed content.
const STREAM_URL = process.env.STREAM_URL ?? 'https://stream.davidawindham.com/stream';
const STREAM_STATUS_URL =
process.env.STREAM_STATUS_URL ?? 'https://stream.davidawindham.com/status-json.xsl';
const STREAM_MOUNT = process.env.STREAM_MOUNT ?? '/stream';
const VENDOR_JS = [
path.join(SRC, 'js/jquery-v2.1.4.js'),
// Was src/js/bootstrap-v3.3.6.js. 3.4.1 patches the XSS fixed in 3.4.0/3.4.1
// (data-target, and the collapse/affix/tooltip selectors).
//
// It does NOT clear `npm audit`: two XSS advisories cover all of 3.1.1-3.4.1
// with no 3.x fix, because Bootstrap 3 went EOL in 2019. Both need
// attacker-controlled content in a popover/tooltip or a data-* attribute,
// and neither exists here -- the only tooltip is on static nav-tabs, and the
// one templated data-* is a literal. Clearing the advisory outright means
// Bootstrap 5, i.e. rewriting the markup. See _claude.md.
path.join(NODE_MODULES, 'bootstrap/dist/js/bootstrap.js'),
path.join(SRC, 'js/bootstrap-validator-v0.9.0.js'),
path.join(SRC, 'js/bootstrap-progress-v0.9.0.js'),
path.join(SRC, 'js/underscore-v1.8.3.js'),
// Was src/js/handlebars-v4.0.5.js. Handlebars < 4.7.7 has a prototype
// pollution RCE, so this now tracks the npm package instead of the checked-in
// copy. The template syntax used here is unchanged between the two.
path.join(NODE_MODULES, 'handlebars/dist/handlebars.min.js'),
];
const RADIO_JS = [
// base.js must come first: it defines window.RADIO, the shared socket and
// mount-path helper that the others depend on.
path.join(SRC, 'js/base.js'),
path.join(SRC, 'js/chat.js'),
path.join(SRC, 'js/amplitude-v2.2.0.js'),
path.join(SRC, 'js/radio.js'),
path.join(SRC, 'js/ham.js'),
];
// hls.js is emitted as its own file (app/js/hls.min.js) and loaded on demand by
// ham.js only when the-ham goes live — NOT bundled into radio.min.js, so the
// common case (nothing broadcasting) never pays its ~530KB.
const HLS_SRC = path.join(NODE_MODULES, 'hls.js/dist/hls.min.js');
// Bootstrap's CSS ships with its JS, so it tracks the same 3.4.1 package. Its
// glyphicon @font-face rules point at ../fonts/, which is what src/fonts/ is
// copied to (those files are byte-identical between 3.3.6 and 3.4.1).
// main.css comes last so it keeps overriding Bootstrap.
const CSS = [
path.join(NODE_MODULES, 'bootstrap/dist/css/bootstrap.css'),
path.join(SRC, 'css/main.css'),
];
const DEFINE = {
__STREAM_URL__: JSON.stringify(STREAM_URL),
__STREAM_STATUS_URL__: JSON.stringify(STREAM_STATUS_URL),
__STREAM_MOUNT__: JSON.stringify(STREAM_MOUNT),
};
// `separator` differs by language and it matters. JS files are joined with a
// semicolon so a file ending in an expression can't swallow the next file's
// leading paren (the classic IIFE concat hazard). CSS must NOT get one: a
// stray top-level ';' is a parse error, and the recovery rule makes the parser
// consume it as part of the *next* rule's selector -- silently dropping that
// rule. It ate main.css's first rule until this was fixed.
async function concat(files, separator) {
const parts = [];
for (const file of files) {
if (!existsSync(file)) throw new Error(`missing build input: ${path.relative(__dirname, file)}`);
parts.push(await readFile(file, 'utf8'));
}
return parts.join(separator);
}
async function bundleJs(files, outFile, { define } = {}) {
const source = await concat(files, '\n;\n');
// These are old-style global scripts, not modules -- minify only, no bundling
// or module resolution, or they'd lose their globals.
const result = await transform(source, { loader: 'js', minify: true, define });
await writeFile(path.join(OUT, outFile), result.code);
}
async function bundleCss(files, outFile) {
const source = await concat(files, '\n');
const result = await transform(source, { loader: 'css', minify: true });
await writeFile(path.join(OUT, outFile), result.code);
}
async function build() {
const started = Date.now();
await rm(OUT, { recursive: true, force: true });
await mkdir(path.join(OUT, 'js'), { recursive: true });
await mkdir(path.join(OUT, 'css'), { recursive: true });
await cp(path.join(SRC, 'index.html'), path.join(OUT, 'index.html'));
await cp(path.join(SRC, 'img'), path.join(OUT, 'img'), { recursive: true });
await cp(path.join(SRC, 'fonts'), path.join(OUT, 'fonts'), { recursive: true });
await cp(path.join(SRC, 'js/templates'), path.join(OUT, 'js/templates'), { recursive: true });
await bundleCss(CSS, 'css/style.min.css');
await bundleJs(VENDOR_JS, 'js/vendor.min.js');
await bundleJs(RADIO_JS, 'js/radio.min.js', { define: DEFINE });
// hls.js is already minified; copy as-is (lazy-loaded, not bundled).
await cp(HLS_SRC, path.join(OUT, 'js/hls.min.js'));
console.log(`BUILD: complete in ${Date.now() - started}ms -> ${path.relative(__dirname, OUT)}/`);
}
await build();
if (process.argv.includes('--watch')) {
let queued = null;
watch(SRC, { recursive: true }, () => {
clearTimeout(queued);
queued = setTimeout(() => build().catch((err) => console.error('BUILD FAILED:', err.message)), 100);
});
console.log('WATCH: watching src/');
}