Skip to content

perf(runtime): enable webpack compress and mangle#5057

Open
awsmadi wants to merge 1 commit intoaws:mainfrom
awsmadi:perf/webpack-optimization
Open

perf(runtime): enable webpack compress and mangle#5057
awsmadi wants to merge 1 commit intoaws:mainfrom
awsmadi:perf/webpack-optimization

Conversation

@awsmadi
Copy link

@awsmadi awsmadi commented Mar 11, 2026

Summary

Enable Terser compression and mangling for the jsii runtime webpack bundle, reducing bundle size and improving startup time.

Changes

  • packages/@jsii/runtime/webpack.config.js:
    • compress: falsecompress: true (dead code elimination, constant folding)
    • beautify: true → removed (no need for pretty output)
    • mangle: falsemangle: { keep_classnames: true, keep_fnames: true }
    • Added top-level keep_classnames and keep_fnames to protect against compress pass renaming

Safety Analysis

  • keep_classnames: true: Preserves class names used in constructor.name error messages (serialization.ts:1296, objects.ts:131) and Kernel.prototype access (recording.ts:34)
  • keep_fnames: true: Preserves function names in stack traces sent to language runtimes via error.stack in the jsii protocol (host.ts:204)
  • Both set at two levels: terserOptions top level (protects compress pass) AND inside mangle (protects mangle pass)
  • No dangerous patterns: No eval(), with, new Function(), arguments.callee in the codebase
  • const enum safe: JsiiErrorType values are inlined as string literals by TypeScript before Terser sees them
  • Source maps preserved: devtool: 'source-map' unchanged, .map files ship with all language runtimes

Blast Radius

This bundle is embedded in ALL four language runtimes (Python, Java, .NET, Go). Full cross-language compliance testing is recommended before merge.

Test plan

  • TypeScript compiles cleanly
  • Full cross-language compliance test suite
  • Verify stack traces are readable with compressed bundle
  • Verify constructor.name values in error messages

Enable Terser compression and mangling for the jsii runtime bundle.
This reduces bundle size and improves require() time at startup.

- compress: true — enables dead code elimination and constant folding
- mangle: true — shortens local variable names
- keep_classnames: true — preserves class names used in error messages
  (serialization.ts:1296, objects.ts:131) and stack traces
- keep_fnames: true — preserves function names for readable stack traces
  sent to language runtimes via the jsii protocol
- Both keep_* options set at terserOptions top level AND inside mangle
  to protect against both compress and mangle passes

Estimated 10-50ms faster initial require() due to smaller bundle size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant