Skip to content

perf: Optimize bundle size by tuning Terser compression options #13243

@kamja44

Description

@kamja44

Title: perf: Optimize bundle size by tuning Terser compression options


Is your feature request related to a problem? Please describe.
The current bundle size of dist/index.cjs.js is 11.22KB, which is very close to the limit of 11.25KB.
We need to secure a bit more margin for future updates without changing the source code logic or breaking backward compatibility.

Describe the solution you'd like
I suggest tuning the Rollup > Terser configuration to enable safer and more aggressive compression options.

I have tested the following configuration changes in scripts/rollup/createRollupConfig.js and confirmed they are safe and effective:

  1. Safety First: I avoided risky options like mangle.properties, unsafe, or pure_getters that could break internal logic or user access.
  2. Modern Syntax: Enabled ecma: 2020 output (using arrow functions, method shorthands) which is shorter than ES5.
  3. Better Compression: Increased passes to 4 and enabled toplevel mangle/compress.

Proposed Configuration Changes:

terser({
  ecma: 2020, // Use shorter modern syntax (optional chaining, etc.)
  output: { comments: false },
  compress: {
    drop_console: true,
    passes: 4, // Increase passes for better compression (default 2)
    toplevel: true, // Drop unused toplevel variables
    unsafe_arrows: true, // Use arrow functions (x => x) instead of function(x){return x}
    unsafe_methods: true, // Use method shorthand { a() {} } instead of { a: function() {} }
    unsafe: false, // Keep false for safety
    unsafe_comps: false, // Keep false for safety
    unsafe_math: false, // Keep false for safety
    booleans_as_integers: true, // Optimize true/false to 1/0
    pure_funcs: ['console.log', 'console.info', 'console.debug'],
  },
});

Describe alternatives you've considered

  • considered mangle.properties (renaming private properties like _options), but it was too risky as it breaks external access if users rely on them.
  • considered changing tsconfig target, but changing Terser config seemed less intrusive to the build pipeline.

Additional context
Result:

  • Before: 11.22KB
  • After: 11.19KB (Saved ~30-40 Bytes without logic change)
  • Tests: pnpm test passed successfully.

Verified with pnpm bundlewatch:

PASS ./dist/index.cjs.js: 11.19KB < 11.25KB (gzip)

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestrequest a feature to be addedwaiting-up-voteWaiting for votes from the community.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions