I find libraries such as Require.js to be too complicated to trust them.<p>I put all my client-side JS files into ./src directory and watch them with Node.js server. When any file in ./src is added or modified then server automatically copies it to ./build directory and wraps it with additional code required by module loader.<p>This way I can use (almost) the same syntax for handling modules on both client- and server-side, e.g. I can write client-side script which looks like this:<p><pre><code> var button = require('widgets/button.js').button;
var toggleButton = Object.extend(button, {
label: 'Toggle me',
});
exports.toggleButton = toggleButton;
</code></pre>
And it will automatically get compiled into:<p><pre><code> 'use strict'; module('widgets/toggleButton.js', function(exports) {
var button = require('widgets/button.js').button;
var toggleButton = Object.extend(button, {
label: 'Toggle me',
});
exports.toggleButton = toggleButton;
});
</code></pre>
The client-side framework for handling such modules takes 28 lines of code: <a href="https://gist.github.com/1287050" rel="nofollow">https://gist.github.com/1287050</a><p>Unless you are writing really large apps, this approach seems to be much cleaner, just remember to combine and minify all modules before deployment.