Add AMD/commonJS compatibility

I put in a relatively standard block that looks for different JavaScript module loaders and uses them if available, otherwise it defaults to the previous behavior of just sending globals to the closure.