Exposing esbuild's charset option to shrink non-ASCII critical scripts #46
constantly-dev
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
First off, thanks for maintaining this open-source project — it's a really focused, well-thought-out primitive.
I've been looking into how
outputSizeLimit's byte budget gets spent, and ran into an esbuild default that eats into it without any way to opt out. Tested againstesbuild@0.28.1and@woowabros/vite-plugin-critical-script@1.0.0.charsetdefaults toascii, so esbuild escapes every non-ASCII character to a\uXXXXsequence. For a critical script embedding non-Latin text, this measurably inflates the byte countoutputSizeLimitchecks against. Compiling this line:comes out to 229 bytes with the default
asciicharset and 142 bytes withutf8— a 61% difference on the exact same source. Since the compiled script ends up inside a UTF-8-encoded HTML document either way, there doesn't seem to be a correctness reason to force the ASCII-escaped form.This is an esbuild option the plugin already knows how to pass through, the same way
targetanddefineare exposed today. A possible API could look like this:Typed straight from esbuild (
esbuild.BuildOptions['charset']) and passed toesbuild.build()unchanged, so the default stays exactly as it is today (ascii) and nothing about existing behavior changes.Let me know if this direction makes sense. If so, I'm happy to put together a PR with the option and a test, mirroring how target is implemented.
All reactions