Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

A way of splitting up your code

1458207205
Lucian
Pro
API Scripter
Hey all, I don't know if anyone else has had the same problem, but I find that with bigger and more complex scripts, I really want to split them down into properly factored modules that can be unit tested etc. The obvious solution is to use CommonJS2 modules and require, but that's obviously not compatible with Roll20's API system, and even if it was, it's dubious whether you'd want all your internal modules having to be installed as separate scripts. Anyway, this is probably obvious to all you modern-front-end-type developers out there, but it turns out that webpack actually does a really good job of producing a script that will run under roll20. I've defined a roll20 wrapper library that the rest of my code uses to keep all the globals in one place, and then I just tell webpack that require('underscore') maps to a global '_', and it all seems to Just Work. Take a look at&nbsp; <a href="https://github.com/symposion/roll20-api-scripts/tr" rel="nofollow">https://github.com/symposion/roll20-api-scripts/tr</a>... and tell me what you think ("npm i && gulp").&nbsp; BTW, I did also try browserify - which I like because the module loading code is less obtrusive in the final result - but it was awkward about handling the global deps the way roll20 does them, most of the transforms available for it to fix the problem were broken in one way or another, and writing my own resulted in a series of hopelessly cryptic error messages in impossible-to-debug code :-( Webpack worked first time and everything seems a bit more... well... professional!&nbsp; Cheers, Lucian
1458221584
The Aaron
Pro
API Scripter
Cool! &nbsp;I'm gonna take a look for sure.