Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge remote-tracking branch 'origin/CoffeeScriptRedux'

  • Loading branch information...
commit 2fd1e1970fc4f8f56b7d6c2dd6a7672080629b51 2 parents b26d081 + efcb74a
@netzpirat authored
View
5 CHANGELOG.md
@@ -1,5 +1,10 @@
# Changelog
+## Master
+
+- Add information about using CoffeeScript Redux.
+- Add `:source_map` option for CoffeeScript Redux.
+
## 1.2.1 - November 1, 2012
- Remove file creation notification.
View
2  Gemfile
@@ -17,6 +17,7 @@ end
require 'rbconfig'
if RbConfig::CONFIG['target_os'] =~ /darwin/i
+ gem 'rb-fsevent', :require => false
gem 'ruby_gntp', :require => false
gem 'rb-fsevent', :require => false
elsif RbConfig::CONFIG['target_os'] =~ /linux/i
@@ -24,6 +25,7 @@ elsif RbConfig::CONFIG['target_os'] =~ /linux/i
elsif RbConfig::CONFIG['target_os'] =~ /mswin|mingw/i
gem 'win32console', :require => false
gem 'rb-notifu', :require => false
+ gem 'wdm', :require => false
end
unless ENV['TRAVIS']
View
9 README.md
@@ -33,7 +33,8 @@ to install the `json` or `json_pure` gem. On Ruby 1.9, JSON is included in the s
## CoffeeScript
-Guard::CoffeeScript uses [Ruby CoffeeScript](https://github.com/josh/ruby-coffee-script/) to compile the CoffeeScripts,
+Guard::CoffeeScript uses [Ruby CoffeeScript](https://github.com/josh/ruby-coffee-script/) or
+[Ruby CoffeeScript Redux](https://github.com/samwgoldman/ruby-coffee-script-redux) to compile the CoffeeScripts,
that in turn uses [ExecJS](https://github.com/sstephenson/execjs) to pick the best runtime to evaluate the JavaScript.
* With CRuby you want to use a V8 JavaScript Engine or Mozilla SpiderMonkey.
@@ -42,6 +43,9 @@ that in turn uses [ExecJS](https://github.com/sstephenson/execjs) to pick the be
* On Linux or as a node.js developer you want to use Node.js (V8).
* On Windows you want to use Microsoft Windows Script Host.
+If you are using CoffeeScript Redux, you can enable the source map generation by setting the `:source_map` option to
+`true`.
+
## JavaScript runtimes
The following sections gives you a short overview of the available JavaScript runtimes and how to install it.
@@ -178,6 +182,9 @@ There following options can be passed to Guard::CoffeeScript:
:shallow => true # Do not create nested output directories.
# default: false
+:source_map => true # Do create the source map file.
+ # default: false
+
:hide_success => true # Disable successful compilation messages.
# default: false
View
4 lib/guard/coffeescript.rb
@@ -19,7 +19,8 @@ class CoffeeScript < Guard
:hide_success => false,
:noop => false,
:error_to_js => false,
- :all_on_start => false
+ :all_on_start => false,
+ :source_map => false
}
# Initialize Guard::CoffeeScript.
@@ -33,6 +34,7 @@ class CoffeeScript < Guard
# @option options [Boolean] :hide_success hide success message notification
# @option options [Boolean] :all_on_start generate all JavaScripts files on start
# @option options [Boolean] :noop do not generate an output file
+ # @option options [Boolean] :source_map generate the source map files
#
def initialize(watchers = [], options = {})
watchers = [] if !watchers
View
43 lib/guard/coffeescript/runner.rb
@@ -18,6 +18,7 @@ class << self
# @option options [Boolean] :shallow do not create nested directories
# @option options [Boolean] :hide_success hide success message notification
# @option options [Boolean] :noop do not generate an output file
+ # @option options [Boolean] :source_map generate the source map files
# @return [Array<Array<String>, Boolean>] the result for the compilation run
#
def run(files, watchers, options = { })
@@ -86,8 +87,9 @@ def compile_files(files, watchers, options)
directories.each do |directory, scripts|
scripts.each do |file|
begin
- content = compile(file, options)
- changed_files << write_javascript_file(content, file, directory, options)
+ js, map = compile(file, options)
+ changed_files << write_javascript_file(js, map, file, directory, options)
+
rescue => e
error_message = file + ': ' + e.message.to_s
@@ -102,17 +104,23 @@ def compile_files(files, watchers, options)
end
end
- [changed_files.compact, errors]
+ [changed_files.flatten.compact, errors]
end
- # Compile the CoffeeScripts
+ # Compile the CoffeeScript and generate the source map.
#
- # @param [String] file the CoffeeScript file
+ # @param [String] filename the CoffeeScript file n
# @param [Hash] options the options for the execution
+ # @option options [Boolean] :source_map generate the source map files
+ # @return [Array<String, String>] the JavaScript source and the source map
#
- def compile(file, options)
+ def compile(filename, options)
+ file = File.read(filename)
file_options = options_for_file(file, options)
- ::CoffeeScript.compile(File.read(file), file_options)
+ js = ::CoffeeScript.compile(file, file_options)
+ map = options[:source_map] ? ::CoffeeScript.compile(file, file_options.merge(:format => :map)) : nil
+
+ [js, map]
end
# Gets the CoffeeScript compilation options.
@@ -134,19 +142,30 @@ def options_for_file(file, options)
# Analyzes the CoffeeScript compilation output and creates the
# nested directories and writes the output file.
#
- # @param [String] content the JavaScript content
+ # @param [String] js the JavaScript content
+ # @param [String] map the source map content
# @param [String] file the CoffeeScript file name
# @param [String] directory the output directory
# @param [Hash] options the options for the execution
# @option options [Boolean] :noop do not generate an output file
+ # @return [String] the JavaScript file name
#
- def write_javascript_file(content, file, directory, options)
+ def write_javascript_file(js, map, file, directory, options)
directory = Dir.pwd if !directory || directory.empty?
- FileUtils.mkdir_p(File.expand_path(directory)) if !File.directory?(directory) && !options[:noop]
filename = javascript_file_name(file, directory)
- File.open(File.expand_path(filename), 'w') { |f| f.write(content) } if !options[:noop]
- filename
+ return filename if options[:noop]
+
+ FileUtils.mkdir_p(File.expand_path(directory)) if !File.directory?(directory)
+ File.open(File.expand_path(filename), 'w') { |f| f.write(js) }
+
+ if options[:source_map]
+ map_name = filename + '.map'
+ File.open(File.expand_path(map_name), 'w') { |f| f.write(map) }
+ [filename, map_name]
+ else
+ filename
+ end
end
# Calculates the output filename from the coffescript filename and
View
32 spec/guard/coffeescript/runner_spec.rb
@@ -69,6 +69,21 @@
runner.run(['src/a.coffee'], [watcher], { :output => 'target', :noop => true })
end
end
+
+ context 'with the :source_map option' do
+ it 'compiles the source map to the same dir like the file and replace .coffee with .js.map' do
+ FileUtils.should_receive(:mkdir_p).with("#{ @project_path }/src")
+ File.should_receive(:open).with("#{ @project_path }/src/a.js.map", 'w')
+ runner.run(['src/a.coffee'], [watcher], :source_map => true)
+ end
+
+ it 'compiles the source map to the same dir like the file and replace .js.coffee with .js.map' do
+ FileUtils.should_receive(:mkdir_p).with("#{ @project_path }/src")
+ File.should_receive(:open).with("#{ @project_path }/src/a.js.map", 'w')
+ runner.run(['src/a.js.coffee'], [watcher], :source_map => true)
+ end
+
+ end
end
context 'with the :bare option set to an array of filenames' do
@@ -94,7 +109,6 @@
::CoffeeScript.should_receive(:compile).with 'src/b.coffee', hash_including(:bare => false)
runner.run(['src/a.coffee', 'src/b.coffee'], [watcher], { :output => 'target', :bare => ['a.coffee'] })
end
-
end
context 'with the :shallow option set to false' do
@@ -105,6 +119,14 @@
File.should_receive(:open).with("#{ @project_path }/javascripts/x/y/a.js", 'w')
runner.run(['app/coffeescripts/x/y/a.coffee'], [watcher], { :output => 'javascripts', :shallow => false })
end
+
+ context 'with the :source_map option' do
+ it 'generates the source map to the output and creates nested directories' do
+ FileUtils.should_receive(:mkdir_p).with("#{ @project_path }/javascripts/x/y")
+ File.should_receive(:open).with("#{ @project_path }/javascripts/x/y/a.js.map", 'w')
+ runner.run(['app/coffeescripts/x/y/a.coffee'], [watcher], { :output => 'javascripts', :shallow => false, :source_map => true })
+ end
+ end
end
context 'with the :shallow option set to true' do
@@ -115,6 +137,14 @@
File.should_receive(:open).with("#{ @project_path }/javascripts/a.js", 'w')
runner.run(['app/coffeescripts/x/y/a.coffee'], [watcher], { :output => 'javascripts', :shallow => true })
end
+
+ context 'with the :source_map option' do
+ it 'generates the source map to the output without creating nested directories' do
+ FileUtils.should_receive(:mkdir_p).with("#{ @project_path }/javascripts")
+ File.should_receive(:open).with("#{ @project_path }/javascripts/a.js.map", 'w')
+ runner.run(['app/coffeescripts/x/y/a.coffee'], [watcher], { :output => 'javascripts', :shallow => true, :source_map => true })
+ end
+ end
end
context 'with compilation errors' do
View
13 spec/guard/coffeescript_spec.rb
@@ -36,6 +36,11 @@
it 'sets a default :all_on_start option' do
guard.options[:all_on_start].should be_false
end
+
+ it 'sets the provided :source_maps option' do
+ guard.options[:source_map].should be_false
+ end
+
end
context 'with other options than the default ones' do
@@ -44,7 +49,9 @@
:shallow => true,
:hide_success => true,
:all_on_start => true,
- :noop => true }) }
+ :noop => true,
+ :source_map => true
+ }) }
it 'sets the provided :bare option' do
guard.options[:bare].should be_true
@@ -65,6 +72,10 @@
it 'sets the provided :all_on_start option' do
guard.options[:all_on_start].should be_true
end
+
+ it 'sets the provided :source_maps option' do
+ guard.options[:source_map].should be_true
+ end
end
context 'with a input option' do
Please sign in to comment.
Something went wrong with that request. Please try again.