Commit ae2c733
feat: Add SWC feature flags library with build-time and runtime transforms (#578)
Implements a two-phase feature flag system:
Phase 1 (Build-time):
- SWC Wasm plugin that marks feature flag usage
- Replaces flag identifiers with __SWC_FLAGS__ markers
- Removes imports and hook calls from configured libraries
- Supports destructuring patterns and multiple libraries
- Handles variable shadowing correctly using SWC's Id system
Phase 2 (Runtime):
- Standalone Rust crate for dead code elimination
- Substitutes flag values with boolean literals
- Eliminates dead branches (if, ternary, logical operators)
- Tracks statistics (bytes removed, branches eliminated)
- Unwraps block statements for cleaner output
Features:
- Customizable function names (not hardcoded)
- Selective flag exclusion
- Scope-safe transformations
- Minifier-safe markers
- Comprehensive fixture tests
- TypeScript definitions
Packages:
- crates/swc_feature_flags: Standalone Rust crate
- packages/feature-flags: SWC Wasm plugin wrapper
---------
Co-authored-by: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
Co-authored-by: Donny/강동윤 <kdy1@users.noreply.github.com>1 parent 15f32da commit ae2c733
25 files changed
Lines changed: 1877 additions & 0 deletions
File tree
- crates/swc_feature_flags
- src
- tests
- fixture
- build-time
- basic
- nested-scope
- runtime
- if-statement
- logical
- ternary
- packages/feature-flags
- src
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
0 commit comments