Compress away all unused tokens in ESM bundle #1507
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
After bundling, the bundle looks something like this:
A smart minifier that knows that tokens and its nested properties are never modified could minify this to:
ESBuild doesn't yet do this, but terser can. In most cases, I don't think it makes sense to minify files published to npm. But in some cases, where there are unusual, project-specific minification techniques that can reduce the bundle size, I think it makes sense for the published package to have the minified version published. This reduces the burden on consumers of the package to have to set up some complex or unusual minification process. (for example Preact does this because their terser setup is definitely unusual).
In this project, getting the tokens object to minify-away requires a slightly unusual terser setup:
The
passesoption must be 6 or more. Each pass allows terser to remove one level of the object structure, i.e. after the first pass the output would be:The minified+gzipped size of the bundle with mangling disabled, passes: 1, is 4.54kb, and with mangling disabled, passes: 6 is 1.14kb