Class: SolidErrors::Frontend::SourceMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_errors/frontend/source_mapper.rb

Overview

Resolves the URL of a served asset back to the source file it was built from.

No source maps are involved and none are needed: with importmap + Propshaft there is no bundling or minification, so line numbers in the served asset match the source file exactly. All that has to be undone is the digest Propshaft splices into the filename (application-abc12345.js).

Returning an absolute path under Rails.root is what lets an error backend recognise the frame as application code and show the surrounding source.

Constant Summary collapse

MAX_CACHE_SIZE =
500

Instance Method Summary collapse

Constructor Details

#initialize(host: nil) ⇒ SourceMapper

Returns a new instance of SourceMapper.



18
19
20
21
# File 'lib/solid_errors/frontend/source_mapper.rb', line 18

def initialize(host: nil)
  @host = host
  @cache = {}
end

Instance Method Details

#resolve(url) ⇒ String?

Returns absolute path of the source file, if it can be found.

Returns:

  • (String, nil)

    absolute path of the source file, if it can be found



24
25
26
27
28
29
30
31
32
# File 'lib/solid_errors/frontend/source_mapper.rb', line 24

def resolve(url)
  return nil if url.nil?

  @cache.fetch(url) do
    path = lookup(url)
    @cache.clear if @cache.size >= MAX_CACHE_SIZE
    @cache[url] = path
  end
end