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, #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_matching_path_for_extensions, #find_upwards, #join, #match_path_extname, #path_extnames, #path_parents, #paths_split, #relative_path?, #relative_path_from, #set_pipeline, #split_subpath, #stat, #stat_directory, #stat_sorted_tree, #stat_tree

Instance Method Details

#resolve(path, load_paths: config[:paths], accept: nil, pipeline: nil, base_path: nil) ⇒ 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.



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

def resolve(path, load_paths: config[:paths], accept: nil, pipeline: nil, base_path: nil)
  paths = load_paths

  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, path_pipeline, deps, index_alias = resolve_relative_path(paths, path, base_path, accept)
  else
    filename, type, path_pipeline, deps, index_alias = resolve_logical_path(paths, path, accept)
  end

  if filename
    uri = build_asset_uri(filename, type: type, pipeline: pipeline || path_pipeline, index_alias: index_alias)
  end

  return uri, deps
end

#resolve!(path, **kargs) ⇒ Object

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sprockets/resolve.rb', line 46

def resolve!(path, **kargs)
  uri, deps = resolve(path, **kargs)

  unless uri
    message = String.new("couldn't find file '#{path}'")

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

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

    load_paths = kargs[:load_paths] || config[:paths]
    message << "\nChecked in these paths: \n  #{ load_paths.join("\n  ") }"

    raise FileNotFound, message
  end

  return uri, deps
end