Sha256: ad5d45f6ec546b8ad2cab08c577521ca1d288e7cb21b1f96cb7f93078ce8a3f2

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

require "propshaft/compiler"

class Propshaft::Compiler::SourceMappingUrls < Propshaft::Compiler
  SOURCE_MAPPING_PATTERN = %r{(//|/\*)# sourceMappingURL=(.+\.map)(\s*?\*\/)?\s*?\Z}

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

  private
    def asset_path(source_mapping_url, logical_path)
      source_mapping_url.gsub!(/^(.+\/)?#{url_prefix}\//, "")

      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(logical_path, resolved_path, comment_start, comment_end)
      if asset = load_path.find(resolved_path)
        "#{comment_start}# sourceMappingURL=#{url_prefix}/#{asset.digested_path}#{comment_end}"
      else
        Propshaft.logger.warn "Removed sourceMappingURL comment for missing asset '#{resolved_path}' from #{logical_path}"
        "#{comment_start}#{comment_end}"
      end
    end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
propshaft-1.1.0 lib/propshaft/compiler/source_mapping_urls.rb
propshaft-1.0.1 lib/propshaft/compiler/source_mapping_urls.rb
propshaft-1.0.0 lib/propshaft/compiler/source_mapping_urls.rb
propshaft-0.9.1 lib/propshaft/compiler/source_mapping_urls.rb