27 points · by v33ra · 14y ago · 14 comments
Let me introduce you OneJS: http://github.com/azer/onejs
It simply converts any CommonJS package into a single, stand-alone JavaScript file. It means, you can structure your client-side project just like a NodeJS project and you can convert your NodeJS project to a single file just like a client-side project.
Unlike Browserify, OneJS is unobtrusive [1], provides an accurate implementation of CommonJS specs [2] and the output it produces is suitable for splitting and having async dependencies.
[1] A chess website built with OneJS: http://multiplayerchess.com/mpc.js
[2] ExpressJS built with OneJS, with all dependencies: https://gist.github.com/2415048
browserify-http can be used by OneJS as well but I don't see any point of emulating http module on client-side. I prefer using libraries supporting both platforms, such as boxcars; http://github.com/azer/boxcars
I wonder how this will work when optimized.
It looks like with browserify you only ever reference a single script file in your HTML, regardless of development or production environments. While this unifies HTML rendering across development and production environments, during development every time you change a module you must rebuild that script file. In actuality, this is not much of a problem since browserify appears to have a "watch" mode to do this automatically. If you have a huge codebase, building could take a noticeable amount of time.
With RequireJS, this build step is not needed during development. When you save module changes and reload your browser (with caching disabled), the changed module will be delivered. If you have a huge codebase, no time-consuming build step is required. Of course in production you want to deliver only a single file, and RequireJS can be used to build this. However, depending on your overall build, you may need to conditionally render different script tags for production and development, which is a tiny but not insignificant bit of complexity.
To allow RequireJS to load node (CommonJS-based) modules the module needs to be wrapped in a special function call. RequireJS supplies tooling to help with this. Alternatively, RequireJS can be used in node to load AMD modules, to allow modules to be used client- and server-side.
If you have a small codebase and use the same modules in node and in the browser, browserify seems to be the way to go. For large codebases, browserify is only an option if you have lots of modules being used in node and in the browser and find RequireJS' tooling inadequate. Otherwise, RequireJS' flexibility seems to make it the preference.
My modules just look like this:
(function(exports) {
...
exports.moduleName = ...;
})(this);
Often times you don't need a huge includer just to keep your code modular.see my comment above for details about the differences; http://news.ycombinator.com/item?id=3915403
I appreciate browserify having the exact same syntax, but RequireJS can do serverside.