Class: Proscenium::Resolver

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

Class Method Summary collapse

Class Method Details

.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 both the manifest path and non-manifest path as an array. Only returns the resolved path if false (default).

Returns:

  • (String)

    URL path.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/proscenium/resolver.rb', line 16

def self.resolve(path, as_array: false)
  self.resolved ||= {}

  if path.start_with?('./', '../')
    raise ArgumentError, '`path` must be an absolute file system or URL path'
  end

  self.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}"]
                          elsif path.start_with?("#{Rails.root}/")
                            vpath = path.delete_prefix(Rails.root.to_s)
                            [Proscenium::Manifest[vpath], vpath]
                          else
                            [Proscenium::Manifest[path], Builder.resolve(path)]
                          end

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