I’m pretty sure that’s exactly what the Google Closure Compiler does: https://developers.google.com/closure/compiler/docs/limitati...
Though it has a ton of restrictions on how you write code and how you can use reflection.