rake-minify 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -23,19 +23,24 @@ Open your Rakefile and add:
23
23
  end
24
24
  end
25
25
 
26
+ This task minify the file "path/to/your/dir/source.js" into the file "output.js".
27
+
26
28
  == Minify multiple JavaScript files
27
29
 
28
30
  Open your Rakefile and add:
29
31
 
30
32
  Rake::Minify.new(:minify_multiple) do
31
33
  dir("path/to/your/dir") do # we specify only the source directory
32
- group("output-multi.js") do
33
- add("first.js") # the output file name is full path
34
+ group("output-multi.js") do # the output file name is full path
35
+ add("first.js")
34
36
  add("second.js")
35
37
  end
36
38
  end
37
39
  end
38
40
 
41
+ This task packs and minify the files "path/to/your/dir/first.js" and
42
+ "path/to/your/dir/second.js" into the file "output-multi.js".
43
+
39
44
  == Combine only multiple JavaScript files
40
45
 
41
46
  Open your Rakefile and add:
@@ -49,6 +54,29 @@ Open your Rakefile and add:
49
54
  end
50
55
  end
51
56
 
57
+ == CoffeeScript support
58
+
59
+ The rake-minify gem now supports even CoffeeScript files to be compiled,
60
+ minified and bundled, just include them like javascript files.
61
+
62
+ CoffeeScript support requires the coffe-script gem, so install the gem:
63
+
64
+ gem install coffee-script
65
+
66
+ Or if your are using Bundler:
67
+
68
+ echo "gem 'coffee-script'" >> Gemfile
69
+ bundle install
70
+
71
+ Then open your Rakefile and add:
72
+
73
+ Rake::Minify.new(:minify_single) do
74
+ dir("path/to/your/dir") do # we specify only the source directory
75
+ add("output.js", "source.coffee") # the coffee file is compiled and minified
76
+ end
77
+ end
78
+
79
+
52
80
  == Contributing to rake-minify
53
81
 
54
82
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/Rakefile CHANGED
@@ -22,6 +22,7 @@ Jeweler::Tasks.new do |gem|
22
22
  gem.add_development_dependency 'test_notifier', '~> 0.3.6'
23
23
  gem.add_development_dependency 'autotest', '~> 4.4'
24
24
  gem.add_development_dependency "rcov", ">= 0"
25
+ gem.add_development_dependency "coffee-script", ">= 2.2.0"
25
26
  end
26
27
  Jeweler::RubygemsDotOrgTasks.new
27
28
 
@@ -34,6 +35,9 @@ end
34
35
  RSpec::Core::RakeTask.new(:rcov) do |spec|
35
36
  spec.pattern = 'spec/**/*_spec.rb'
36
37
  spec.rcov = true
38
+ spec.rcov_opts = ["--text-summary", "--exclude","lib\/rspec,bin\/rspec,lib\/rcov," +
39
+ "spec,diff-lcs,lib\/cucumber,lib\/gherkin,cucumber,features,rake-0,coffee,json,execjs,jsmin"]
40
+
37
41
  end
38
42
 
39
43
  require 'cucumber/rake/task'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -0,0 +1,15 @@
1
+ Feature: Compile minify and bundle CoffeeScript files
2
+ In order to build awesome javascript applications
3
+ A ruby programmer
4
+ Wants to minify its coffeescripts using rake
5
+
6
+ Scenario: Minify single javascript
7
+ Given we want to minify the js file "c-a.coffee" into "c-a.min.js"
8
+ When I run rake minify
9
+ Then "c-a.min.js" should be minified
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"
14
+ When I run rake minify
15
+ Then "app.js" should include "c-a.js" and "b.js"
@@ -0,0 +1 @@
1
+ alert("hello a");
@@ -0,0 +1,2 @@
1
+
2
+ alert "hello a"
@@ -0,0 +1 @@
1
+ alert("hello a");
@@ -46,3 +46,19 @@ Feature: Minify Javascripts
46
46
  And we want to minify the js file "b.js" into "app.min.js"
47
47
  When I run rake minify
48
48
  Then "app.min.js" should include "a.min.js" and "b.min.js"
49
+
50
+ Scenario: Minify multiple javascripts with an inner directory
51
+ 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"
54
+ When I run rake minify
55
+ Then "app.min.js" should include "a.min.js" and "b.min.js"
56
+
57
+ @wip
58
+ Scenario: Minify multiple javascripts with a basedir and an inner directory
59
+ Given the basedir "base"
60
+ 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"
63
+ When I run rake minify
64
+ Then "app.min.js" should include "a.min.js" and "b.min.js"
@@ -11,7 +11,7 @@ When /^I run rake minify$/ do
11
11
  end
