Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the CoffeeScript compiler and clean up some specs
- Loading branch information
Showing
10 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module Rake::Pipeline::Web::Filters | ||
# A filter that compiles CoffeeScript to JavaScript. | ||
class CoffeeScriptCompiler < Rake::Pipeline::Filter | ||
# By default, the CoffeeScriptCompiler converts inputs | ||
# with the extension +.coffee+ to +.js+. | ||
# | ||
# @param [Proc] block the output name generator block | ||
def initialize(&block) | ||
block ||= proc { |input| input.sub(/\.coffee$/, '.js') } | ||
super(&block) | ||
end | ||
|
||
# The body of the filter. Compile each input file into | ||
# a CoffeeScript compiled output file. | ||
# | ||
# @param [Array] inputs an Array of FileWrapper objects. | ||
# @param [FileWrapper] output a FileWrapper object | ||
def generate_output(inputs, output) | ||
require "coffee-script" | ||
|
||
inputs.each do |input| | ||
output.write CoffeeScript.compile(input) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
describe "CoffeeScriptCompiler" do | ||
CoffeeScriptCompiler = Rake::Pipeline::Web::Filters::CoffeeScriptCompiler | ||
|
||
COFFEE_INPUT = <<-COFFEE | ||
x = 1; | ||
y = -> | ||
x += 1 | ||
COFFEE | ||
|
||
EXPECTED_COFFEE_OUTPUT = <<-HTML | ||
(function() { | ||
var x, y; | ||
x = 1; | ||
y = function() { | ||
return x += 1; | ||
}; | ||
}).call(this); | ||
HTML | ||
|
||
def input_file(name, content) | ||
MemoryFileWrapper.new("/path/to/input", name, "UTF-8", content) | ||
end | ||
|
||
def output_file(name) | ||
MemoryFileWrapper.new("/path/to/output", name, "UTF-8") | ||
end | ||
|
||
def setup_filter(filter) | ||
filter.file_wrapper_class = MemoryFileWrapper | ||
filter.input_files = [input_file("input.coffee", COFFEE_INPUT)] | ||
filter.output_root = "/path/to/output" | ||
filter.rake_application = Rake::Application.new | ||
filter | ||
end | ||
|
||
it "generates output" do | ||
filter = setup_filter CoffeeScriptCompiler.new | ||
|
||
filter.output_files.should == [output_file("input.js")] | ||
|
||
tasks = filter.generate_rake_tasks | ||
tasks.each(&:invoke) | ||
|
||
file = MemoryFileWrapper.files["/path/to/output/input.js"] | ||
file.body.should == EXPECTED_COFFEE_OUTPUT | ||
file.encoding.should == "UTF-8" | ||
end | ||
|
||
describe "naming output files" do | ||
it "translates .coffee extensions to .js by default" do | ||
filter = setup_filter CoffeeScriptCompiler.new | ||
filter.output_files.first.path.should == "input.js" | ||
end | ||
|
||
it "accepts a block to customize output file names" do | ||
filter = setup_filter(CoffeeScriptCompiler.new { |input| "octopus" }) | ||
filter.output_files.first.path.should == "octopus" | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters