I’ve been working on a development setup and wanted to see if others have tried something similar or have ideas to make it better.
My current approach uses Node.js for building frontend code. I have Grunt with a watch task that combines and compresses all my source files into one minified file that goes into the public/js directory. For unit testing, I use Jasmine-node which requires an index file that loads all the individual source files.
Since I want the same code to work in both Node and browsers, I put everything under a global namespace like:
myProject.core.someModule
In my main file, I export to Node only if module and exports are available. This way I don’t need to add module.exports to every file. Instead I can just write:
(function() { myProject.core.someModule = function() {...} })();
or simply:
myProject.core.someModule = function() {...}
This works great in browsers since everything loads together, but in Node I sometimes get errors when one class tries to use another from the myProject namespace because I’m not handling dependencies properly like RequireJS would.
I’m thinking about adding require statements to files like a normal Node app, then stripping out Node-specific code during the build process.
Any thoughts or suggestions would be really helpful!