Class: Sprockets::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/squash/javascript/patches.rb

Direct Known Subclasses

BundledAsset

Instance Method Summary collapse

Instance Method Details

#sourcemapObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/squash/javascript/patches.rb', line 6

def sourcemap
  relative_path = if pathname.to_s.include?(Rails.root.to_s)
                    pathname.relative_path_from(Rails.root)
                  else
                    pathname
                  end.to_s
  # any extensions after the ".js" can be removed, because they will have
  # already been processed
  relative_path.gsub! /(?<=\.js)\..*$/, ''
  resource_path = [Rails.application.config.assets.prefix, logical_path].join('/')

  mappings = Array.new
  to_s.lines.each_with_index do |_, index|
    offset = SourceMap::Offset.new(index, 0)
    mappings << SourceMap::Mapping.new(relative_path, offset, offset)
  end
  SourceMap::Map.new(mappings, resource_path)
end