Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Stitch ported to Ruby

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 examples
Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 Gemfile
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 stitch.gemspec
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.