Module: Sprockets::Resolve

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

Constant Summary

Constants included from PathUtils

PathUtils::SEPARATOR_PATTERN

Instance Method Summary collapse

Methods included from HTTPUtils

#find_best_mime_type_match, #find_best_q_match, #find_mime_type_matches, #find_q_matches, #match_mime_type?, #match_mime_type_keys, #parse_q_values

Methods included from PathDependencyUtils

#entries_with_dependencies, #file_digest_dependency_set, #stat_directory_with_dependencies, #stat_sorted_tree_with_dependencies

Methods included from URIUtils

#build_asset_uri, #build_file_digest_uri, #encode_uri_query_params, #join_file_uri, #join_uri, #parse_asset_uri, #parse_file_digest_uri, #parse_uri_query_params, #split_file_uri, #split_uri, #valid_asset_uri?

Methods included from PathUtils

#absolute_path?, #atomic_write, #directory?, #entries, #file?, #find_upwards, #match_path_extname, #path_extnames, #path_parents, #paths_split, #relative_path?, #split_subpath, #stat, #stat_directory, #stat_sorted_tree, #stat_tree

Instance Method Details

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

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

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

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

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

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sprockets/resolve.rb', line 23

def resolve(path, options = {})
  path = path.to_s
  paths = options[:load_paths] || config[:paths]
  accept = options[:accept]

  if valid_asset_uri?(path)
    uri, deps = resolve_asset_uri(path)
  elsif absolute_path?(path)
    filename, type, deps = resolve_absolute_path(paths, path, accept)
  elsif relative_path?(path)
    filename, type, pipeline, deps = resolve_relative_path(paths, path, options[:base_path], accept)
  else
    filename, type, pipeline, deps = resolve_logical_path(paths, path, accept)
  end

  if filename
    params = {}
    params[:type] = type if type
    params[:pipeline] = pipeline if pipeline
    params[:pipeline] = options[:pipeline] if options[:pipeline]
    uri = build_asset_uri(filename, params)
  end

  return uri, deps
end

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

Public: Same as resolve() but raises a FileNotFound exception instead of nil if no assets are found.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sprockets/resolve.rb', line 51

def resolve!(path, options = {})
  uri, deps = resolve(path, options.merge(compat: false))

  unless uri
    message = "couldn't find file '#{path}'"

    if relative_path?(path) && options[:base_path]
      load_path, _ = paths_split(config[:paths], options[:base_path])
      message << " under '#{load_path}'"
    end

    message << " with type '#{options[:accept]}'" if options[:accept]

    raise FileNotFound, message
  end

  return uri, deps
end