Skip to content

Instantly share code, notes, and snippets.

@sgruhier
Created September 8, 2012 14:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgruhier/3675564 to your computer and use it in GitHub Desktop.
Save sgruhier/3675564 to your computer and use it in GitHub Desktop.
Quick (and dirty) Guardfile for CoffeeScriptRedux and SourceMap

After having seen that CoffeeScriptRedux supports SourceMap, I did a quick and dirty Guardfile for the project I'm working on. Quick, because I did an inline Guardfile, Dirty because I dealt with paths by making symbolic links. I dont really know how paths should work in source maps.

If you have a better solution, please share !!

To setup CoffeeScriptRedux read this excellent post http://ryanflorence.com/2012/coffeescript-source-maps/

In my case I have coffeescript files in coffee directory and generated javascript files in js directory.

DEBUG = 1
module ::Guard
class Coffee < Guard
DEFAULT_OPTIONS = {compiler: 'coffee', input: 'coffee', output: 'js'}
def initialize(watchers = [], options = {})
watchers = [] if !watchers
defaults = DEFAULT_OPTIONS.clone
if options[:input]
defaults.merge!({ :output => options[:input] })
watchers << ::Guard::Watcher.new(%r{^#{ options[:input] }/(.+\.coffee)$})
end
super(watchers, defaults.merge(options))
end
def run_all
true
end
def run_on_change(paths)
paths.each do |path|
output = path.gsub(%r{^#{ options[:input] }/}, "#{options[:output]}/")
.gsub(/coffee$/, 'js')
# Generate JS file
FileUtils.mkdir_p File.dirname(output)
run_cmd "#{options[:compiler]} --js --input #{path} > #{output}"
# Generate MAP file
run_cmd "cd #{File.dirname(path)}; #{options[:compiler]} --source-map --input #{File.basename(path)} > #{File.realpath(output)}.map"
# Update JS file with sourceMappingURL
run_cmd "(echo; echo '//@ sourceMappingURL=#{File.basename(output)}.map') >> #{output}"
# Hack to be able to see coffee source file in Chrome
run_cmd "cd #{File.dirname(output)}; ln -s #{File.realpath(path)} . 2>/dev/null"
end
true
end
private
def run_cmd(cmd)
puts cmd if DEBUG
system cmd
end
end
end
# Available options: :input, :output, :compiler
guard 'coffee'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment