Sha256: 6d1dd7754476a31cdd4262576d1874750dedace7f0443d60d03a8544960a7cf9

Contents?: true

Size: 926 Bytes

Versions: 1

Compression:

Stored size: 926 Bytes

Contents

# frozen_string_literal: true

class Propshaft::Compilers::SourceMappingUrls
  attr_reader :assembly

  SOURCE_MAPPING_PATTERN = /\/\/# sourceMappingURL=(.*\.map)/

  def initialize(assembly)
    @assembly = assembly
  end

  def compile(logical_path, input)
    input.gsub(SOURCE_MAPPING_PATTERN) { source_mapping_url(asset_path($1, logical_path)) }
  end

  private
    def asset_path(source_mapping_url, logical_path)
      if logical_path.dirname.to_s == "."
        source_mapping_url
      else
        logical_path.dirname.join(source_mapping_url).to_s
      end
    end

    def source_mapping_url(resolved_path)
      if asset = assembly.load_path.find(resolved_path)
        "//# sourceMappingURL=#{assembly.config.prefix}/#{asset.digested_path}"
      else
        Propshaft.logger.warn "Removed sourceMappingURL comment for missing asset '#{resolved_path}' from #{resolved_path}"
        nil
      end
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
propshaft-0.3.0 lib/propshaft/compilers/source_mapping_urls.rb