Class: Proscenium::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/proscenium/resolver.rb

Class Method Summary collapse

Class Method Details

.resetObject



31
32
33
# File 'lib/proscenium/resolver.rb', line 31

def self.reset
  self.resolved = {}
end

.resolve(path, as_array: false) ⇒ String+

Resolve the given ‘path` to a fully qualified URL path.

Parameters:

  • path (String)

    URL path, file system path, or bare specifier (ie. NPM package).

  • as_array (Boolean) (defaults to: false)

    whether or not to return the manifest path, non-manifest path, and absolute file system path as an array. Only returns the resolved path if false (default).

Returns:

  • (String, Array<String>)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/proscenium/resolver.rb', line 13

def self.resolve(path, as_array: false)
  if path.start_with?('./', '../')
    raise ArgumentError, '`path` must be an absolute file system or URL path'
  end

  resolved[path] ||= if (gem = BundledGems.paths.find { |_, v| path.start_with? "#{v}/" })
                       vpath = path.sub(/^#{gem.last}/, "@rubygems/#{gem.first}")
                       [Proscenium::Manifest[vpath], "/node_modules/#{vpath}", path]
                     elsif path.start_with?("#{Rails.root}/")
                       vpath = path.delete_prefix(Rails.root.to_s)
                       [Proscenium::Manifest[vpath], vpath, path]
                     else
                       [Proscenium::Manifest[path], *Builder.resolve(path)]
                     end

  as_array ? resolved[path] : resolved[path][0] || resolved[path][1]
end