Class: Pakyow::Assets::SourceMap

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pakyow/assets/source_map.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content = nil, file:) ⇒ SourceMap

Returns a new instance of SourceMap.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



26
27
28
# File 'lib/pakyow/assets/source_map.rb', line 26

def file
  @file
end

#sourcesObject (readonly)

Returns the value of attribute sources.



26
27
28
# File 'lib/pakyow/assets/source_map.rb', line 26

def sources
  @sources
end

#sources_contentObject (readonly)

Returns the value of attribute sources_content.



26
27
28
# File 'lib/pakyow/assets/source_map.rb', line 26

def sources_content
  @sources_content
end

Class Method Details

.mapping_url(path:, type:) ⇒ Object



13
14
15
16
17
18
19
20
# 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

Instance Method Details

#bytesizeObject



71
72
73
# File 'lib/pakyow/assets/source_map.rb', line 71

def bytesize
  read.bytesize
end

#each(&block) ⇒ Object



94
95
96
# File 'lib/pakyow/assets/source_map.rb', line 94

def each(&block)
  StringIO.new(read).each(&block)
end

#merge(other) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# 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_typeObject



67
68
69
# File 'lib/pakyow/assets/source_map.rb', line 67

def mime_type
  "application/octet-stream"
end

#readObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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