Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add minify configuration to BundleOptions #141

Merged
merged 3 commits into from
Oct 23, 2023

Conversation

jonmmease
Copy link
Contributor

This PR adds a minify option to BundleOptions and passes the option through to swc::codegen::Config.

Partially based on #63

Thanks!

@CLAassistant
Copy link

CLAassistant commented Oct 1, 2023

CLA assistant check
All committers have signed the CLA.

jonmmease added a commit to vega/vl-convert that referenced this pull request Oct 5, 2023
Closes #33, cc @joelostblom

This PR adds support for converting Vega and Vega-Lite charts to live HTML documents. There is a "bundle" option that controls whether the JavaScript dependencies should be loaded from a CDN, or whether they should be inlined into the resulting HTML file. 

### bundle=False
When bundle is False, this follows the [Vega Embed directions](https://vega.github.io/vega-lite/usage/embed.html#start-using-vega-lite-with-vega-embed) to load vega, vega-lite, and vega-embed from jsdelivr 

### bundle=True
When bundle is True, things are a bit more involved. We already inline Vega and several versions of Vega-Lite into the VlConvert executables, so I wanted to avoid including additional copies for the purpose of HTML export.  But in order to use the JS deps inlined into VlConvert, they need to be bundled.  I found that the [deno_emit](https://github.com/denoland/deno_emit) project provides a Rust crate that uses SWC to bundle JavaScript / TypeScript dependencies, and this ended up working well.

One note, the bundled code isn't fully minimized yet, but I have an open PR that will expose SWC's minify option. See denoland/deno_emit#141. Currently, the resulting HTML files start at ~1.6MB, but this will drop to ~1MB when minification is enabled.

The bundling process takes about 1.5s on my machine. Because this is pretty slow, I decided to cache the bundle results, so that subsequent HTML exports that use the same Vega-Lite version will be fast (10-20ms).

### Custom JavaScript bundles
Additionally, a `javascipt_bundle` Python function is added that can be used to create bundles with custom JavaScript logic that references Vega Embed, Vega, and Vega-Lite.  The idea is that Vega-based systems like Altair can use this to build additional integrations.

### Integrations
The most immediate application of the HTML export is to remove the altair_viewer dependency in Altair's html export when `inline=True`.
 
We could also use this to add an "html-offline" Altair renderer, though this could result in large notebooks as every individual Chart would be over 1MB.

Another use-case I have in mind is to use the `javascript_bundle` function to create offline bundles for Altair's JupyterChart. This is why I added support for the lodash debounce function as well, since this is the only import, in addition to vegaEmbed, that JupyterChart's JavaScript logic uses.  The cool thing about this approach is that we can build the offline bundle on the fly (in under 2s) without an internet connection required.
@jonmmease
Copy link
Contributor Author

Friendly ping. Does this looks reasonable @dsherret?

@dsherret dsherret changed the title Add minify configuration to BundleOptions feat: add minify configuration to BundleOptions Oct 23, 2023
Copy link
Member

@dsherret dsherret left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

@dsherret dsherret merged commit 8599742 into denoland:main Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants