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 'growl'
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'))
require 'benchmark'
require 'yaml'
require 'fileutils'
require 'getoptlong'
@ -36,7 +37,7 @@ opts = GetoptLong.new(
)
options = {
:colors => false,
:colors => false,
:remove_html_file => true,
:jasmine_config => 'spec/javascripts/support/jasmine.yml',
:report => false,
@ -68,7 +69,7 @@ opts.each(&@process_options)
puts "Running Jasmine specs..."
files_list = Jasmine::FilesList.new(
:config => load_config(options[:jasmine_config]),
:config => load_config(options[:jasmine_config]),
:only => ARGV.dup
)

View File

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

View File

@ -112,39 +112,72 @@ describe Jasmine::FilesList do
describe '#.*files_to_html' do
include FakeFS::SpecHelpers
before do
files_list.instance_variable_set(:@files, [
'test.js',
'test.coffee',
'test.css'
])
context 'one coffeescript file' do
before do
files_list.instance_variable_set(:@files, [
'test.js',
'test.coffee',
'test.css'
])
files_list.instance_variable_set(:@filtered_files, [
'test.js',
'test.coffee'
])
files_list.instance_variable_set(:@filtered_files, [
'test.js',
'test.coffee'
])
File.open('test.coffee', 'w')
File.open('test.coffee', 'w') { |fh| fh.print "first" }
CoffeeScript.stubs(:compile).returns("i compiled")
end
CoffeeScript.stubs(:compile).with() { |field| field.read == "first" }.returns("i compiled")
end
describe '#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" />}
]
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
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
describe '#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>}
]
context 'two coffeescript files' do
before do
files_list.instance_variable_set(:@files, [
'test.js',
'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