Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Add compat flag to Context#resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Jan 24, 2015
1 parent 9a90a6c commit bdc0f7a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
52 changes: 34 additions & 18 deletions lib/sprockets/context.rb
Expand Up @@ -69,26 +69,30 @@ def metadata
#
attr_reader :content_type

# Given a logical path, `resolve` will find and return the fully
# expanded path. Relative paths will also be resolved. An optional
# `:content_type` restriction can be supplied to restrict the
# search.
# Public: Given a logical path, `resolve` will find and return an Asset URI.
# Relative paths will also be resolved. An accept type maybe given to
# restrict the search.
#
# resolve("foo.js")
# # => "/path/to/app/javascripts/foo.js"
# resolve("foo.js", compat: false)
# # => "file:///path/to/app/javascripts/foo.js?type=application/javascript"
#
# resolve("./bar.js")
# # => "/path/to/app/javascripts/bar.js"
# resolve("./bar.js", compat: false)
# # => "file:///path/to/app/javascripts/bar.js?type=application/javascript"
#
# path - String logical or absolute path
# options
# accept - String content accept type
# compat - Force 2.x plain filename return type. Else return Asset URI.
#
# Returns filename String in compat mode, otherwise returns an Asset URI
# string. 4.x will always return an Asset URI string.
def resolve(path, options = {})
path, _ = environment.parse_asset_uri(locate(path, options))
path
end
options[:compat] = true unless options.key?(:compat)

def locate(path, options = {})
if environment.valid_asset_uri?(path)
path
uri = path
else
# Deprecated: Use accept instead of content_type
options[:content_type] = self.content_type if options[:content_type] == :self
options[:accept] ||= options.delete(:content_type)

Expand All @@ -103,16 +107,28 @@ def locate(path, options = {})

uri || environment.fail_file_not_found(path, dirname: @dirname, load_path: @load_path, accept: options[:accept])
end

if options[:compat]
path, _ = environment.parse_asset_uri(uri)
path
else
uri
end
end

# Deprecated:
alias_method :new_resolve, :resolve

# `depend_on` allows you to state a dependency on a file without
# including it.
#
# This is used for caching purposes. Any changes made to
# the dependency file with invalidate the cache of the
# source file.
def depend_on(path)
@dependencies << @environment.build_file_digest_uri(resolve(path).to_s)
uri = resolve(path, compat: false)
filename, _ = environment.parse_asset_uri(uri)
@dependencies << @environment.build_file_digest_uri(filename)
nil
end

Expand All @@ -124,7 +140,7 @@ def depend_on(path)
# file. Unlike `depend_on`, this will include recursively include
# the target asset's dependencies.
def depend_on_asset(path)
if asset = @environment.load(locate(path))
if asset = @environment.load(resolve(path, compat: false))
@dependencies.merge(asset.metadata[:dependencies])
end
nil
Expand All @@ -140,15 +156,15 @@ def depend_on_asset(path)
# <%= require_asset "#{framework}.js" %>
#
def require_asset(path)
@required << locate(path, accept: @content_type, bundle: false)
@required << resolve(path, accept: @content_type, bundle: false, compat: false)
nil
end

# `stub_asset` blacklists `path` from being included in the bundle.
# `path` must be an asset which may or may not already be included
# in the bundle.
def stub_asset(path)
@stubbed << locate(path, accept: @content_type, bundle: false)
@stubbed << resolve(path, accept: @content_type, bundle: false, compat: false)
nil
end

Expand All @@ -158,7 +174,7 @@ def stub_asset(path)
#
# Returns an Asset or nil.
def link_asset(path)
if asset = @environment.load(locate(path))
if asset = @environment.load(resolve(path, compat: false))
@dependencies.merge(asset.metadata[:dependencies])
@links << asset.uri
end
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/asset/link/asset_uri.css.erb
@@ -1,4 +1,4 @@
<% uri = locate("POW.png") %>
<% uri = resolve("POW.png", :compat => false) %>
.logo {
background: url(<%= link_asset(uri).digest_path %>);
}
6 changes: 3 additions & 3 deletions test/fixtures/context/resolve_content_type.js.erb
@@ -1,3 +1,3 @@
<%= resolve("foo.js") %>,
<%= resolve("foo.js", :content_type => "application/javascript") %>,
<%= resolve("foo.js", :content_type => :self) %>
<%= resolve("foo.js", compat: true) %>,
<%= resolve("foo.js", :content_type => "application/javascript", compat: true) %>,
<%= resolve("foo.js", :content_type => :self, compat: true) %>
4 changes: 2 additions & 2 deletions test/test_context.rb
Expand Up @@ -92,7 +92,7 @@ def initialize(file, &block)
def render(context)
data = @data
data.gsub(/url\(\"(.+?)\"\)/) do
path = context.resolve($1)
path = context.resolve($1, compat: true)
context.depend_on(path)
data = Base64.encode64(File.open(path, "rb") { |f| f.read })
"url(data:image/png;base64,#{data})"
Expand All @@ -113,7 +113,7 @@ def render(context)

@env.register_preprocessor 'text/css', :data_uris do |context, data|
data.gsub(/url\(\"(.+?)\"\)/) do
path = context.resolve($1)
path = context.resolve($1, compat: true)
context.depend_on(path)
data = Base64.encode64(File.open(path, "rb") { |f| f.read })
"url(data:image/png;base64,#{data})"
Expand Down

0 comments on commit bdc0f7a

Please sign in to comment.