Module: Sprockets::Resolve

Includes:
URIUtils
Included in:
Base, Loader
Defined in:
lib/sprockets/resolve.rb

Instance Method Summary collapse

Methods included from URIUtils

#build_asset_uri, #parse_asset_uri, #valid_asset_uri?

Instance Method Details

#locate(path, options = {}) ⇒ Object

Public: Find Asset URI for given a logical path by searching the environment's load paths.

locate("application.js")
# => "file:///path/to/app/javascripts/application.js?content_type=application/javascript"

An accept content type can be given if the logical path doesn't have a format extension.

locate("application", accept: "application/javascript")
# => "file:///path/to/app/javascripts/application.coffee?content_type=application/javascript"

The String Asset URI is returned or nil if no results are found.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sprockets/resolve.rb', line 56

def locate(path, options = {})
  path = path.to_s
  accept = options[:accept]
  skip_bundle = options.key?(:bundle) ? !options[:bundle] : false

  available_encodings = self.encodings.keys + ['identity']
  encoding = find_best_q_match(options[:accept_encoding], available_encodings)

  paths = options[:load_paths] || self.paths

  if valid_asset_uri?(path)
    return path
  elsif absolute_path?(path)
    path = File.expand_path(path)
    if paths_split(paths, path) && file?(path)
      mime_type = parse_path_extnames(path)[1]
      _type = resolve_transform_type(mime_type, accept)
      if !accept || _type
        filename = path
        type = _type
      end
    end
  else
    logical_name, mime_type, _ = parse_path_extnames(path)
    parsed_accept = parse_accept_options(mime_type, accept)

    if parsed_accept.empty?
      return
    end

    transformed_accepts = expand_transform_accepts(parsed_accept)
    filename, mime_type = resolve_under_paths(paths, logical_name, mime_type, transformed_accepts)
    type = resolve_transform_type(mime_type, parsed_accept) if filename
  end

  if filename
    encoding = nil if encoding == 'identity'
    build_asset_uri(filename, type: type, skip_bundle: skip_bundle, encoding: encoding)
  end
end

#resolve(path, options = {}) ⇒ Object

Public: Finds the absolute path for a given logical path by searching the environment's load paths.

resolve("application.js")
# => "/path/to/app/javascripts/application.js"

An accept content type can be given if the logical path doesn't have a format extension.

resolve("application", accept: "application/javascript")
# => "/path/to/app/javascripts/application.js"

The String path is returned or nil if no results are found.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sprockets/resolve.rb', line 20

def resolve(path, options = {})
  logical_name, mime_type, _ = parse_path_extnames(path)

  paths = options[:load_paths] || self.paths

  if absolute_path?(path)
    path = File.expand_path(path)
    if paths_split(paths, path) && file?(path)
      if accept = options[:accept]
        find_best_q_match(accept, [path]) do |candidate, matcher|
          match_mime_type?(mime_type || "application/octet-stream", matcher)
        end
      else
        path
      end
    end
  else
    accepts = parse_accept_options(mime_type, options[:accept])
    filename, _ = resolve_under_paths(paths, logical_name, mime_type, accepts)
    filename
  end
end