On the quest for bundle size
Note
This was written before discord's rust client rewrite
Background
Vencord, a discord client mod, has a feature where users can write custom css in-app that will apply to the page. We use monaco-editor as the interface for users to write the aforementioned css. This works well as monaco, which is literally the extracted vscode core editor; however it isn't perfect. If you throw monaco into a simple vite app.
// index.js
import monaco from "monaco-editor";
console.log(monaco);you will end up with an app that loads a whopping 14MiB of assets (js/css)
The solution
With a few tricks, we can get monaco-editor down to only 4.9MiB. The inquisitive can find the full code here.
Warning
The link above is specially written for vencord and will not work in a standard vite setup, for code that you can just drop into your vite app with minimal changes, see this (the same codegen that powers this site!)
esbuild, the build system used for vencord, just isn't enough. While esbuild is a great tool, it doesn't have support for anything even close to the emitFile plugin context method provided by rollup.
esbuild also provides us with a blessing in disguise, it doesn't support the standard syntax for bundling web workers(everyone's least favorite nail)
// file.js
const worker = new Worker(new URL("./worker.js", import.meta.url))This is supported in virtually every other bundler: vite, rspack, webpack, parcel, rolldown(experimental) just to name a few
if esbuild did support this feature, the bundle size would be a the 14MiB figure mentioned above, because of all the web workers that monaco imports; however, because esbuild erroneously does not bundle workers, the bundle is only 6MiB. Our bundle manages to just beat it at 4.9MiB.
Trick #1: Codegen
everyone's favorite hammer.
monaco-editor providers two entry points. editor.main and editor.api. When you import editor.main not only do you include the main editor, you also include every feature it has, and support for every language it has. This includes some very heavy dependencies, such as the entire typescript language server, a css, scss, less, json, and html parser and a ton of language definitions. editor.api includes only the core editor api, with the downside that you have to manually select the languages and features that you want. This isn't that simple as some languages also require separate build config for their respective workers (html, css, json and js/ts). With codegen, we can automate this process, and easily only include the features we need.
Trick #2: Common sense
one would think
Imagine a bundle like this
// file 1
import shared from "large-shared-dep";
do_thing(shared);
const worker = new Worker(new URL("./worker.js", import.meta.url));
// worker.js
import shared from "large-shared-dep";
do_worker_thing(shared);you would want large-shared-dep to actually be shared, but bundlers will actually bundle the worker as a single file1, duplicating the dependency. by using the emitFile plugin context method, we can instead create a separate entry chunk, so the bundler uses shared chunks for shared dependencies. With this, we can save an additional 1.1MiB over esbuild.
Further reading
This only covers surface-level bundle size optimizations, if you want to learn more about neat ways to minify code even more, I suggest you read this 2023 vscode blog post about how vscode shaved 3.9MiB off their bundle size (sadly we don't get those gains).
Footnotes
-
There are various reasons for this that make sense from a "it should just work" point of view, and i do believe this is the correct default choice ↩