only call the coffeescript compiler as much as needed. so much faster

This commit is contained in:
John Bintz 2011-06-10 14:18:35 -04:00
parent 26675216ad
commit 67940c5066
4 changed files with 95 additions and 44 deletions

View File

@ -11,4 +11,4 @@ gem 'guard-shell'
gem 'guard-coffeescript' gem 'guard-coffeescript'
gem 'growl' gem 'growl'
gem 'rake', '0.8.7' gem 'rake', '0.8.7'
gem 'mocha' gem 'mocha', '0.9.12'

View File

@ -8,6 +8,7 @@ end
$:.unshift(File.join(gem_dir, 'lib')) $:.unshift(File.join(gem_dir, 'lib'))
require 'benchmark'
require 'yaml' require 'yaml'
require 'fileutils' require 'fileutils'
require 'getoptlong' require 'getoptlong'
@ -36,7 +37,7 @@ opts = GetoptLong.new(
) )
options = { options = {
:colors => false, :colors => false,
:remove_html_file => true, :remove_html_file => true,
:jasmine_config => 'spec/javascripts/support/jasmine.yml', :jasmine_config => 'spec/javascripts/support/jasmine.yml',
:report => false, :report => false,
@ -68,7 +69,7 @@ opts.each(&@process_options)
puts "Running Jasmine specs..." puts "Running Jasmine specs..."
files_list = Jasmine::FilesList.new( files_list = Jasmine::FilesList.new(
:config => load_config(options[:jasmine_config]), :config => load_config(options[:jasmine_config]),
:only => ARGV.dup :only => ARGV.dup
) )

View File

@ -37,25 +37,42 @@ module Jasmine
private private
def to_html(files) def to_html(files)
coffeescript_run = []
files.collect { |file| files.collect { |file|
next @code_for_file[file] if @code_for_file[file] next @code_for_file[file] if @code_for_file[file]
@code_for_file[file] = (case File.extname(file) coffeescript_run << file if (ext = File.extname(file)) == '.coffee'
when '.js'
%{<script type="text/javascript" src="#{file}"></script>} output = []
when '.coffee' if (files.last == file or ext != '.coffee') and !coffeescript_run.empty?
begin output << ensure_coffeescript_run!(coffeescript_run)
%{<script type="text/javascript">#{CoffeeScript.compile(fh = File.open(file))}</script>} end
rescue CoffeeScript::CompilationError => e
puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), file.color(:yellow), e.message.to_s.color(:white) ] if ext != '.coffee'
exit 1 output << case File.extname(file)
ensure when '.js'
fh.close %{<script type="text/javascript" src="#{file}"></script>}
when '.css'
%{<link rel="stylesheet" href="#{file}" type="text/css" />}
end end
when '.css' end
%{<link rel="stylesheet" href="#{file}" type="text/css" />}
end) @code_for_file[file] = output
} }.flatten.reject(&:empty?)
end
def ensure_coffeescript_run!(files)
data = StringIO.new
files.each { |file| data << File.read(file) }
data.rewind
%{<script type="text/javascript">#{CoffeeScript.compile(data)}</script>}
rescue CoffeeScript::CompilationError => e
puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), file.color(:yellow), e.message.to_s.color(:white) ]
exit 1
ensure
files.clear
end end
def spec_filter def spec_filter

View File

@ -112,39 +112,72 @@ describe Jasmine::FilesList do
describe '#.*files_to_html' do describe '#.*files_to_html' do
include FakeFS::SpecHelpers include FakeFS::SpecHelpers
before do context 'one coffeescript file' do
files_list.instance_variable_set(:@files, [ before do
'test.js', files_list.instance_variable_set(:@files, [
'test.coffee', 'test.js',
'test.css' 'test.coffee',
]) 'test.css'
])
files_list.instance_variable_set(:@filtered_files, [ files_list.instance_variable_set(:@filtered_files, [
'test.js', 'test.js',
'test.coffee' 'test.coffee'
]) ])
File.open('test.coffee', 'w') File.open('test.coffee', 'w') { |fh| fh.print "first" }
CoffeeScript.stubs(:compile).returns("i compiled") CoffeeScript.stubs(:compile).with() { |field| field.read == "first" }.returns("i compiled")
end end
describe '#files_to_html' do context '#files_to_html' do
it "should create the right HTML" do it "should create the right HTML" do
files_list.files_to_html.should == [ files_list.files_to_html.should == [
%{<script type="text/javascript" src="test.js"></script>}, %{<script type="text/javascript" src="test.js"></script>},
%{<script type="text/javascript">i compiled</script>}, %{<script type="text/javascript">i compiled</script>},
%{<link rel="stylesheet" href="test.css" type="text/css" />} %{<link rel="stylesheet" href="test.css" type="text/css" />}
] ]
end
end
context '#filtered_files_to_html' do
it "should create the right HTML" do
files_list.filtered_files_to_html.should == [
%{<script type="text/javascript" src="test.js"></script>},
%{<script type="text/javascript">i compiled</script>}
]
end
end end
end end
describe '#filtered_files_to_html' do context 'two coffeescript files' do
it "should create the right HTML" do before do
files_list.filtered_files_to_html.should == [ files_list.instance_variable_set(:@files, [
%{<script type="text/javascript" src="test.js"></script>}, 'test.js',
%{<script type="text/javascript">i compiled</script>} 'test.coffee',
] 'test2.coffee',
'test.css'
])
files_list.instance_variable_set(:@filtered_files, [
'test.js',
'test.coffee'
])
File.open('test.coffee', 'w') { |fh| fh.print "first" }
File.open('test2.coffee', 'w') { |fh| fh.print "second" }
CoffeeScript.stubs(:compile).with() { |field| field.read == "firstsecond" }.returns("i compiled")
end
context '#files_to_html' do
it "should create the right HTML" do
files_list.files_to_html.should == [
%{<script type="text/javascript" src="test.js"></script>},
%{<script type="text/javascript">i compiled</script>},
%{<link rel="stylesheet" href="test.css" type="text/css" />}
]
end
end end
end end
end end