Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Use CommonJS modules in your Rack/Rails applications

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 example
Octocat-spinner-32 lib
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 rack-modulr.gemspec
README.md

rack-modulr lets you easily use Common.JS modules in your Rack/Rails applications.

Prerequisites

Although not required, it's recommended you use this custom version of the Modulr gem.

Usage

For example with Rack:

require "rack/modulr"

use Rack::Modulr, :modulr => {:minify => true}
run Proc.new { [200, {"Content-Type" => "text/html"}, []] }

Or with Rails:

// Gemfile

gem "modulr",      :git => "git://github.com/maccman/modulr.git"    
gem "rack-modulr", :git => "git://github.com/maccman/rack-modulr.git"

// config/application.rb
require "rack/modulr"
config.middleware.use "Rack::Modulr"

Then any modules in app/javascripts will be automatically parsed by Modulr

// app/javascripts/utils.js    
exports.sum = function(val1, val2){
  return(val1 + val2);
};

// app/javascripts/application.js
var utils = require("./utils");
console.log(utils.sum(1, 2));

When the browser requests a module, all its dependencies will be recursively resolved.

$ curl "http://localhost:5001/javascripts/application.js"

(function() {
  require.define({
    'utils': function(require, exports, module) {
      exports.sum = function(val1, val2){
        return(val1 + val2);
      };
    }
  });

  require.ensure(['utils'], function(require) {
    var utils = require("./utils");
    console.log(utils.sum(1, 2));
  });
})();

Modulr injects a module loader library but if you want to use a different one, like Yabble, you'll need to pass the :custom_loader option to Rack::Modulr:

use Rack::Modulr, :modulr => {:custom_loader => true}

Rack::Modulr caches the compiled modules in memory. Every request, the request module will be checked, to see if it's mtime has changed. If the module hasn't been changed, it'll be served from memory if possible.

By defaulting, caching and minification are turned off. To turn them on in the production environment, for example, set the global settings on Rack::Modulr.

// production.rb
Rack::Modulr.configure do |config|
  config.cache  = true
  config.minify = true
end

Based on Kelly Redding's great work on rack-less.

Something went wrong with that request. Please try again.