rake-minify 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rake-minify (0.2.1)
5
+ jsmin (~> 1.0.1)
6
+ rake-minify
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ ZenTest (4.5.0)
12
+ autotest (4.4.6)
13
+ ZenTest (>= 4.4.1)
14
+ builder (3.0.0)
15
+ coffee-script (2.2.0)
16
+ coffee-script-source
17
+ execjs
18
+ coffee-script-source (1.1.0)
19
+ cucumber (0.10.2)
20
+ builder (>= 2.1.2)
21
+ diff-lcs (>= 1.1.2)
22
+ gherkin (>= 2.3.5)
23
+ json (>= 1.4.6)
24
+ term-ansicolor (>= 1.0.5)
25
+ diff-lcs (1.1.2)
26
+ execjs (0.3.3)
27
+ multi_json (~> 1.0)
28
+ gherkin (2.3.7)
29
+ json (>= 1.4.6)
30
+ git (1.2.5)
31
+ jeweler (1.5.2)
32
+ bundler (~> 1.0.0)
33
+ git (>= 1.2.5)
34
+ rake
35
+ jsmin (1.0.1)
36
+ json (1.5.1)
37
+ multi_json (1.0.1)
38
+ notifier (0.1.3)
39
+ rake (0.8.7)
40
+ rcov (0.9.9)
41
+ rspec (2.5.0)
42
+ rspec-core (~> 2.5.0)
43
+ rspec-expectations (~> 2.5.0)
44
+ rspec-mocks (~> 2.5.0)
45
+ rspec-core (2.5.2)
46
+ rspec-expectations (2.5.0)
47
+ diff-lcs (~> 1.1.2)
48
+ rspec-mocks (2.5.0)
49
+ term-ansicolor (1.0.5)
50
+ test_notifier (0.3.6)
51
+ notifier
52
+ therubyracer (0.8.1)
53
+
54
+ PLATFORMS
55
+ ruby
56
+
57
+ DEPENDENCIES
58
+ autotest (~> 4.4)
59
+ coffee-script (>= 2.2.0)
60
+ cucumber
61
+ jeweler (~> 1.5.2)
62
+ rake-minify!
63
+ rcov
64
+ rspec (~> 2.5.0)
65
+ test_notifier (~> 0.3.6)
66
+ therubyracer
@@ -76,6 +76,19 @@ Then open your Rakefile and add:
76
76
  end
77
77
  end
78
78
 
