Sha256: c9dcd34b7933f9dbc60f6a991eac93467e736495d5768ee3430fad24c28c6a55

Contents?: true

Size: 1.32 KB

Versions: 20

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require 'active_support/current_attributes'

module Proscenium
  class Resolver < ActiveSupport::CurrentAttributes
    # TODO: cache this across requests in production.
    attribute :resolved

    # Resolve the given `path` to a URL path.
    #
    # @param path [String] Can be URL path, file system path, or bare specifier (ie. NPM package).
    # @return [String] URL path.
    def self.resolve(path) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
      self.resolved ||= {}

      self.resolved[path] ||= begin
        if path.start_with?('./', '../')
          raise ArgumentError, 'path must be an absolute file system or URL path'
        end

        if path.start_with?('@proscenium/')
          "/#{path}"
        elsif (gem = Proscenium.config.side_load_gems.find do |_, x|
                 path.start_with? "#{x[:root]}/"
               end)
          unless (package_name = gem[1][:package_name] || gem[0])
            # TODO: manually resolve the path without esbuild
            raise PathResolutionFailed, path
          end

          Builder.resolve "#{package_name}/#{path.delete_prefix("#{gem[1][:root]}/")}"
        elsif path.start_with?("#{Rails.root}/")
          path.delete_prefix Rails.root.to_s
        else
          Builder.resolve path
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
proscenium-0.11.0.pre.7-x86_64-linux lib/proscenium/resolver.rb
proscenium-0.11.0.pre.7-aarch64-linux lib/proscenium/resolver.rb
proscenium-0.11.0.pre.7-arm64-darwin lib/proscenium/resolver.rb
proscenium-0.11.0.pre.7-x86_64-darwin lib/proscenium/resolver.rb
proscenium-0.11.0.pre.6-x86_64-linux lib/proscenium/resolver.rb
proscenium-0.11.0.pre.6-aarch64-linux lib/proscenium/resolver.rb
proscenium-0.11.0.pre.6-arm64-darwin lib/proscenium/resolver.rb
proscenium-0.11.0.pre.6-x86_64-darwin lib/proscenium/resolver.rb
proscenium-0.11.0.pre.5-x86_64-linux lib/proscenium/resolver.rb
proscenium-0.11.0.pre.5-aarch64-linux lib/proscenium/resolver.rb
proscenium-0.11.0.pre.5-arm64-darwin lib/proscenium/resolver.rb
proscenium-0.11.0.pre.5-x86_64-darwin lib/proscenium/resolver.rb
proscenium-0.11.0.pre.4-x86_64-linux lib/proscenium/resolver.rb
proscenium-0.11.0.pre.4-arm64-darwin lib/proscenium/resolver.rb
proscenium-0.11.0.pre.3-arm64-darwin lib/proscenium/resolver.rb
proscenium-0.11.0.pre.3-x86_64-linux lib/proscenium/resolver.rb
proscenium-0.11.0.pre.2-x86_64-linux lib/proscenium/resolver.rb
proscenium-0.11.0.pre.2-aarch64-linux lib/proscenium/resolver.rb
proscenium-0.11.0.pre.2-arm64-darwin lib/proscenium/resolver.rb
proscenium-0.11.0.pre.2-x86_64-darwin lib/proscenium/resolver.rb