I've been using a very similar tool, browserify to do the same thing. The great thing about browserify is it already comes with core node.js library support, as well as an http module wrapper.
With tools like this, I love being able to modularize my client-side code using CommonJS modules and running JS test from the command line using tools like Vows and Buster.js. I also love being able to declare dependencies on npm modules. It's just as easy as testing and writing Ruby code with rubygems and rspec.
The only issue that still bothers me is that nearly every DOM querying/manipulating library out there assumes there's a global window or document object. This means I have to stub it out in any CLI tests I run and then make sure jquery or whatever is referencing a virtual window I create with jsdom instead of the global stub.
although onejs and browserify tries to accomplish similar tasks, onejs has some noticeable implementation and concept differences (e.g browserify pollutes global scope a lot and it expects you to provide it a main module instead of a package manifest)
here is the output comparison of onejs and browserify:
I've been looking at Node recently and I'm struggling to get my head around the run both on server and client methodology. I understand why it's useful to define model classes and include the file in both places, however I don't really see how this extends to the rest of the app. This tool seems to imply that you join all the modules of the app together into one file, which you can then include on the client - but surely there'd be code that you don't want included in this (like the server-side of websocket communication)?
Does anyone have any articles that can help me to fill the gaps?
1) "Hey, how can I use the same code on the server and the client?" Hard problem, but you can look at Meteor or Derby for people working hard to solve it. It's unrelated to this project though.
2) "Hey, I've been using npm and require and coffeescript to write some cool code, but I want to run the entire thing in the browser. How can I somehow "bake" this all down to a single JS file I can deliver to the client where everything just works? This is a pretty easy problem, and it's been solved by Stitch (my favourite), Hem, Browserify, and probably a few more. And now, OneJS. :)
If it helps to see an example, I'm working on an app with a really big complicated source code tree; it's split into lots of files, plus a bunch of libraries, some of which are "traditional" JS libraries like jQuery, and some of which are "modern" CommonJS modules installed by npm. I use Stitch to bundle the whole thing into a single application.js file which contains every bit of my client-side code (only). My HTML file only contains two script blocks.
FOA, "using same code" means "sharing same source code in all JS environments" in this context. "Proxying some server-side JS code to client-side" is totally irrelevant to this topic, as you pointed.
And here are the differences of OneJS:
- it produces unobtrusive code
- it bundles any level of dependency properly
- it's not only for web browsers, but also all JS environments including Node itself
- it's a command-line tool that doesn't require you to code.
Interesting. From what I can see, it just tricks any CommonJS style module into thinking that the module and exports variables are available; and that it's running in a node like environment. Things like accessing the file system and http wont' work, but that doesn't seem to be what it's meant for - it's more "write your client side js in a testable common js format, declare dependencies like backbone and underscore, and you'll get a neat little browser runnable package at the end".
sigh If I had known about this a week ago, I could have avoided a whole lot of hardship dealing with Require.js + node_jasmine + NPM... three complex things that were never really meant to work together. But I can see this as being really groundbreaking, kind of sidestepping the whole AMD vs. CommonJS protocol fragmentation and offering NPM as a way to distribute code meant to be bundled for clients!