Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Stitch ported to Ruby
Ruby JavaScript CoffeeScript
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
examples rename stitch.erb to stitch.js.erb to better represent what it generates
lib
test
.gitignore
Gemfile
LICENSE
README.md
Rakefile add tests
stitch.gemspec stitch gem is already taken :(, call it stitch-rb instead

README.md

Stitch

This is a port of Sam Stephenson's Stitch to Ruby. Stitch allows you to:

Develop and test your JavaScript applications as CommonJS modules in Node.js. Then Stitch them together to run in the browser.

In other words, this is a CommonJS JavaScript package management solution. It's terribly simple, bundling up all your applications JavaScript files without intelligently resolving dependencies. However, unless your application is very modular, it turns out thats all you need.

Usage

Install the gem, or add it to your Gemfile:

gem 'stitch-rb'

You can compile your application like this:

Stitch::Package.new(:paths => ["app"], :dependencies => ["lib/jquery.js"]).compile

It returns a JavaScript file that you can serve to your users, or write to disk and cache.

You should give Stitch::Package an array of :paths, the relative directories your JavaScript application is served from. You can also specify an array of :dependencies, JavaScript files which will be concatenated without being wrapped in CommonJS modules.

Rails & Rack

Stitch includes a basic Rack server, for example this is how you'd use it with Rails 3 routes:

match '/application.js' => Stitch::Server.new(:paths => ["app/assets/javascripts"])

Adding compilers

Compilers need to inherit from Stitch::Compiler. They're very simple, for example:

class TmplCompiler < Stitch::Compiler
  # List of supported extensions
  extensions :tmpl

  # A compile method which takes a file path,
  # and returns a compiled string
  def compile(path)
    content = File.read(path)
    %{
      var template = jQuery.template(#{content.to_json});
      module.exports = (function(data){ return jQuery.tmpl(template, data); });
    }
  end
end
Something went wrong with that request. Please try again.