JavaScript Bundling#
Most applications should use Interactive HTML Output to produce a complete interactive page. Use the bundling API when you are building your own web integration and need one JavaScript file containing compatible versions of Vega, Vega-Lite, and Vega Embed.
Get the Standard Bundle#
use vl_convert_rs::{VlConverter, VlVersion};
let converter = VlConverter::new();
let bundle = converter
.get_vegaembed_bundle(VlVersion::v6_4)
.await?;
std::fs::write("vega-embed.js", bundle)?;
The standard bundle exposes vega, vegaLite, and vegaEmbed on window, so browser code can use them without module imports. Select the same Vega-Lite version that the browser will render.
Add an Application Snippet#
A snippet is bundled in the same module scope, so it can refer to vega, vegaLite, and vegaEmbed. It must not import anything outside the dependencies bundled with VlConvert.
For example, save this as snippet.js:
snippet.js
window.renderVegaLite = (element, spec, options = {}) =>
vegaEmbed(element, spec, options)
let bundle = converter
.bundle_vega_snippet(
include_str!("../snippet.js"),
VlVersion::v6_4,
)
.await?;
std::fs::write("app-chart.js", bundle)?;
The resulting app-chart.js defines the wrapper. This complete page loads the bundle and calls it with a Vega-Lite specification. Save it as index.html beside app-chart.js, then open it in a browser:
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>VlConvert bundle example</title>
</head>
<body>
<div id="vis"></div>
<script src="app-chart.js"></script>
<script>
const spec = {
"$schema": "https://vega.github.io/schema/vega-lite/v6.json",
"data": {"values": [{"category": "A", "value": 28}, {"category": "B", "value": 55}]},
"mark": "bar",
"encoding": {
"x": {"field": "category", "type": "nominal"},
"y": {"field": "value", "type": "quantitative"}
}
};
window.renderVegaLite("#vis", spec);
</script>
</body>
</html>
Bundling runs build tooling on caller-supplied source. Apply authentication, body-size limits, and request budgets before exposing the server endpoints to other users.