Skip to content

@josh /Gemfile
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Sprockets Closure Library Processor
require 'sprockets'
require 'tilt'
# svn checkout http://closure-library.googlecode.com/svn/trunk/ closure-library
ClosureLibrary = File.expand_path("../closure-library", __FILE__)
class ClosureDependenciesProcessor < Tilt::Template
def prepare; end
def evaluate(context, locals, &block)
# METHOD 1: Shell out to calcdeps
# calcdeps(file).each do |path|
# context.require_asset(path)
# end
# METHOD 2: Extract goog.require statments and guess filename
# Much faster, but I don't know about closure lib to know
# that its reilable.
data.lines.each do |line|
if line =~ /goog\.require\s*\(\s*[\'\"]([^\)]+)[\'\"]\s*\)/
goog, mod, sub = $1.split(".")
sub ||= mod
mod, sub = mod.downcase, sub.downcase
dep = [goog, mod, sub].join("/")
context.require_asset(dep)
end
end
data
end
def calcdeps(path)
bin = File.join(ClosureLibrary, "closure/bin/calcdeps.py")
`#{bin} -i #{path} 2> /dev/null`.split("\n")
end
end
Assets = Sprockets::Environment.new do |env|
env.append_path "."
env.append_path File.join(ClosureLibrary, "closure")
env.register_preprocessor 'application/javascript', ClosureDependenciesProcessor
end
map "/assets" do
run Assets
end
map "/" do
run lambda { |env|
html = <<-HTML
<html>
<head>
<script src="/assets/hello.js"></script>
</head>
<body onload="sayHi()">
</body>
</html>
HTML
[200, {'Content-Type' => 'text/html'}, [html]]
}
end
source :rubygems
gem 'rack'
gem 'sprockets', :git => 'git://github.com/sstephenson/sprockets.git'
goog.require('goog.dom');
function sayHi() {
var newHeader = goog.dom.createDom('h1', {'style': 'background-color:#EEE'}, 'Hello world!');
goog.dom.appendChild(document.body, newHeader);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.