module Opal::Sprockets::SourceMapHeaderPatch

def self.included(base)

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

def self.inject!(prefix)

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

def self.prefix

def self.prefix
  @prefix
end

def self.prefix= val

def self.prefix= val
  @prefix = val
end

def headers_with_opal_source_maps(env, asset, length)

with a .rb or .opal extension in the extension chain.
Adds the source map header to all sprocket responses for assets
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