Sounds like a solid approach, but definitely could be tricky.
I played around a bit and had some luck using https://github.com/benjamn/recast, with an incredibly basic feature flag implementation:
https://gist.github.com/pikdum/46ddcc20b73072f1aa873844b2595b3f
Hopefully that's a bit helpful...