Load only what the page uses
The loader is 2.0 KB. It reads the specs on the page, asks the core which blocks they need, and downloads just those files.
A chart through the loader
Nothing but loader.js is in the page. With data-auto it fetches manifest.json, the core and the blocks this chart needs, then mounts.
Files the loader fetched for the chart above:
waiting for the chart…
HTML
<script src="/release/loader.js" data-auto></script>
<script type="application/json" id="bc-data">
{"visits":[{"month":"2024-01-01","channel":"web","visits":200},{"month":"2024-02-01","channel":"web","visits":260},{"month":"2024-03-01","channel":"web","visits":310},{"month":"2024-04-01","channel":"web","visits":290},{"month":"2024-05-01","channel":"web","visits":350},{"month":"2024-06-01","channel":"web","visits":420},{"month":"2024-01-01","channel":"app","visits":120},{"month":"2024-02-01","channel":"app","visits":150},{"month":"2024-03-01","channel":"app","visits":210},{"month":"2024-04-01","channel":"app","visits":260},{"month":"2024-05-01","channel":"app","visits":280},{"month":"2024-06-01","channel":"app","visits":330}]}
</script>
<script type="application/json" data-bc-chart>
{
"data": "visits",
"size": [720, 260],
"scales": {
"x": { "type": "time" },
"y": { "type": "linear", "zero": true },
"color": { "type": "color" }
},
"guides": [
{ "type": "grid", "scale": "y" },
{ "type": "axis", "scale": "x" },
{ "type": "axis", "scale": "y", "title": "Visits" },
{ "type": "legend", "scale": "color", "position": "top" }
],
"marks": [{ "type": "line", "x": "month", "y": "visits", "color": "channel" }],
"interaction": [{ "type": "tooltip" }]
}
</script> By hand
Without data-auto you call it yourself, for example to add blocks that a chart made later will need:
JavaScript
await BCLoader.load({
base: 'https://cdn.jsdelivr.net/npm/blockcharts@0.0.5/release/', // default: the folder of loader.js
blocks: ['interaction.zoom'], // extra blocks besides the ones the page uses
scan: true, // read the specs on the page (default)
mount: true, // draw them when loaded (default)
integrity: true, // put the SRI hashes of manifest.json on the script tags
});