Commit 577b5ee
feat(flags): Add two-phase mode support (mark & shake) (#580)
## Summary
Add support for two-phase feature flag transformation with `mark` and
`shake` modes.
## Changes
### New Mode System
- **Mark mode (default)**: Marks flags with `__SWC_FLAGS__` markers for
later substitution
- **Shake mode**: Substitutes markers with boolean values and performs
DCE
This provides a clearer two-phase workflow:
1. **Phase 1 (mark)**: Convert flag variables to markers
2. **Phase 2 (shake)**: Substitute markers and eliminate dead code
### Implementation
- Add `TransformMode` enum with `Mark` and `Shake` variants
- Add unified `FeatureFlagsConfig` supporting both modes
- Update plugin to dispatch to `BuildTimeTransform` (mark) or
`RuntimeTransform` (shake)
- Mark mode is now the default (previously was implicit build-time
behavior)
### TypeScript Types
- Updated `types.d.ts` with new `TransformMode` type
- Added comprehensive JSDoc documentation with examples
- Maintained backward compatibility
### Tests
- Added comprehensive WASM integration tests (`__tests__/wasm.test.ts`)
- Tests for mark mode: marker generation, custom marker objects
- Tests for shake mode: DCE on markers, logical operations
- Tests for edge cases: exclude flags, nested scopes, multiple libraries
- All 8 tests passing ✅
### Documentation
- Updated README with two-phase workflow explanation
- Added complete examples showing both phases
- Clarified when to use each mode
## Example Usage
### Mark Mode (Phase 1)
```json
{
"mode": "mark",
"libraries": {
"@your/flags": {
"functions": ["useFlags"]
}
}
}
```
### Shake Mode (Phase 2)
```json
{
"mode": "shake",
"flagValues": {
"featureA": true,
"featureB": false
}
}
```
## Breaking Changes
None - the plugin maintains backward compatibility with the old
`BuildTimeConfig` format.
## Test Results
- ✅ Rust crate tests: 5 passed
- ✅ WASM integration tests: 8 passed
- ✅ Build successful
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>1 parent cdb3cca commit 577b5ee
10 files changed
Lines changed: 1076 additions & 52 deletions
File tree
- contrib/mut-cjs-exports/src
- crates/swc_feature_flags/src
- packages/feature-flags
- __tests__
- __snapshots__
- src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
214 | 214 | | |
215 | 215 | | |
216 | 216 | | |
217 | | - | |
218 | 217 | | |
219 | 218 | | |
220 | 219 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
5 | 55 | | |
6 | 56 | | |
7 | 57 | | |
| |||
76 | 126 | | |
77 | 127 | | |
78 | 128 | | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
| |||
111 | 113 | | |
112 | 114 | | |
113 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
0 commit comments