CommonJs support for Rhino, in Ruby (JRuby specifically). commonjs-rhino allows you to create JavaScript contexts that contain a require
method you can use to load CommonJs modules.
gem install commonjs-rhino
Then, require it somewhere in your code:
require 'commonjs-rhino'
Please be aware this gem requires Mozilla's Rhino JavaScript environment, which means Rhino will need to be somewhere in your Java classpath. commonjs-rhino was developed using expert. You may find it convenient to use expert in your own project to manage commonjs-rhino's Java dependencies. Just add expert to your bundle and add these lines in your project:
require 'expert'
Expert.environment.require_all
require 'commonjs-rhino'
# your code here
Let's say you have this nice little CommonJs JavaScript module you'd like to use in Rhino. The module exists on disk at /path/to/camertron/teapot.js
:
(function() {
module.exports.strVariable = 'foobarbaz';
module.exports.func = function() {
return "I'm a little teapot";
};
}).call();
Create a commonjs-rhino context, point it at your modules, and require away:
context = CommonjsRhino.create_context(['/path/to/camertron'])
context.eval('var hello = require("camertron/teapot")')
context.eval('hello.strVariable') # => 'foobarbaz'
It's that easy!
You can also evaluate files with the handy eval_file
method:
context.eval_file('/path/to/file.js')
bundle exec rspec
should do the trick :)
- Cameron C. Dutro: http://github.com/camertron