class Pakyow::Assets::SourceMap

Attributes

file[R]
sources[R]
sources_content[R]

Public Class Methods

mapping_url(path:, type:) click to toggle source
# File lib/pakyow/assets/source_map.rb, line 13
def mapping_url(path:, type:)
  case type.to_sym
  when :css
    "\n/*# sourceMappingURL=#{path}.map */\n"
  when :javascript
    "\n//# sourceMappingURL=#{path}.map\n"
  end
end
new(content = nil, file:) click to toggle source
# File lib/pakyow/assets/source_map.rb, line 28
def initialize(content = nil, file:)
  @file = file

  @raw = content

  if content.is_a?(String)
    content = JSON.parse(content)
  end

  @sources = if content
    content["sources"].dup
  else
    []
  end

  @sources_content = if content
    content["sourcesContent"].to_a
  else
    []
  end

  @internal = if content.nil?
    ::SourceMap.new
  else
    ::SourceMap.from_json(content)
  end
end

Public Instance Methods

bytesize() click to toggle source
# File lib/pakyow/assets/source_map.rb, line 71
def bytesize
  read.bytesize
end
each(&block) click to toggle source
# File lib/pakyow/assets/source_map.rb, line 94
def each(&block)
  StringIO.new(read).each(&block)
end
merge(other) click to toggle source
# File lib/pakyow/assets/source_map.rb, line 56
def merge(other)
  tap do
    other.mappings.each do |mapping|
      @internal.add_mapping(mapping)
    end

    @sources.concat(other.sources)
    @sources_content.concat(other.sources_content)
  end
end
mime_type() click to toggle source
# File lib/pakyow/assets/source_map.rb, line 67
def mime_type
  "application/octet-stream"
end
read() click to toggle source
# File lib/pakyow/assets/source_map.rb, line 75
def read
  root = "/"
  map = @internal.as_json
  map["file"] = @file
  map["sourceRoot"] = root

  # The source_map gem reorders sources, so make sure that the sources content matches.
  #
  map["sourcesContent"] = map["sources"].map { |source|
    @sources_content[@sources.index(source)]
  }

  map["sources"].map! do |source|
    File.join(root, source)
  end

  map.to_json
end