-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbundle-site-js.mjs
More file actions
97 lines (87 loc) · 2.45 KB
/
bundle-site-js.mjs
File metadata and controls
97 lines (87 loc) · 2.45 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
/**
* Bundles and minifies site JavaScript (same idea as bundle-site-css.mjs).
*
* Outputs:
* site.min.js — theme, a11y, nav, cookie consent, analytics, footer
* home.min.js — hero carousel, scroll (homepage only)
* ladybug.min.js — ladybug easter egg (homepage only, lazy-loaded)
* search-palette.min.js — search palette (after inline window.CFD_SEARCH)
* cfddc.min.js — CFDDC year pages only
*
* Run: npm run build:js
*/
import { readFileSync, writeFileSync } from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import { transformSync, buildSync } from 'esbuild';
const __dir = dirname(fileURLToPath(import.meta.url));
const jsDir = resolve(__dir, '../static/js');
const BUNDLES = [
{
out: 'site.min.js',
files: [
'theme.js',
'a11y.js',
'home-nav.js',
'cookie-consent.js',
'analytics.js',
'site-footer.js',
],
},
{
out: 'home.min.js',
files: ['home-hero.js', 'home-scroll.js'],
},
{
out: 'ladybug.min.js',
files: ['ladybug.js'],
},
{
out: 'search-palette.min.js',
files: ['search-palette.js'],
},
{
out: 'cfddc.min.js',
files: ['cfddc.js'],
},
];
let totalIn = 0;
let totalOut = 0;
for (const bundle of BUNDLES) {
const outPath = resolve(jsDir, bundle.out);
if (bundle.out === 'search-palette.min.js') {
const entryPath = resolve(jsDir, bundle.files[0]);
const source = readFileSync(entryPath, 'utf8');
const result = buildSync({
entryPoints: [entryPath],
bundle: true,
format: 'iife',
minify: true,
target: 'es2018',
write: false,
});
const code = result.outputFiles[0].text;
writeFileSync(outPath, code);
totalIn += source.length;
totalOut += code.length;
console.log(
`${bundle.files.join(' + ')} (bundled) → ${bundle.out} (${source.length} → ${code.length} bytes)`
);
continue;
}
const combined = bundle.files
.map((name) => readFileSync(resolve(jsDir, name), 'utf8'))
.join('\n');
const { code } = transformSync(combined, {
loader: 'js',
minify: true,
target: 'es2018',
});
writeFileSync(outPath, code);
totalIn += combined.length;
totalOut += code.length;
console.log(
`${bundle.files.join(' + ')} → ${bundle.out} (${combined.length} → ${code.length} bytes)`
);
}
console.log(`Wrote ${BUNDLES.length} bundles (${totalIn} → ${totalOut} bytes)`);