12
12
 
13
13
  Then /^"([^"]*)" should be minified$/ do |file|
14
- open(File.join(@dir, basedir, file)) do |result|
14
+ open(File.join(@dir, file)) do |result|
15
15
  open(File.join(File.dirname(__FILE__), "..", "js-expected", file)) do |expected|
16
16
  (result.read + "\n").should == expected.read
17
17
  end
@@ -34,7 +34,7 @@ Then /^"([^"]*)" should include "([^"]*)" and "([^"]*)"$/ do |result, source1, s
34
34
  end
35
35
  end
36
36
 
37
- open(File.join(@dir, basedir, result)) do |result|
37
+ open(File.join(@dir, result)) do |result|
38
38
  content = result.read
39
39
  sources.each { |s| content.should include(s.gsub(/\n$/,'')) } # this is unfortunate :/
40
40
  end
@@ -56,3 +56,6 @@ Given /^I have a "([^"]*)" rake task$/ do |arg1|
56
56
  pre << "task #{arg1} "
57
57
  end
58
58
 
59
+ Given /^the inner directory "([^"]*)"$/ do |dir|
60
+ indir(dir)
61
+ end
@@ -31,21 +31,27 @@ def generate_rakefile
31
31
  <%= pre.join("\n") %>
32
32
 
33
33
  Rake::Minify.new(<%= @name_for_generation %>) do
34
- <% if basedir.size > 0 %>
35
- dir(File.join(File.dirname(__FILE__), <%= basedir.inspect %>)) do
34
+ <% if basedir %>
35
+ dir(<%= basedir.inspect %>) do
36
36
  <% end %>
37
37
  <% groups.values.each do |group| %>
38
38
  <% if group.sources.size == 1 %>
39
39
  add(<%= group.output.inspect %>, <%= group.sources.first.source.inspect %>)
40
40
  <% else %>
41
41
  group(<%= group.output.inspect %>) do |group|
42
+ <% if indir %>
43
+ dir(<%= indir.inspect %>) do
44
+ <% end %>
42
45
  <% group.sources.each do |element| %>
43
46
  add(<%= element.source.inspect %>, :minify => <%= element.minify %>)
44
47
  <% end %>
48
+ <% if indir %>
49
+ end
50
+ <% end %>
45
51
  end
46
52
  <% end %>
47
53
  <% end %>
48
- <% if basedir.size > 0 %>
54
+ <% if basedir %>
49
55
  end
50
56
  <% end %>
51
57
  end
@@ -54,15 +60,20 @@ EOF
54
60
  erb.result(binding)
55
61
  end
56
62
 
57
- def basedir(dir="")
63
+ def basedir(dir=nil)
58
64
  @basedir ||= dir
59
65
  end
60
66
 
67
+ def indir(dir=nil)
68
+ @indir ||= dir
69
+ end
70
+
61
71
  def run_task(task_name="minify")
62
72
  @dir = Dir.mktmpdir
63
- FileUtils.mkdir File.join(@dir, basedir) if basedir.size > 0
73
+ dest_dir = File.join(*([basedir, indir, ""].select { |e| e }))
74
+ FileUtils.mkdir_p File.join(@dir, dest_dir) if dest_dir.size > 0
64
75
  Dir.glob(File.join(File.dirname(__FILE__), "..", "js-sources", "*")) do |file|
65
- FileUtils.cp(file, File.join(@dir, basedir))
76
+ FileUtils.cp(file, File.join(@dir, dest_dir))
66
77
  end
67
78
  rakefile = File.join(@dir, "Rakefile")
68
79
  open(rakefile, "w") { |io| io << generate_rakefile }
data/lib/rake/minify.rb CHANGED
@@ -11,6 +11,7 @@ class Rake::Minify < Rake::TaskLib
11
11
  @sources = {}
12
12
  @dir = nil
13
13
  @config = block
14
+ @dir_stack = []
14
15
 
15
16
  desc "Minifies the specified javascripts" unless Rake.application.last_comment
16
17
  task name do
@@ -19,17 +20,19 @@ class Rake::Minify < Rake::TaskLib
19
20
  end
20
21
 
21
22
  def add(output, source, opts = { :minify => true })
22
- @sources[build_path(output)] = Source.new(build_path(source), opts[:minify])
23
+ add_source(output, Source.new(build_path(source), opts[:minify]))
23
24
  end
24
25
 
25
26
  def group(output, &block)
