ASCEND / build.mjs
mmrech's picture
v2.0.0: all-economy World lens with LCoGS and SDG 3.8 indicators, choropleth globe, build pipeline and tests
c140718 verified
Raw History Blame Contribute Delete
1.19 kB
#!/usr/bin/env node
/** Build the standalone index.html from source.html + style.css + data.json + bundled modules. */
import {readFileSync,writeFileSync,statSync} from 'node:fs';
import {build} from 'esbuild';
const data=readFileSync('data.json','utf8');
JSON.parse(data); // fail fast on an invalid snapshot
const result=await build({entryPoints:['app.mjs'],bundle:true,format:'iife',minify:true,target:['es2020'],write:false,legalComments:'none',logLevel:'warning'});
const bundle=result.outputFiles[0].text;
let html=readFileSync('source.html','utf8');
const css=readFileSync('style.css','utf8');
html=html.replace('<link rel="stylesheet" href="style.css">',()=>`<style>${css}</style>`);
html=html.replace('<script type="module" src="app.mjs"></script>',()=>`<script>window.ASCEND_DATA=${data.replace(/<\/script/gi,'<\\/script')};</script>\n<script>${bundle}</script>`);
if(!html.includes('window.ASCEND_DATA=')||!html.includes('<style>'))throw new Error('inlining failed');
writeFileSync('index.html',html);
console.log(`index.html ${(statSync('index.html').size/1024).toFixed(0)} kB · bundle ${(bundle.length/1024).toFixed(0)} kB · data ${(data.length/1024).toFixed(0)} kB`);