more sprockets improvements

This commit is contained in:
John Bintz 2011-11-20 11:56:25 -05:00
parent 61c8ed8828
commit ca8c655f00
4 changed files with 30 additions and 5 deletions

View File

@ -1,6 +1,7 @@
require 'jasmine-core' require 'jasmine-core'
require 'time' require 'time'
require 'multi_json' require 'multi_json'
require 'set'
module Jasmine::Headless module Jasmine::Headless
class FilesList class FilesList
@ -35,6 +36,7 @@ module Jasmine::Headless
@files = [] @files = []
@filtered_files = [] @filtered_files = []
@checked_dependency = Set.new
DEFAULT_FILES.each { |file| add_dependency('require', file, nil) } DEFAULT_FILES.each { |file| add_dependency('require', file, nil) }
@ -96,7 +98,12 @@ module Jasmine::Headless
end end
def add_dependencies(file, source_root) def add_dependencies(file, source_root)
TestFile.new(file, source_root).dependencies.each { |type, name| add_dependency(type, name, source_root) } TestFile.new(file, source_root).dependencies.each do |type, name|
if !@checked_dependency.include?(name)
@checked_dependency << name
add_dependency(type, name, source_root)
end
end
end end
EXTENSION_FILTER = %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$} EXTENSION_FILTER = %r{(#{(%w{.js .css} + Sprockets.engine_extensions).join('|')})$}
@ -124,7 +131,11 @@ module Jasmine::Headless
ok = (root == file) ok = (root == file)
ok ||= File.basename(path.gsub("#{file}.", '')).split('.').all? { |part| ".#{part}"[EXTENSION_FILTER] } ok ||= File.basename(path.gsub("#{file}.", '')).split('.').all? { |part| ".#{part}"[EXTENSION_FILTER] }
return [ File.expand_path(path), File.expand_path(dir) ] if ok expanded_path = File.expand_path(path)
if ok
return [ expanded_path, File.expand_path(dir) ]
end
end end
end end

View File

@ -24,6 +24,8 @@ module Jasmine::Headless
@dependencies = processor.directives.collect do |_, type, name| @dependencies = processor.directives.collect do |_, type, name|
if name[%r{^\.}] if name[%r{^\.}]
name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '') name = File.expand_path(File.join(File.dirname(path), name)).gsub(%r{^#{source_root}/}, '')
else
raise Sprockets::ArgumentError.new("require_tree needs a relative path: ./#{path}") if type == 'require_tree'
end end
[ type, name ] [ type, name ]

View File

@ -1,5 +1,5 @@
//= require 'jquery' //= require 'jquery'
//= require_tree 'things/templates' //= require_tree './templates'
//= require 'things/required' //= require 'things/required'
window.a = '1'; window.a = '1';

View File

@ -82,9 +82,11 @@ describe Jasmine::Headless::TestFile do
describe '#dependencies' do describe '#dependencies' do
include FakeFS::SpecHelpers include FakeFS::SpecHelpers
let(:directive) { 'require' }
before do before do
FileUtils.mkdir_p File.dirname(path) FileUtils.mkdir_p File.dirname(path)
File.open(path, 'wb') { |fh| fh.print "//= require '#{req}'\njavascript" } File.open(path, 'wb') { |fh| fh.print "//= #{directive} '#{req}'\njavascript" }
end end
context 'absolute' do context 'absolute' do
@ -92,9 +94,19 @@ describe Jasmine::Headless::TestFile do
subject { file.dependencies } subject { file.dependencies }
context 'require' do
it { should == [ [ 'require', req ] ] } it { should == [ [ 'require', req ] ] }
end end
context 'require_tree' do
let(:directive) { 'require_tree' }
it 'should raise an error' do
expect { subject }.to raise_error(Sprockets::ArgumentError, /relative/)
end
end
end
context 'relative' do context 'relative' do
let(:path) { File.join(source_root, 'subdir/subsubdir/path.js') } let(:path) { File.join(source_root, 'subdir/subsubdir/path.js') }