26
- @sources[build_path(output)] = Group.new(self, &block)
27
+ add_source(output, Group.new(self, &block))
27
28
  end
28
29
 
29
30
  def dir(dir, &block)
30
- @dir = dir
31
- instance_eval &block # to be configured like the pros
32
- @dir = nil
31
+ @dir_stack << dir
32
+ @dir = File.join(@dir_stack)
33
+ block.call #FIXME @dir should be a stack
34
+ @dir_stack.pop
35
+ @dir = File.join(@dir_stack)
33
36
  end
34
37
 
35
38
  def invoke
@@ -45,4 +48,9 @@ class Rake::Minify < Rake::TaskLib
45
48
  def build_path(file)
46
49
  @dir.nil? && file || File.join(@dir, file)
47
50
  end
51
+
52
+ private
53
+ def add_source(output, source)
54
+ @sources[output] = source
55
+ end
48
56
  end
@@ -17,5 +17,9 @@ class Rake::Minify
17
17
  def build
18
18
  @sources.map { |s| s.build }.join("\n")
19
19
  end
20
+
21
+ def dir(dir, &block)
22
+ parent.dir(dir, &block)
23
+ end
20
24
  end
21
25
  end
@@ -1,4 +1,3 @@
1
-
2
1
  class Rake::Minify::Source
3
2
  attr_reader :source, :minify
4
3
 
@@ -8,8 +7,27 @@ class Rake::Minify::Source
8
7
  end
9
8
 
10
9
  def build
10
+ coffee_script! if coffee?
11
+
11
12
  Kernel.open(source) do |input|
12
- minify && JSMin.minify(input.read).strip || input.read
13
+ output = input.read
14
+ output = CoffeeScript.compile(output, :bare => true) if coffee? #TODO make bare configurable
15
+ output = JSMin.minify(output).strip if minify
16
+ output
17
+ end
18
+ end
19
+
20
+ def coffee?
21
+ source =~ /\.coffee$/
22
+ end
23
+
24
+ private
25
+ def coffee_script!
26
+ begin
27
+ require 'coffee-script'
28
+ rescue
29
+ raise "Missing coffee-script gem"
30
+ exit 1
13
31
  end
14
32
  end
15
33
  end
data/rake-minify.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rake-minify}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Matteo Collina"]
12
- s.date = %q{2011-04-06}
12
+ s.date = %q{2011-04-17}
13
13
  s.description = %q{A rake task to minify javascripts}
