Skip to content

Instantly share code, notes, and snippets.

@naan
Forked from alexspeller/source_maps.rb
Last active December 14, 2015 13:48
Show Gist options
  • Select an option

  • Save naan/5096056 to your computer and use it in GitHub Desktop.

Select an option

Save naan/5096056 to your computer and use it in GitHub Desktop.
Monkey patch for Coffee Script v.1.6.2 Source Maps for Rails.
# config/initializers/source_maps.rb
if Rails.env.development?
module CoffeeScript
class SourceMapError < StandardError; end;
class << self
def compile script, options
script = script.read if script.respond_to?(:read)
if options.key?(:no_wrap) and !options.key?(:bare)
options[:bare] = options[:no_wrap]
else
options[:bare] = false
end
# adding source mapss option. (source maps option requires filename option.)
options[:sourceMap] = true
options[:filename] = options[:pathname].basename.to_s
ret = Source.context.call("CoffeeScript.compile", script, options)
map_dir = Rails.root.join("public/source_maps")
map_dir.mkpath
basename = options[:pathname].basename('.coffee')
map_file = map_dir.join "#{basename}.map"
coffee_file = map_dir.join "#{basename}.coffee"
# workaround for missing filename
source_map = JSON.load(ret["v3SourceMap"])
source_map["sources"][0] = options[:filename]
coffee_file.open('w') {|f| f.puts script }
map_file.open('w') {|f| f.puts source_map.to_json}
comment = "\n/*\n//@ sourceMappingURL=/source_maps/#{map_file.basename}\n*/\n"
return ret["js"] + comment
end
end
end
# Monkeypatch this method to include the scripts' pathname
require 'tilt/template'
module Tilt
class CoffeeScriptTemplate < Template
def evaluate(scope, locals, &block)
@output ||= CoffeeScript.compile(data, options.merge(:pathname => scope.pathname))
end
end
end
end
@markbates

Copy link
Copy Markdown

You should really make a patch for this in the coffee-rails gem.

@turadg

turadg commented Mar 7, 2013

Copy link
Copy Markdown

@naan beautiful! thanks!

@markbates it's coming: rails/coffee-rails#40

@bsodmike

bsodmike commented Mar 8, 2013

Copy link
Copy Markdown

@naan guess what! @markbates just turned this into a gem for rails https://github.com/markbates/coffee-rails-source-maps

💖

@turadg

turadg commented Mar 27, 2013

Copy link
Copy Markdown

anyone having trouble with this: it stopped working for me recently, until I upgraded from sprockets 2.2.2 to 2.2.2.backport1.

@naan

naan commented Mar 28, 2013

Copy link
Copy Markdown
Author

Updated for fixing issues of un-match line number and missing map file.

@OtherCroissant

Copy link
Copy Markdown

@bsomike @naan, @markbates Why is the gem of @markbates saying DO NOT USE THIS? Is there another repository which I should use?

@tsikov

tsikov commented Sep 6, 2015

Copy link
Copy Markdown

I also want to know what is the status of the project. All tutorials I read on source mapping coffeescript used the gem. Is it deprecated due to updated functionality in rails/sprockets/etc...? The repo doesn't give us any information.

@dawez

dawez commented Nov 28, 2015

Copy link
Copy Markdown

also would like to know the status of this ...so far with rails 4..2.4 and ruby 2.2.0 but never seen the maps generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment