Class: Packr::SourceMap

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

Defined Under Namespace

Classes: Segment, V3Encoder

Constant Summary collapse

IDENTIFIER =
/[a-zA-Z_$][\w\$]*/
LINE_ENDING =
/\r\n|\r|\n/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script, options = {}) ⇒ SourceMap

Returns a new instance of SourceMap.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/packr/source_map.rb', line 9

def initialize(script, options = {})
  @header = options[:header]
  if @header
    @header += "\n" unless options[:minify] != false
    @header += "\n"
  else
    @header = ''
  end
  
  @source_code = script
  return if String === @source_code
  
  @generated_file = options[:output_file]
  @base_62        = options[:base62]
  @line_offset    = @base_62 ? 0 : @header.scan(LINE_ENDING).size
  @source_code    = ''
  @source_files   = []
  
  script.each do |section|
    @source_files << [@source_code.size, section[:source]]
    @source_code << section[:code] + "\n"
  end
  
  @source_files << [@source_code.size, nil]
  
  @tokens = tokenize(@source_code, true)
end

Instance Attribute Details

Returns the value of attribute footer.



7
8
9
# File 'lib/packr/source_map.rb', line 7

def footer
  @footer
end

#generated_fileObject (readonly)

Returns the value of attribute generated_file.



7
8
9
# File 'lib/packr/source_map.rb', line 7

def generated_file
  @generated_file
end

#headerObject (readonly)

Returns the value of attribute header.



7
8
9
# File 'lib/packr/source_map.rb', line 7

def header
  @header
end

#source_codeObject (readonly)

Returns the value of attribute source_code.



7
8
9
# File 'lib/packr/source_map.rb', line 7

def source_code
  @source_code
end

Instance Method Details

#==(other) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/packr/source_map.rb', line 131

def ==(other)
  return false unless Hash === other
  return false unless names == other[:names]
  return false unless sources == other[:sources]
  return false unless @segments == other[:segments]
  true
end

#append_metadata(script) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/packr/source_map.rb', line 90

def (script)
  code   = script.dup
  script = @header + script
  
  script = if enabled?
    @footer = "\n//@ sourceMappingURL=#{File.basename(filename)}"
    script + @footer
  else
    @footer = ''
    script
  end
  
  script.extend(StringExtension)
  script.source_map = self
  script.code       = code
  
  script
end

#enabled?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/packr/source_map.rb', line 37

def enabled?
  !!@generated_file
end

#filenameObject



121
122
123
# File 'lib/packr/source_map.rb', line 121

def filename
  @generated_file && "#{@generated_file}.map"
end

#namesObject



109
110
111
# File 'lib/packr/source_map.rb', line 109

def names
  @names_array ||= @names.to_a
end

#remove(sections) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/packr/source_map.rb', line 41

def remove(sections)
  return unless enabled?
  
  sections.each_with_index do |section, i|
    effect = section[2] - section[1]
    sections[(i+1)..-1].each { |moved| moved[0] += effect }
    
    range = section[0]...(section[0] + section[1])
    @tokens.delete_if { |t| range === t[:offset] }
    
    @tokens.each do |token|
      token[:offset] += effect if token[:offset] > section[0]
    end
  end
end

#segmentsObject



117
118
119
# File 'lib/packr/source_map.rb', line 117

def segments
  @segments_objects ||= @segments.map { |s| Segment.new(s) }
end

#sourcesObject



113
114
115
# File 'lib/packr/source_map.rb', line 113

def sources
  @sources ||= @source_files.map { |pair| pair.last }.compact.sort
end

#to_jsonObject Also known as: to_s



125
126
127
128
# File 'lib/packr/source_map.rb', line 125

def to_json
  return nil unless enabled?
  V3Encoder.new(self).serialize
end

#update(script) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/packr/source_map.rb', line 57

def update(script)
  return unless enabled?
  
  @segments = []
  @names = SortedSet.new
  
  tokenize(script, false).each_with_index do |token, i|
    source_token = @tokens[i]
    
    if source_token[:name] != token[:name]
      @names.add(source_token[:name])
      name = source_token[:name]
    else
      name = nil
    end
    
    @segments << {
      :line     => token[:line] + @line_offset,
      :column   => token[:column],
      :mapping  => {
        :source => source_token[:source],
        :line   => source_token[:line],
        :column => source_token[:column],
        :name   => name
      }
    }
  end
  
  if @generated_file and @base_62
    script << "\n//@ sourceURL=$$SOURCE_URL"
  end
end