reowrk a bunch of the tilt stuff, only use extensions we know
This commit is contained in:
parent
8867d00ac8
commit
e04d692d26
@ -1,5 +1,5 @@
|
||||
require 'pathname'
|
||||
require 'sprockets/engines'
|
||||
require 'sprockets'
|
||||
|
||||
module Jasmine::Headless
|
||||
autoload :CoffeeScriptCache, 'jasmine/headless/coffee_script_cache'
|
||||
@ -14,6 +14,8 @@ module Jasmine::Headless
|
||||
|
||||
autoload :TemplateWriter, 'jasmine/headless/template_writer'
|
||||
|
||||
autoload :CoffeeTemplate, 'jasmine/headless/coffee_template'
|
||||
|
||||
autoload :Report, 'jasmine/headless/report'
|
||||
autoload :ReportMessage, 'jasmine/headless/report_message'
|
||||
|
||||
@ -25,4 +27,21 @@ module Jasmine::Headless
|
||||
end
|
||||
|
||||
require 'jasmine/headless/errors'
|
||||
Sprockets::Engines
|
||||
|
||||
# register haml-sprockets if it's available...
|
||||
%w{haml-sprockets}.each do |library|
|
||||
begin
|
||||
require library
|
||||
rescue LoadError
|
||||
end
|
||||
end
|
||||
|
||||
# ...and unregister ones we don't want/need
|
||||
module Sprockets
|
||||
%w{less sass scss erb str}.each do |extension|
|
||||
@engines.delete(".#{extension}")
|
||||
end
|
||||
|
||||
register_engine '.coffee', Jasmine::Headless::CoffeeTemplate
|
||||
end
|
||||
|
||||
|
29
lib/jasmine/headless/coffee_template.rb
Normal file
29
lib/jasmine/headless/coffee_template.rb
Normal file
@ -0,0 +1,29 @@
|
||||
require 'tilt/template'
|
||||
|
||||
module Jasmine::Headless
|
||||
class CoffeeTemplate < Tilt::Template
|
||||
def prepare ; end
|
||||
|
||||
def evaluate(scope, locals, &block)
|
||||
begin
|
||||
cache = Jasmine::Headless::CoffeeScriptCache.new(file)
|
||||
source = cache.handle
|
||||
if cache.cached?
|
||||
%{<script type="text/javascript" src="#{cache.cache_file}"></script>
|
||||
<script type="text/javascript">
|
||||
window.CSTF['#{File.split(cache.cache_file).last}'] = '#{file}';
|
||||
</script>}
|
||||
else
|
||||
%{<script type="text/javascript">#{source}</script>}
|
||||
end
|
||||
rescue CoffeeScript::CompilationError => ne
|
||||
puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), file.color(:yellow), "#{ne.message}".color(:white) ]
|
||||
raise ne
|
||||
rescue StandardError => e
|
||||
puts "[%s] Error in compiling file: %s" % [ 'coffeescript'.color(:red), file.color(:yellow) ]
|
||||
raise e
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -92,7 +92,7 @@ module Jasmine::Headless
|
||||
TestFile.new(file, source_root).dependencies.each { |type, name| add_dependency(type, name, source_root) }
|
||||
end
|
||||
|
||||
EXTENSION_FILTER = %r{\.(js|css|coffee|jst.*)$}
|
||||
EXTENSION_FILTER = %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$}
|
||||
|
||||
def add_dependency(type, file, source_root)
|
||||
case type
|
||||
|
@ -1,13 +1,6 @@
|
||||
require 'rainbow'
|
||||
require 'sprockets'
|
||||
|
||||
%w{haml-sprockets}.each do |library|
|
||||
begin
|
||||
require library
|
||||
rescue LoadError
|
||||
end
|
||||
end
|
||||
|
||||
module Jasmine::Headless
|
||||
class TestFile
|
||||
attr_reader :path, :source_root
|
||||
@ -21,38 +14,7 @@ module Jasmine::Headless
|
||||
end
|
||||
|
||||
def to_html
|
||||
case File.extname(path)
|
||||
when '.coffee'
|
||||
begin
|
||||
cache = Jasmine::Headless::CoffeeScriptCache.new(path)
|
||||
source = cache.handle
|
||||
if cache.cached?
|
||||
%{<script type="text/javascript" src="#{cache.cache_file}"></script>
|
||||
<script type="text/javascript">
|
||||
window.CSTF['#{File.split(cache.cache_file).last}'] = '#{path}';
|
||||
</script>}
|
||||
else
|
||||
%{<script type="text/javascript">#{source}</script>}
|
||||
end
|
||||
rescue CoffeeScript::CompilationError => ne
|
||||
puts "[%s] %s: %s" % [ 'coffeescript'.color(:red), path.color(:yellow), ne.message.dup.to_s.color(:white) ]
|
||||
raise ne
|
||||
rescue StandardError => e
|
||||
puts "[%s] Error in compiling file: %s" % [ 'coffeescript'.color(:red), path.color(:yellow) ]
|
||||
raise e
|
||||
end
|
||||
when '.js'
|
||||
%{<script type="text/javascript" src="#{path}"></script>}
|
||||
when '.css'
|
||||
%{<link rel="stylesheet" href="#{path}" type="text/css" />}
|
||||
when '.jst'
|
||||
to_jst(read)
|
||||
else
|
||||
case path
|
||||
when %r{\.jst(\..*)$}
|
||||
to_jst(Sprockets.engines($1).new { read }.evaluate(self, {}))
|
||||
end
|
||||
end
|
||||
process_data_by_filename(path)
|
||||
end
|
||||
|
||||
def dependencies
|
||||
@ -73,13 +35,28 @@ module Jasmine::Headless
|
||||
end
|
||||
|
||||
private
|
||||
def to_jst(data)
|
||||
%{<script type="text/javascript">#{Sprockets.engines('.jst').new { data }.evaluate(self, {})}</script>}
|
||||
end
|
||||
|
||||
def read
|
||||
File.read(path)
|
||||
end
|
||||
|
||||
def process_data_by_filename(path, data = nil)
|
||||
case extension = File.extname(path)
|
||||
when ''
|
||||
data || ''
|
||||
when '.js'
|
||||
data || %{<script type="text/javascript" src="#{path}"></script>}
|
||||
when '.css'
|
||||
data || %{<link rel="stylesheet" href="#{path}" type="text/css" />}
|
||||
else
|
||||
if engine = Sprockets.engines(extension)
|
||||
data = engine.new(path) { data || read }.render(self)
|
||||
|
||||
process_data_by_filename(path.gsub(%r{#{extension}$}, ''), data)
|
||||
else
|
||||
data || ''
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
55
spec/lib/jasmine/headless/coffee_template_spec.rb
Normal file
55
spec/lib/jasmine/headless/coffee_template_spec.rb
Normal file
@ -0,0 +1,55 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Jasmine::Headless::CoffeeTemplate do
|
||||
let(:data) { 'data' }
|
||||
let(:path) { 'path.coffee' }
|
||||
|
||||
let(:template) { described_class.new(path) { data } }
|
||||
|
||||
subject { template.render }
|
||||
|
||||
let(:handle_expectation) { Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:handle) }
|
||||
|
||||
context 'compilation error' do
|
||||
let(:error) { CoffeeScript::CompilationError.new("fail") }
|
||||
|
||||
before do
|
||||
handle_expectation.raises(error)
|
||||
end
|
||||
|
||||
it 'should pass along the error' do
|
||||
expect { subject }.to raise_error(CoffeeScript::CompilationError)
|
||||
end
|
||||
end
|
||||
|
||||
context 'compiles fine' do
|
||||
let(:source) { 'source' }
|
||||
|
||||
before do
|
||||
Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cached?).returns(cache_return)
|
||||
handle_expectation.returns(source)
|
||||
end
|
||||
|
||||
context 'cached' do
|
||||
let(:file_path) { 'dir/file.js' }
|
||||
let(:cache_return) { true }
|
||||
|
||||
before do
|
||||
Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cache_file).returns(file_path)
|
||||
end
|
||||
|
||||
it 'should return the cached file' do
|
||||
subject.should include(%{<script type="text/javascript" src="#{file_path}"></script>})
|
||||
end
|
||||
end
|
||||
|
||||
context 'not cached' do
|
||||
let(:cache_return) { false }
|
||||
|
||||
it 'should return the generated js' do
|
||||
subject.should include(%{<script type="text/javascript">#{source}</script>})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -25,83 +25,45 @@ describe Jasmine::Headless::TestFile do
|
||||
it { should == %{<link rel="stylesheet" href="#{path}" type="text/css" />} }
|
||||
end
|
||||
|
||||
context '.coffee' do
|
||||
let(:path) { 'path.coffee' }
|
||||
|
||||
let(:handle_expectation) { Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:handle) }
|
||||
|
||||
context 'compilation error' do
|
||||
let(:error) { CoffeeScript::CompilationError.new("fail") }
|
||||
|
||||
before do
|
||||
handle_expectation.raises(error)
|
||||
end
|
||||
|
||||
it 'should pass along the error' do
|
||||
expect { subject }.to raise_error(CoffeeScript::CompilationError)
|
||||
end
|
||||
end
|
||||
|
||||
context 'compiles fine' do
|
||||
let(:source) { 'source' }
|
||||
|
||||
before do
|
||||
Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cached?).returns(cache_return)
|
||||
handle_expectation.returns(source)
|
||||
end
|
||||
|
||||
context 'cached' do
|
||||
let(:file_path) { 'dir/file.js' }
|
||||
let(:cache_return) { true }
|
||||
|
||||
before do
|
||||
Jasmine::Headless::CoffeeScriptCache.any_instance.stubs(:cache_file).returns(file_path)
|
||||
end
|
||||
|
||||
it 'should return the cached file' do
|
||||
subject.should include(%{<script type="text/javascript" src="#{file_path}"></script>})
|
||||
end
|
||||
end
|
||||
|
||||
context 'not cached' do
|
||||
let(:cache_return) { false }
|
||||
|
||||
it 'should return the generated js' do
|
||||
subject.should include(%{<script type="text/javascript">#{source}</script>})
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context '.jst' do
|
||||
context 'with tilt template' do
|
||||
include FakeFS::SpecHelpers
|
||||
|
||||
let(:path) { 'file.jst' }
|
||||
let(:content) { 'content' }
|
||||
|
||||
before do
|
||||
File.open(path, 'wb') { |fh| fh.print content }
|
||||
end
|
||||
|
||||
it 'should use the JST template processor to get the processed file' do
|
||||
subject.should include('JST["file"]')
|
||||
subject.should include(content)
|
||||
let(:klass) do
|
||||
Class.new(Tilt::Template) do
|
||||
def prepare ; end
|
||||
|
||||
def evaluate(scope, locals, &block)
|
||||
"#{file} made it #{data}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context '.jst.*' do
|
||||
include FakeFS::SpecHelpers
|
||||
|
||||
let(:path) { 'file.jst.ejs' }
|
||||
let(:content) { 'content' }
|
||||
|
||||
before do
|
||||
File.open(path, 'wb') { |fh| fh.print content }
|
||||
Sprockets.stubs(:engines).with('.tilt').returns(klass)
|
||||
end
|
||||
|
||||
it 'should use the JST template processor to get the processed file' do
|
||||
subject.should include('JST["file"]')
|
||||
subject.should include(content)
|
||||
context '.tilt' do
|
||||
let(:path) { 'path.tilt' }
|
||||
|
||||
it { should == "#{path} made it #{content}" }
|
||||
end
|
||||
|
||||
context '.tilt.tilt' do
|
||||
let(:path) { 'path.tilt.tilt' }
|
||||
|
||||
it { should == "path.tilt made it #{path} made it #{content}" }
|
||||
end
|
||||
|
||||
context '.js.tilt' do
|
||||
let(:path) { 'path.js.tilt' }
|
||||
|
||||
it { should == "#{path} made it #{content}" }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -3,6 +3,7 @@ require 'fakefs/spec_helpers'
|
||||
|
||||
RSpec.configure do |c|
|
||||
c.mock_with :mocha
|
||||
c.backtrace_clean_patterns = []
|
||||
|
||||
c.before(:each) do
|
||||
Jasmine::Headless::CacheableAction.enabled = false
|
||||
|
Loading…
Reference in New Issue
Block a user