Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Serious implicit formats

Fixes #103
  • Loading branch information...
josh committed Jul 8, 2011
1 parent f571c0a commit fa333dca5300645dee805b5173390ab5f2b58cfa
@@ -30,18 +30,30 @@ def pretty_path
sub(/^#{Regexp.escape(environment.root)}\//, '')
end

# Returns the index location.
#
# "foo/bar.js"
# # => "foo/bar/index.js"
#
def index_path
if basename_without_extensions.to_s == 'index'
pathname.to_s
else
basename = "#{basename_without_extensions}/index#{extensions.join}"
pathname.dirname.to_s == '.' ? basename : pathname.dirname.join(basename).to_s
# Returns paths search the load path for.
def search_paths
paths = [pathname.to_s]

if format_extension && engine_extensions.empty?
environment.engine_formats(format_extension).each do |ext, engine|
paths << pathname.sub(format_extension, ext).to_s
end
end

if pathname.basename(extensions.join).to_s != 'index'
path_without_extensions = extensions.inject(pathname) { |p, ext| p.sub(ext, '') }
index_path = path_without_extensions.join("index#{extensions.join}").to_s

paths << index_path

if format_extension && engine_extensions.empty?
environment.engine_formats(format_extension).each do |ext, engine|
paths << index_path.sub(format_extension, ext).to_s
end
end
end

paths
end

# Reverse guess logical path for fully expanded path.
@@ -144,15 +156,6 @@ def path_with_fingerprint(digest)
end

private
# Returns basename alone.
#
# "foo/bar.js"
# # => "bar"
#
def basename_without_extensions
@pathname.basename(extensions.join)
end

# Returns implicit engine content type.
#
# `.coffee` files carry an implicit `application/javascript`
@@ -9,12 +9,6 @@ module Sprockets
# https://github.com/sstephenson/eco
#
class EcoTemplate < Tilt::Template
# Eco templates always produced compiled JS. We can set Tilt's
# default mime type hint.
def self.default_mime_type
'application/javascript'
end

# Autoload eco library. If the library isn't loaded, Tilt will produce
# a thread safetly warning. If you intend to use `.eco` files, you
# should explicitly require it.
@@ -8,12 +8,6 @@ module Sprockets
# https://github.com/sstephenson/ruby-ejs
#
class EjsTemplate < Tilt::Template
# EJS templates always produced compiled JS. We can set Tilt's
# default mime type hint.
def self.default_mime_type
'application/javascript'
end

# Autoload ejs library. If the library isn't loaded, Tilt will produce
# a thread safetly warning. If you intend to use `.ejs` files, you
# should explicitly require it.
@@ -48,6 +48,15 @@ def engines(ext = nil)
end
end

def engine_formats(ext = nil)
if ext
ext = Sprockets::Utils.normalize_extension(ext)
@engine_formats[ext]
else
deep_copy_hash(@engine_formats)
end
end

# Returns an `Array` of engine extension `String`s.
#
# environment.engine_extensions
@@ -64,6 +73,14 @@ def engine_extensions
def register_engine(ext, klass)
ext = Sprockets::Utils.normalize_extension(ext)
@engines[ext] = klass

if klass.respond_to?(:default_mime_type) && klass.default_mime_type
if format_ext = extension_for_mime_type(klass.default_mime_type)
@engine_formats[format_ext] << ext
end
end

klass
end

private
@@ -76,6 +93,7 @@ def deep_copy_hash(hash)
# Extend Sprockets module to provide global registry
extend Engines
@engines = {}
@engine_formats = Hash.new { |h, k| h[k] = [] }

# Cherry pick the default Tilt engines that make sense for
# Sprockets. We don't need ones that only generate html like HAML.
@@ -33,7 +33,8 @@ def initialize(root = ".")

@static_root = nil

@engines = Sprockets.engines
@engines = Sprockets.engines
@engine_formats = Sprockets.engine_formats
@trail.extensions.replace(engine_extensions)

@mime_types = {}
@@ -26,6 +26,7 @@ def initialize(environment)
@preprocessors = environment.preprocessors
@postprocessors = environment.postprocessors
@bundle_processors = environment.bundle_processors
@engine_formats = environment.engine_formats

# Initialize caches
@assets = {}
@@ -70,7 +70,8 @@ def extensions
def resolve(logical_path, options = {})
# If a block is given, preform an iterable search
if block_given?
trail.find(logical_path.to_s, attributes_for(logical_path).index_path, options) do |path|
args = attributes_for(logical_path).search_paths + [options]
trail.find(*args) do |path|
yield Pathname.new(path)
end
else
@@ -6,27 +6,42 @@ class TestAssetAttributes < Sprockets::TestCase
pathname(pathname(__FILE__).relativize_root).expand_root
end

test "index path" do
assert_equal "index", pathname("index").index_path
assert_equal "index.html", pathname("index.html").index_path
assert_equal "index.css", pathname("index.css").index_path
assert_equal "index.js", pathname("index.js").index_path
assert_equal "index.coffee", pathname("index.coffee").index_path
assert_equal "index.js.coffee", pathname("index.js.coffee").index_path

assert_equal "foo/index", pathname("foo").index_path
assert_equal "foo/index.html", pathname("foo.html").index_path
assert_equal "foo/index.js", pathname("foo.js").index_path
assert_equal "foo/index.coffee", pathname("foo.coffee").index_path
assert_equal "foo/index.js.coffee", pathname("foo.js.coffee").index_path

assert_equal "foo/bar/index", pathname("foo/bar").index_path
assert_equal "foo/bar/index.js", pathname("foo/bar.js").index_path
assert_equal "foo/bar/index.coffee", pathname("foo/bar.coffee").index_path
assert_equal "foo/bar/index.js.coffee", pathname("foo/bar.js.coffee").index_path

# Doesn't make much sense, but we need to return something
assert_equal "jquery/index.foo.js", pathname("jquery.foo.js").index_path
test "search paths" do
assert_equal ["index"],
pathname("index").search_paths
assert_equal ["index.html"],
pathname("index.html").search_paths
assert_equal ["index.css", "index.less", "index.sass", "index.scss"],
pathname("index.css").search_paths
assert_equal ["index.js", "index.coffee", "index.jst"],
pathname("index.js").search_paths
assert_equal ["index.coffee"], pathname("index.coffee").search_paths
assert_equal ["index.js.coffee"], pathname("index.js.coffee").search_paths

assert_equal ["foo", "foo/index"],
pathname("foo").search_paths
assert_equal ["foo.html", "foo/index.html"],
pathname("foo.html").search_paths
assert_equal ["foo.js", "foo.coffee", "foo.jst", "foo/index.js", "foo/index.coffee", "foo/index.jst"],
pathname("foo.js").search_paths
assert_equal ["foo.coffee", "foo/index.coffee"],
pathname("foo.coffee").search_paths
assert_equal ["foo.js.coffee", "foo/index.js.coffee"],
pathname("foo.js.coffee").search_paths

assert_equal ["foo/bar", "foo/bar/index"], pathname("foo/bar").search_paths
assert_equal ["foo/bar.js", "foo/bar.coffee", "foo/bar.jst", "foo/bar/index.js", "foo/bar/index.coffee", "foo/bar/index.jst"],
pathname("foo/bar.js").search_paths
assert_equal ["foo/bar.coffee", "foo/bar/index.coffee"], pathname("foo/bar.coffee").search_paths
assert_equal ["foo/bar.js.coffee", "foo/bar/index.js.coffee"], pathname("foo/bar.js.coffee").search_paths

assert_equal ["jquery.foo.coffee", "jquery/index.foo.coffee"],
pathname("jquery.foo.coffee").search_paths
assert_equal ["jquery.foo.js.coffee", "jquery/index.foo.js.coffee"],
pathname("jquery.foo.js.coffee").search_paths
assert_equal ["jquery.foo.js", "jquery.foo.coffee", "jquery.foo.jst",
"jquery/index.foo.js", "jquery/index.foo.coffee", "jquery/index.foo.jst"],
pathname("jquery.foo.js").search_paths
end

test "logical path" do
@@ -91,6 +91,8 @@ def self.test(name, &block)
@env.resolve("gallery.js").to_s
assert_equal fixture_path('default/gallery.js'),
@env.resolve(Pathname.new("gallery.js")).to_s
assert_equal fixture_path('default/coffee/foo.coffee'),
@env.resolve("coffee/foo.js").to_s
end

test "missing file raises an exception" do
@@ -107,6 +109,11 @@ def self.test(name, &block)
assert_equal "var Gallery = {};\n", @env[fixture_path("default/gallery.js")].to_s
end

test "find bundled asset with implicit format" do
assert_equal "(function() {\n var foo;\n foo = 'hello';\n}).call(this);\n",
@env["coffee/foo.js"].to_s
end

test "find static asset in environment" do
assert_equal "Hello world\n", @env["hello.txt"].to_s
end

0 comments on commit fa333dc

Please sign in to comment.
You can’t perform that action at this time.