Skip to content

Instantly share code, notes, and snippets.

@Xenofex
Last active January 18, 2016 11:12
Show Gist options
  • Save Xenofex/3abbcb5543763f0faae3 to your computer and use it in GitHub Desktop.
Save Xenofex/3abbcb5543763f0faae3 to your computer and use it in GitHub Desktop.
An initializer to enable source maps for coffee script in Rails 4.2.5/Sprockets 3.5.2
if Rails.env.development?
require "base64"
module Sprockets
module CoffeeScriptProcessor
DEFAULT_OPTIONS = { "sourceMap" => true }
def self.call(input)
data = input[:data]
input[:cache].fetch([self.cache_key, data]) do
result = Autoload::CoffeeScript.compile(data, DEFAULT_OPTIONS)
source_map = result['v3SourceMap']
parsed_source_map = MultiJson.decode(source_map)
parsed_source_map['sources'] = [File.basename(input[:filename])]
parsed_source_map['sourcesContent'] = [data]
result['js'] + "\n//# sourceMappingURL=data:application/json;base64," + Base64.strict_encode64(MultiJson.encode(parsed_source_map))
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment