Module: Opal::Sprockets::SourceMapHeaderPatch

Defined in:
opal/lib/opal/sprockets/source_map_header_patch.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



17
18
19
20
21
# File 'opal/lib/opal/sprockets/source_map_header_patch.rb', line 17

def self.included(base)
  # Poor man's alias_method_chain :)
  base.send(:alias_method, :headers_without_opal_source_maps, :headers)
  base.send(:alias_method, :headers, :headers_with_opal_source_maps)
end

.inject!(prefix) ⇒ Object



23
24
25
26
27
28
# File 'opal/lib/opal/sprockets/source_map_header_patch.rb', line 23

def self.inject!(prefix)
  self.prefix = prefix
  unless ::Sprockets::Server.ancestors.include?(self)
    ::Sprockets::Server.send :include, self
  end
end

.prefixObject



30
31
32
# File 'opal/lib/opal/sprockets/source_map_header_patch.rb', line 30

def self.prefix
  @prefix
end

.prefix=(val) ⇒ Object



34
35
36
# File 'opal/lib/opal/sprockets/source_map_header_patch.rb', line 34

def self.prefix= val
  @prefix = val
end

Instance Method Details

#headers_with_opal_source_maps(env, asset, length) ⇒ Object

Adds the source map header to all sprocket responses for assets with a .rb or .opal extension in the extension chain.



8
9
10
11
12
13
14
15
# File 'opal/lib/opal/sprockets/source_map_header_patch.rb', line 8

def headers_with_opal_source_maps(env, asset, length)
  headers_without_opal_source_maps(env, asset, length).tap do |current_headers|
    if asset.pathname.to_s =~ /\.(rb|opal)\b/
      base_path = asset.logical_path.gsub('.js', '')
      current_headers['X-SourceMap'] = "#{::Opal::Sprockets::SourceMapHeaderPatch.prefix}/#{base_path}.map"
    end
  end
end