14
14
  s.email = %q{matteo.collina@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -25,10 +25,14 @@ Gem::Specification.new do |s|
25
25
  "README.rdoc",
26
26
  "Rakefile",
27
27
  "VERSION",
28
+ "features/coffeescript.feature",
28
29
  "features/js-expected/a.min.js",
29
30
  "features/js-expected/b.min.js",
31
+ "features/js-expected/c-a.min.js",
30
32
  "features/js-sources/a.js",
31
33
  "features/js-sources/b.js",
34
+ "features/js-sources/c-a.coffee",
35
+ "features/js-sources/c-a.js",
32
36
  "features/minify.feature",
33
37
  "features/rake-interface.feature",
34
38
  "features/step_definitions/rake-minify_steps.rb",
@@ -67,6 +71,7 @@ Gem::Specification.new do |s|
67
71
  s.add_development_dependency(%q<test_notifier>, ["~> 0.3.6"])
68
72
  s.add_development_dependency(%q<autotest>, ["~> 4.4"])
69
73
  s.add_development_dependency(%q<rcov>, [">= 0"])
74
+ s.add_development_dependency(%q<coffee-script>, [">= 2.2.0"])
70
75
  else
71
76
  s.add_dependency(%q<jsmin>, ["~> 1.0.1"])
72
77
  s.add_dependency(%q<rspec>, ["~> 2.5.0"])
@@ -75,6 +80,7 @@ Gem::Specification.new do |s|
75
80
  s.add_dependency(%q<test_notifier>, ["~> 0.3.6"])
76
81
  s.add_dependency(%q<autotest>, ["~> 4.4"])
77
82
  s.add_dependency(%q<rcov>, [">= 0"])
83
+ s.add_dependency(%q<coffee-script>, [">= 2.2.0"])
78
84
  end
79
85
  else
80
86
  s.add_dependency(%q<jsmin>, ["~> 1.0.1"])
@@ -84,6 +90,7 @@ Gem::Specification.new do |s|
84
90
  s.add_dependency(%q<test_notifier>, ["~> 0.3.6"])
85
91
  s.add_dependency(%q<autotest>, ["~> 4.4"])
86
92
  s.add_dependency(%q<rcov>, [">= 0"])
93
+ s.add_dependency(%q<coffee-script>, [">= 2.2.0"])
87
94
  end
88
95
  end
89
96
 
@@ -53,5 +53,10 @@ class Rake::Minify
53
53
 
54
54
  subject.build.should == "aaaa"
55
55
  end
56
+
57
+ it "should expose its parent dir method" do
58
+ parent.should_receive(:dir).with("a_dir").and_yield("hello world")
59
+ (subject.dir("a_dir") { |a| a }).should == "hello world" #weird method to test this
60
+ end
56
61
  end
57
62
  end
@@ -22,5 +22,15 @@ class Rake::Minify
22
22
  subject.build.should == ' var a = "b" ;'
23
23
  end
24
24
  end
25
+
26
+ context "compile a coffeescript file" do
27
+ subject { Source.new("a_source.coffee", true) }
28
+
29
+ it "should open and minify the file" do
30
+ input = StringIO.new(' a = "b" ')
31
+ Kernel.stub!(:open).with("a_source.coffee").and_yield(input)
32
+ subject.build.should == "var a;a=\"b\";"
33
+ end
34
+ end
25
35
  end
26
36
  end
@@ -84,7 +84,29 @@ describe Rake::Minify do
84
84
 
85
85
  before :each do
86
86
  stub_open("the_dir/source",' var a = "b" ;')
87
- @output = stub_open("the_dir/output","", "w")
87
+ @output = stub_open("output","", "w")
88
+ end
89
+
90
+ it "should minify the input file when invoked" do
91
+ do_invoke
92
+ @output.string.should == "var a=\"b\";"
93
+ end
94
+ end
95
+
96
+ context "configured to minify a file in a deep directory" do
97
+ subject do
98
+ Rake::Minify.new do
99
+ dir("the_dir") do
100
+ dir("another_dir") do
101
+ add("output", "source")
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ before :each do
108
+ stub_open("the_dir/another_dir/source",' var a = "b" ;')
109
+ @output = stub_open("output","", "w")
88
110
  end
89
111
 
90
112
  it "should minify the input file when invoked" do
@@ -93,6 +115,32 @@ describe Rake::Minify do
93
115
  end
94
116
  end
95
117
 
118
+ context "configured to minify multiple files in a deep directory" do
119
+ subject do
120
+ Rake::Minify.new do
121
+ dir("the_dir") do
122
+ group("output") do
123
+ dir("another_dir") do
124
+ add("a")
125
+ end
126
+ add("b")
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ before :each do
133
+ stub_open("the_dir/another_dir/a",' var a = "hello" ;')
134
+ stub_open("the_dir/b",' var b = "hello2" ;')
135
+ @output = stub_open("output","", "w")
136
+ end
137
+
138
+ it "should minify the input file when invoked" do
139
+ do_invoke
140
+ @output.string.should == "var a=\"hello\";\nvar b=\"hello2\";"
141
+ end
142
+ end
143
+
96
144
  it "should accepts arguments" do
97
145
  Rake::Task.should_receive(:define_task).with(:a_task)
98
146
  Rake::Minify.new(:a_task) do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rake-minify
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matteo Collina
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-06 00:00:00 +02:00
18
+ date: 2011-04-17 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -125,6 +125,22 @@ dependencies:
125
125
  version: "0"
126
126
  type: :development
127
127
  version_requirements: *id007
128
+ - !ruby/object:Gem::Dependency
129
+ name: coffee-script
130
+ prerelease: false
131
+ requirement: &id008 !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ hash: 7
137
+ segments:
138
+ - 2
139
+ - 2
140
+ - 0
141
+ version: 2.2.0
142
+ type: :development
143
+ version_requirements: *id008
128
144
  description: A rake task to minify javascripts
129
145
  email: matteo.collina@gmail.com
130
146
  executables: []
@@ -143,10 +159,14 @@ files:
143
159
  - README.rdoc
144
160
  - Rakefile
145
161
  - VERSION
162
+ - features/coffeescript.feature
146
163
  - features/js-expected/a.min.js
147
164
  - features/js-expected/b.min.js
165
+ - features/js-expected/c-a.min.js
148
166
  - features/js-sources/a.js
149
167
  - features/js-sources/b.js
168
+ - features/js-sources/c-a.coffee
169
+ - features/js-sources/c-a.js
150
170
  - features/minify.feature
151
171
  - features/rake-interface.feature
152
172
  - features/step_definitions/rake-minify_steps.rb