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'

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)
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 '.coffee'
begin
%{<script type="text/javascript">#{CoffeeScript.compile(fh = File.open(file))}</script>}
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
fh.close
end
when '.css'
%{<link rel="stylesheet" href="#{file}" type="text/css" />}
end)
}
files.clear
end
def spec_filter

View File

@ -112,6 +112,7 @@ describe Jasmine::FilesList do
describe '#.*files_to_html' do
include FakeFS::SpecHelpers
context 'one coffeescript file' do
before do
files_list.instance_variable_set(:@files, [
'test.js',
@ -124,12 +125,12 @@ describe Jasmine::FilesList do
'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
describe '#files_to_html' do
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>},
@ -139,7 +140,7 @@ describe Jasmine::FilesList do
end
end
describe '#filtered_files_to_html' do
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>},
@ -148,5 +149,37 @@ describe Jasmine::FilesList do
end
end
end
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
end