Class: Packr::SourceMap::V3Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/packr/source_map.rb

Constant Summary collapse

BASE64 =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
SHIFT =
5
MASK =
2**SHIFT
BASE =
MASK - 1
TEMPLATE =
<<JSON
{
  "version": 3,
  "file": <%= File.basename(@source_map.generated_file).inspect %>,
  "sourceRoot": "",
  "sources": <%= @source_map.sources.inspect %>,
  "names": <%= @source_map.names.inspect %>,
  "mappings": "<%= mappings %>"
}
JSON

Instance Method Summary collapse

Constructor Details

#initialize(source_map) ⇒ V3Encoder

Returns a new instance of V3Encoder.



230
231
232
# File 'lib/packr/source_map.rb', line 230

def initialize(source_map)
  @source_map = source_map
end

Instance Method Details

#encode(number) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/packr/source_map.rb', line 283

def encode(number)
  bits = number.abs
  bits <<= 1
  bits |= 1 if number < 0
  string = ''
  while bits >= MASK
    index = (bits & BASE) | MASK
    string << BASE64[index].chr
    bits >>= SHIFT
  end
  string << BASE64[bits].chr
  string
end

#mappingsObject



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/packr/source_map.rb', line 238

def mappings
  max_line = @source_map.segments.map { |s| s.generated_line }.max
  previous_segment = nil
  previous_name = nil
  
  (0..max_line).inject('') do |mappings, line_no|
    segments = @source_map.segments.
               select { |s| s.generated_line == line_no }.
               sort_by { |s| s.generated_column }
    
    previous_column = nil
    segment_strings = []
    
    segments.each do |segment|
      segment_strings << serialize_segment(segment, previous_segment, previous_column, previous_name)
      
      previous_segment = segment
      previous_column  = segment.generated_column
      previous_name    = segment.source_name || previous_name
    end
    
    mappings << segment_strings.join(',') << ';'
  end
end

#serializeObject



234
235
236
# File 'lib/packr/source_map.rb', line 234

def serialize
  ERB.new(TEMPLATE).result(binding).strip
end

#serialize_segment(segment, previous, previous_column, previous_name) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/packr/source_map.rb', line 263

def serialize_segment(segment, previous, previous_column, previous_name)
  pvalues = [
    previous_column ? previous_column : 0,
    previous ? @source_map.sources.index(previous.source_file) : 0,
    previous ? previous.source_line : 0,
    previous ? previous.source_column : 0,
    previous_name ? @source_map.names.index(previous_name) : 0
  ]
  numbers = [
    segment.generated_column - pvalues[0],
    @source_map.sources.index(segment.source_file) - pvalues[1],
    segment.source_line - pvalues[2],
    segment.source_column - pvalues[3]
  ]
  if segment.source_name
    numbers << @source_map.names.index(segment.source_name) - pvalues[4]
  end
  numbers.map { |n| encode(n) } * ''
end