79
+ === Bare switch
80
+
81
+ Since version 0.3.0 rake-minify support the configuration of the coffee script bare switch
82
+ (see http://jashkenas.github.com/coffee-script/#installation for details).
83
+ However in the 0.2.x series that flag defaulted to true, now it defaults to false.
84
+
85
+ You can specify it:
86
+
87
+ Rake::Minify.new(:minify_single) do
88
+ # the coffee file is compiled and minified
89
+ # without the top-level function safety wrapper.
90
+ add("output.js", "source.coffee", :bare => true)
91
+ end
79
92
 
80
93
  == Contributing to rake-minify
81
94
 
data/Rakefile CHANGED
@@ -23,6 +23,7 @@ Jeweler::Tasks.new do |gem|
23
23
  gem.add_development_dependency 'autotest', '~> 4.4'
24
24
  gem.add_development_dependency "rcov", ">= 0"
25
25
  gem.add_development_dependency "coffee-script", ">= 2.2.0"
26
+ gem.add_development_dependency "therubyracer"
26
27
  end
27
28
  Jeweler::RubygemsDotOrgTasks.new
28
29
 
@@ -43,7 +44,7 @@ end
43
44
  require 'cucumber/rake/task'
44
45
  Cucumber::Rake::Task.new(:features)
45
46
 
46
- task :default => [:spec, :features]
47
+ task :default => [:features, :spec]
47
48
 
48
49
  require 'rake/rdoctask'
49
50
  Rake::RDocTask.new do |rdoc|
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -3,13 +3,44 @@ Feature: Compile minify and bundle CoffeeScript files
3
3
  A ruby programmer
4
4
  Wants to minify its coffeescripts using rake
5
5
 
6
- Scenario: Minify single javascript
7
- Given we want to minify the js file "c-a.coffee" into "c-a.min.js"
6
+ Scenario: Minify single coffeescript
7
+ Given we want to add the file "c-a.coffee" into "c-a-wrapped.min.js"
8
8
  When I run rake minify
9
- Then "c-a.min.js" should be minified
9
+ Then "c-a-wrapped.min.js" should be minified
10
10
 
11
- Scenario: Combine multiple javascripts into a single file
12
- Given we want to combine the js file "c-a.coffee" into "app.js"
13
- And we want to combine the js file "b.js" into "app.js"
11
+ Scenario: Combine multiple coffeescripts into a single file
12
+ Given we want to add the file "c-a.coffee" into "app.js" with options:
13
+ | minify | false |
14
+ And we want to add the file "b.js" into "app.js" with options:
15
+ | minify | false |
14
16
  When I run rake minify
15
17
  Then "app.js" should include "c-a.js" and "b.js"
18
+
19
+ Scenario: Minify single javascript in bare mode
20
+ Given we want to add the file "c-a.coffee" into "c-a-bare.min.js" with options:
21
+ | bare | true |
22
+ When I run rake minify
23
+ Then "c-a-bare.min.js" should be minified
24
+
25
+ Scenario: Minify single javascript in wrapped mode
26
+ Given we want to add the file "c-a.coffee" into "c-a-wrapped.min.js" with options:
27
+ | bare | false |
28
+ When I run rake minify
29
+ Then "c-a-wrapped.min.js" should be minified
30
+
31
+ Scenario: Combine multiple coffeescripts into a single file in bare mode
32
+ Given we want to add the file "c-a.coffee" into "app.js" with options:
33
+ | bare | true |
34
+ And we want to add the file "b.js" into "app.js" with options:
35
+ | minify | false |
36
+ When I run rake minify
37
+ Then "app.js" should include "c-a-bare.min.js" and "b.js"
38
+
39
+ Scenario: Combine multiple coffeescripts into a single file in wrapped mode
40
+ Given we want to add the file "c-a.coffee" into "app.js" with options:
41
+ | bare | false |
42
+ | minify | false |
43
+ And we want to add the file "b.js" into "app.js" with options:
44
+ | minify | false |
45
+ When I run rake minify
46
+ Then "app.js" should include "c-a-wrapped.js" and "b.js"
@@ -1 +1 @@
1
- alert("hello a");
1
+ var a="hello a";
@@ -1 +1 @@
1
- alert("hello b");
1
+ var b="hello b";
@@ -0,0 +1 @@
1
+ var a;a="hello a";
@@ -0,0 +1 @@
1
+ (function(){var a;a="hello a";}).call(this);
@@ -1 +1 @@
1
- alert("hello a");
1
+ var a;a="hello a";
@@ -1,5 +1,5 @@
1
1
 
2
- alert( "hello a" );
2
+ var a = "hello a" ;
3
3
 
4
4
 
5
5
 
@@ -1,2 +1,2 @@
1
1
 
2
- alert( "hello b" );
2
+ var b = "hello b" ;
@@ -0,0 +1,2 @@
1
+ var a;
2
+ a = "hello a";
@@ -0,0 +1,5 @@
1
+ (function() {
2
+ var a;
3
+ a = "hello a";
4
+ }).call(this);
5
+
@@ -1,2 +1,2 @@
1
1
 
2
- alert "hello a"
2
+ a = "hello a"
@@ -1 +1,4 @@
1
- alert("hello a");
1
+ (function() {
2
+ var a;
3
+ a = "hello a";
4
+ }).call(this);
@@ -4,61 +4,75 @@ Feature: Minify Javascripts
4
4
  Wants to minify its javascripts using rake
5
5
 
6
6
  Scenario: Minify single javascript
7
- Given we want to minify the js file "a.js" into "a.min.js"
7
+ Given we want to add the file "a.js" into "a.min.js" with options:
8
+ | minify | true |
8
9
  When I run rake minify
9
10
  Then "a.min.js" should be minified
10
11
 
11
12
  Scenario: Combine multiple javascripts into a single file
12
- Given we want to combine the js file "a.js" into "app.js"
13
- And we want to combine the js file "b.js" into "app.js"
13
+ Given we want to add the file "a.js" into "app.js" with options:
14
+ | minify | false |
15
+ And we want to add the file "b.js" into "app.js" with options:
16
+ | minify | false |
14
17
  When I run rake minify
15
18
  Then "app.js" should include "a.js" and "b.js"
16
19
 
17
20
  Scenario: Minify multiple javascripts into a single file
18
- Given we want to minify the js file "a.js" into "app.min.js"
19
- And we want to minify the js file "b.js" into "app.min.js"
21
+ Given we want to add the file "a.js" into "app.min.js" with options:
22
+ | minify | true |
23
+ And we want to add the file "b.js" into "app.min.js"
20
24
  When I run rake minify
21
25
  Then "app.min.js" should include "a.min.js" and "b.min.js"
22
26
 
23
27
  Scenario: Minify multiple javascripts into multiple files
24
- Given we want to minify the js file "a.js" into "app.min.js"
25
- And we want to minify the js file "b.js" into "app.min.js"
26
- And we want to minify the js file "a.js" into "a.min.js"
28
+ Given we want to add the file "a.js" into "app.min.js"
29
+ And we want to add the file "b.js" into "app.min.js"
30
+ And we want to add the file "a.js" into "a.min.js"
27
31
  When I run rake minify
28
32
  Then "app.min.js" should include "a.min.js" and "b.min.js"
29
33
  And "a.min.js" should be minified
30
34
 
31
- Scenario: Combine and minify only one js file
32
- Given we want to combine the js file "a.js" into "app.js"
33
- And we want to minify the js file "b.js" into "app.js"
35
+ Scenario: Combine and minify only one file
36
+ Given we want to add the file "a.js" into "app.js" with options:
37
+ | minify | false |
38
+ And we want to add the file "b.js" into "app.js"
34
39
  When I run rake minify
35
40
  Then "app.js" should include "a.js" and "b.min.js"
36
41
 
37
42
  Scenario: Minify single javascript with basedir
38
43
  Given the basedir "public"
39
- And we want to minify the js file "a.js" into "a.min.js"
44
+ And we want to add the file "a.js" into "a.min.js"
40
45
  When I run rake minify
41
46
  Then "a.min.js" should be minified
42
47
 
43
48
  Scenario: Minify multiple javascripts with basedir
44
49
  Given the basedir "public"
45
- And we want to minify the js file "a.js" into "app.min.js"
46
- And we want to minify the js file "b.js" into "app.min.js"
50
+ And we want to add the file "a.js" into "app.min.js"
51
+ And we want to add the file "b.js" into "app.min.js"
47
52
  When I run rake minify
48
53
  Then "app.min.js" should include "a.min.js" and "b.min.js"
49
54
 
50
55
  Scenario: Minify multiple javascripts with an inner directory
51
56
  Given the inner directory "public"
52
- And we want to minify the js file "a.js" into "app.min.js"
53
- And we want to minify the js file "b.js" into "app.min.js"
57
+ And we want to add the file "a.js" into "app.min.js"
58
+ And we want to add the file "b.js" into "app.min.js"
54
59
  When I run rake minify
55
60
  Then "app.min.js" should include "a.min.js" and "b.min.js"
56
61
 
57
- @wip
58
62
  Scenario: Minify multiple javascripts with a basedir and an inner directory
59
63
  Given the basedir "base"
60
64
  And the inner directory "in"
61
- And we want to minify the js file "a.js" into "app.min.js"
62
- And we want to minify the js file "b.js" into "app.min.js"
65
+ And we want to add the file "a.js" into "app.min.js"
66
+ And we want to add the file "b.js" into "app.min.js"
63
67
  When I run rake minify
64
68
  Then "app.min.js" should include "a.min.js" and "b.min.js"
69
+
70
+ Scenario: Minify single javascript with a destination directory
71
+ Given we want to add the file "a.js" into "build/a.min.js"
72
+ When I run rake minify
73
+ Then "build/a.min.js" should include "a.min.js"
74
+
75
+ Scenario: Minify single javascript with a deep destination directory
76
+ Given we want to add the file "a.js" into "a/dir/a.min.js"
77
+ When I run rake minify
78
+ Then "a/dir/a.min.js" should include "a.min.js"
@@ -4,7 +4,7 @@ Feature: Rake Interface
4
4
  Wants to customize the minify task
5
5
 
6
6
  Background:
7
- Given we want to minify the js file "a.js" into "a.min.js"
7
+ Given we want to add the file "a.js" into "a.min.js"
8
8
 
9
9
  Scenario: Custom name for the task
10
10
  Given I have configured rake to minify at the ":my_minify" command
@@ -2,8 +2,12 @@ require 'fileutils'
2
2
  require 'tmpdir'
3
3
  require 'rake'
4
4
 
5
- Given /^we want to minify the js file "([^"]*)" into "([^"]*)"$/ do |file, output|
6
- add_to_groups(output, true, file)
5
+ Given /^we want to add the file "([^"]*)" into "([^"]*)"$/ do |file, output|
6
+ add_to_groups(output, file, nil)
7
+ end
8
+
9
+ Given /^we want to add the file "([^"]*)" into "([^"]*)" with options:$/ do |file, output, options|
10
+ add_to_groups(output, file, options.rows_hash)
7
11
  end
8
12
 
9
13
  When /^I run rake minify$/ do
@@ -18,12 +22,10 @@ Then /^"([^"]*)" should be minified$/ do |file|
18
22
  end
