-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Description
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:
- Safety First: I avoided risky options like
mangle.properties,unsafe, orpure_gettersthat could break internal logic or user access. - Modern Syntax: Enabled
ecma: 2020output (using arrow functions, method shorthands) which is shorter than ES5. - Better Compression: Increased
passesto 4 and enabledtoplevelmangle/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
tsconfigtarget, 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 testpassed successfully.
Verified with pnpm bundlewatch:
PASS ./dist/index.cjs.js: 11.19KB < 11.25KB (gzip)