19
23
  end
20
24
 
21
- Given /^we want to combine the js file "([^"]*)" into "([^"]*)"$/ do |file, output|
22
- add_to_groups(output, false, file)
23
- end
24
-
25
- Then /^"([^"]*)" should include "([^"]*)" and "([^"]*)"$/ do |result, source1, source2|
26
- sources = [source1, source2].map do |s|
25
+ Then /^"([^"]*)" should include "([^"]*)"(?: and "([^"]*)")?$/ do |result, source1, source2|
26
+ sources = [source1]
27
+ sources << source2 if source2
28
+ sources.map! do |s|
27
29
  file = File.join(File.dirname(__FILE__), "..", "js-expected", s)
28
30
  unless File.exists? file
29
31
  file = File.join(File.dirname(__FILE__), "..", "js-sources", s)
@@ -1,4 +1,7 @@
1
1
 
2
+ require 'bundler'
3
+ Bundler.setup
4
+
2
5
  $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
3
6
  require 'rake-minify'
4
7
 
@@ -9,12 +9,12 @@ class Group
9
9
  @sources = []
10
10
  end
11
11
 
12
- def add(source, minify)
13
- @sources << GroupElement.new(source, minify)
12
+ def add(source, options)
13
+ @sources << GroupElement.new(source, options)
14
14
  end
15
15
  end
16
16
 
17
- GroupElement = Struct.new(:source, :minify)
17
+ GroupElement = Struct.new(:source, :options)
18
18
 
19
19
  def groups
20
20
  @groups ||= Hash.new do |h, k|
@@ -22,8 +22,12 @@ def groups
22
22
  end
23
23
  end
24
24
 
25
- def add_to_groups(output, minify, file)
26
- groups[output].add(file, minify)
25
+ def add_to_groups(output, file, options=nil)
26
+ options = options.keys.reduce({}) do |hash, key|
27
+ hash[key.to_sym] = eval(options[key])
28
+ hash
29
+ end if options
30
+ groups[output].add(file, options)
27
31
  end
28
32
 
29
33
  def generate_rakefile
@@ -36,14 +40,14 @@ Rake::Minify.new(<%= @name_for_generation %>) do
36
40
  <% end %>
37
41
  <% groups.values.each do |group| %>
38
42
  <% if group.sources.size == 1 %>
39
- add(<%= group.output.inspect %>, <%= group.sources.first.source.inspect %>)
43
+ add(<%= group.output.inspect %>, <%= group.sources.first.source.inspect %>, <%= group.sources.first.options.inspect %>)
40
44
  <% else %>
41
45
  group(<%= group.output.inspect %>) do |group|
42
46
  <% if indir %>
43
47
  dir(<%= indir.inspect %>) do
44
48
  <% end %>
45
49
  <% group.sources.each do |element| %>
46
- add(<%= element.source.inspect %>, :minify => <%= element.minify %>)
50
+ add(<%= element.source.inspect %>, <%= element.options.inspect %>)
47
51
  <% end %>
48
52
  <% if indir %>
49
53
  end
@@ -20,7 +20,7 @@ class Rake::Minify < Rake::TaskLib
20
20
  end
21
21
 
22
22
  def add(output, source, opts = { :minify => true })
23
- add_source(output, Source.new(build_path(source), opts[:minify]))
23
+ add_source(output, Source.new(build_path(source), opts))
24
24
  end
25
25
 
26
26
  def group(output, &block)
@@ -39,6 +39,8 @@ class Rake::Minify < Rake::TaskLib
39
39
  instance_eval &@config # to be configured like the pros
40
40
 
41
41
  @sources.each do |dest, source|
42
+ FileUtils.mkdir_p(File.dirname(dest))
43
+
42
44
  Kernel.open(dest, "w") do |output|
43
45
  output << source.build
44
46
  end
@@ -10,8 +10,9 @@ class Rake::Minify
10
10
  instance_eval &block if block
11
11
  end
12
12
 
13
- def add(source, opts={:minify => true})
14
- @sources << Source.new(parent.build_path(source), opts[:minify])
13
+ def add(source, opts=nil)
14
+ opts ||= { :minify => true }
15
+ @sources << Source.new(parent.build_path(source), opts)
15
16
  end
16
17
 
17
18
  def build
@@ -1,9 +1,11 @@
1
1
  class Rake::Minify::Source
2
2
  attr_reader :source, :minify
3
3
 
4
- def initialize(source, minify)
4
+ def initialize(source, options=nil)
5
+ options ||= {}
5
6
  @source = source
6
- @minify = minify
7
+ @minify = optional_boolean(options[:minify], true)
8
+ @coffee_bare = optional_boolean(options[:bare], false)
7
9
  end
8
10
 
9
11
  def build
@@ -11,7 +13,7 @@ class Rake::Minify::Source
11
13
 
12
14
  Kernel.open(source) do |input|
13
15
  output = input.read
14
- output = CoffeeScript.compile(output, :bare => true) if coffee? #TODO make bare configurable
16
+ output = CoffeeScript.compile(output, :bare => @coffee_bare) if coffee?
15
17
  output = JSMin.minify(output).strip if minify
16
18
  output
17
19
  end
@@ -27,7 +29,12 @@ class Rake::Minify::Source
27
29
  require 'coffee-script'
28
30
  rescue
29
31
  raise "Missing coffee-script gem"
30
- exit 1
31
32
  end
32
33
  end
34
+
35
+ def optional_boolean(value, default)
36
+ return true if value
37
+ return default if value.nil?
38
+ value
39
+ end
33
40
  end