rack-pack 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,4 @@
1
1
  .bundle
2
- .rvmrc
3
2
  pkg/*
4
- *.gem
3
+ *.gem
4
+ Gemfile.lock
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :gemcutter
1
+ source :rubygems
2
2
  gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Peter Browne
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -4,7 +4,7 @@ Rack::Pack is a piece of Rack Middleware that packages and optionally compresses
4
4
 
5
5
  ### Why?
6
6
 
7
- I've tried a dozen different asset packaging solutions inlcuding AssetPackager, BundleFu, Jammit, Sprockets, etc...none of which were quite what I wanted. I didn't need any helpers, controllers, embedded images, rake tasks, or Yaml config files. I just wanted something to take my assets and package them into one file, and you're looking at it.
7
+ I've tried a dozen different asset packaging solutions including AssetPackager, BundleFu, Jammit, Sprockets, etc...none of which were quite what I wanted. I didn't need any helpers, controllers, embedded images, rake tasks, or Yaml config files. I just wanted something to take my assets and package them into one file, and you're looking at it.
8
8
 
9
9
  ## Installation
10
10
 
@@ -19,6 +19,7 @@ or in Rails:
19
19
 
20
20
  # Gemfile
21
21
  gem 'rack-pack'
22
+
22
23
  # config/application.rb
23
24
  config.middleware.use Rack::Pack
24
25
 
@@ -65,22 +66,19 @@ or in Rails:
65
66
 
66
67
  # Gemfile
67
68
  gem 'jsmin'
69
+
68
70
  # config/application.rb
69
71
  config.middleware.use Rack::Pack
70
72
  # would use JSMin
71
73
 
72
74
  To pass options to the javascript compressor just use the `:js_compressor` option:
73
75
 
74
- requrie 'packr'
76
+ require 'packr'
75
77
  use Rack::Pack, :js_compression => { :shrink_vars => true }
76
78
 
77
79
  By default, packages are only compressed in a production environment. If for some reason you want them to always be compressed, pass the `:always_compress` option:
78
80
 
79
81
  use Rack::Pack, :always_compress => true
80
-
81
- ## Advanced Usage
82
-
83
- TODO
84
82
 
85
83
  ## Heroku and other read-only filesystems
86
84
 
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ Bundler::GemHelper.install_tasks
3
3
 
4
4
  require 'rspec/core'
5
5
  require 'rspec/core/rake_task'
6
- Rspec::Core::RakeTask.new
6
+ RSpec::Core::RakeTask.new
7
7
 
8
8
  desc 'Open an irb session preloaded with this library'
9
9
  task :console do
data/lib/rack/pack.rb CHANGED
@@ -8,16 +8,36 @@ module Rack
8
8
  autoload :Version, 'rack/pack/version'
9
9
 
10
10
  DEFAULT_OPTIONS = {
11
- :public_dir => 'public',
12
- :always_update => false,
13
- :always_compress => false,
14
- :js_compression => {},
15
- :css_compression => {}
11
+ :public_dir => 'public',
12
+ :always_update => false,
13
+ :always_compress => false,
14
+ :js_compression => {},
15
+ :css_compression => {},
16
+ :environment => :development,
17
+ :add_default_packages => true
16
18
  }.freeze
17
19
 
18
20
  class << self
19
21
  attr_accessor :packages, :options, :environment
20
22
 
23
+ def configure(options = {})
24
+ self.packages = {}
25
+ self.options = DEFAULT_OPTIONS.dup
26
+
27
+ self.options.each_key do |key|
28
+ self.options[key] = options.delete(key) if options.key?(key)
29
+ end
30
+
31
+ unless self.options[:add_default_packages] == false
32
+ add_package 'javascripts/application.js', '{vendor,app,.}/javascripts/*.js'
33
+ add_package 'stylesheets/application.css', '{vendor,app,.}/stylesheets/*.css'
34
+ end
35
+
36
+ options.each do |to_file, from_files|
37
+ add_package(to_file, from_files)
38
+ end
39
+ end
40
+
21
41
  def production?
22
42
  self.environment.to_s == 'production'
23
43
  end
@@ -31,34 +51,27 @@ module Rack
31
51
  packages[output_file] = package_class.new(public_output_file, source_files)
32
52
  end
33
53
  end
54
+
55
+ def update_packages
56
+ Pack.packages.each_value do |package|
57
+ package.update if package.stale?
58
+ end
59
+ end
34
60
  end
35
61
 
36
62
  def initialize(app, options = {})
37
63
  @app = app
64
+ Pack.configure(options)
38
65
 
39
- Pack.packages = {}
40
- Pack.options = DEFAULT_OPTIONS.dup
41
- Pack.environment = if defined?(RAILS_ENV)
42
- RAILS_ENV # Rails 2
66
+ # Set environment based on Application environment
67
+ if defined?(RAILS_ENV)
68
+ Pack.environment = RAILS_ENV # Rails 2
43
69
  elsif defined?(Rails) && defined?(Rails.env)
44
- Rails.env # Rails 3
70
+ Pack.environment = Rails.env # Rails 3
45
71
  elsif defined?(app.settings) && defined?(app.settings.environment)
46
- app.settings.environment # Sinatra
72
+ Pack.environment = app.settings.environment # Sinatra
47
73
  elsif ENV.key?('RACK_ENV')
48
- ENV['RACK_ENV']
49
- else
50
- :development
51
- end
52
-
53
- Pack.options.each_key do |key|
54
- Pack.options[key] = options.delete(key) if options.key?(key)
55
- end
56
-
57
- Pack.add_package 'javascripts/application.js', '{vendor,app,.}/javascripts/*.js'
58
- Pack.add_package 'stylesheets/application.css', '{vendor,app,.}/stylesheets/*.css'
59
-
60
- options.each do |to_file, from_files|
61
- Pack.add_package(to_file, from_files)
74
+ Pack.environment = ENV['RACK_ENV']
62
75
  end
63
76
  end
64
77
 
@@ -70,9 +83,7 @@ module Rack
70
83
  protected
71
84
 
72
85
  def update_packages
73
- Pack.packages.each_value do |package|
74
- package.update if package.stale?
75
- end
86
+ Pack.update_packages
76
87
  @updated = true
77
88
  end
78
89
 
@@ -12,6 +12,8 @@ module Rack
12
12
  YUI::JavaScriptCompressor.new(options).compress(source)
13
13
  elsif defined?(Closure) and defined?(Closure::Compiler)
14
14
  Closure::Compiler.new(compression_options).compile(source)
15
+ elsif defined?(Uglifier)
16
+ Uglifier.new(compression_options).compile(source)
15
17
  else
16
18
  source
17
19
  end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class Pack
3
- VERSION = '0.2.2'
3
+ VERSION = '0.3.0'
4
4
  end
5
5
  end
data/rack-pack.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = 'Pete Browne'
10
10
  s.email = 'me@petebrowne.com'
11
- s.homepage = 'http://rubygems.org/gems/rack-pack'
11
+ s.homepage = 'http://github.com/petebrowne/rack-pack'
12
12
  s.summary = 'Rack Middleware for packaging assets such as javascripts and stylesheets.'
13
13
  s.description = 'Rack::Pack is a piece of Rack Middleware that packages and optionally compresses assets such as javascripts and stylesheets into single files. In a development environment, assets will be packaged on each request if there have been changes to the source files. In a production environment, assets will only be packaged one time, and only if there have been changes.'
14
14
 
@@ -16,13 +16,15 @@ Gem::Specification.new do |s|
16
16
  s.rubyforge_project = 'rack-pack'
17
17
 
18
18
  s.add_dependency 'rack', '~> 1.2.1'
19
- s.add_development_dependency 'rspec', '~> 2.0.0'
20
- s.add_development_dependency 'activesupport', '~> 3.0.0'
19
+ s.add_development_dependency 'rspec', '~> 2.6.0'
20
+ s.add_development_dependency 'activesupport', '~> 3.0.7'
21
+ s.add_development_dependency 'i18n', '~> 0.5.0'
21
22
  s.add_development_dependency 'test-construct', '~> 1.2.0'
22
23
  s.add_development_dependency 'jsmin', '~> 1.0.1'
23
24
  s.add_development_dependency 'packr', '~> 3.1.0'
24
25
  s.add_development_dependency 'yui-compressor', '~> 0.9.1'
25
26
  s.add_development_dependency 'closure-compiler', '~> 0.3.2'
27
+ s.add_development_dependency 'uglifier', '~> 0.5.3'
26
28
  s.add_development_dependency 'rainpress', '~> 1.0.0'
27
29
 
28
30
  s.files = `git ls-files`.split("\n")
@@ -72,6 +72,21 @@ describe Rack::Pack::Javascript do
72
72
  end
73
73
  end
74
74
  end
75
+
76
+ context 'when uglifier is required' do
77
+ it 'compresses using Uglifier' do
78
+ reveal_const :Uglifier do
79
+ within_construct do |c|
80
+ c.file 'input.js', '1'
81
+
82
+ compressor = double(:uglifier)
83
+ compressor.should_receive(:compile).with('1').and_return('1')
84
+ Uglifier.should_receive(:new).with({}).and_return(compressor)
85
+ Rack::Pack::Javascript.new('output.js', 'input.js').compile
86
+ end
87
+ end
88
+ end
89
+ end
75
90
  end
76
91
  end
77
92
  end
@@ -14,6 +14,66 @@ describe Rack::Pack do
14
14
  end
15
15
  alias_method :request, :request_for
16
16
 
17
+ describe '.configure' do
18
+ it 'sets the given options' do
19
+ Rack::Pack.configure(:public_dir => '_site', :always_compress => true, :environment => :production)
20
+ Rack::Pack.options.should == {
21
+ :public_dir => '_site',
22
+ :always_update => false,
23
+ :always_compress => true,
24
+ :js_compression => {},
25
+ :css_compression => {},
26
+ :environment => :production,
27
+ :add_default_packages => true
28
+ }
29
+ end
30
+
31
+ it 'adds default packages' do
32
+ Rack::Pack.configure
33
+ Rack::Pack.packages['javascripts/application.js'].should be_an_instance_of(Rack::Pack::Javascript)
34
+ Rack::Pack.packages['stylesheets/application.css'].should be_an_instance_of(Rack::Pack::Stylesheet)
35
+ end
36
+
37
+ it 'adds given packages' do
38
+ Rack::Pack.configure('main.js' => %w(vendor/javascripts/file-1.js app/javascripts/file-2.js))
39
+ Rack::Pack.packages['main.js'].should be_an_instance_of(Rack::Pack::Javascript)
40
+ end
41
+
42
+ context 'with :add_default_packages => false' do
43
+ it 'does not add default packages' do
44
+ Rack::Pack.configure(:add_default_packages => false)
45
+ Rack::Pack.packages['javascripts/application.js'].should be_nil
46
+ Rack::Pack.packages['stylesheets/application.css'].should be_nil
47
+ end
48
+ end
49
+ end
50
+
51
+ context '.update_packages' do
52
+ it 'packs javascripts' do
53
+ within_construct do |c|
54
+ c.file 'vendor/javascripts/file-1.js', '1'
55
+ c.file 'javascripts/file-2.js', '2'
56
+ c.file 'javascripts/file-3.js', '3'
57
+
58
+ Rack::Pack.configure
59
+ Rack::Pack.update_packages
60
+ File.read('public/javascripts/application.js').should == '123'
61
+ end
62
+ end
63
+
64
+ it 'packs stylesheets' do
65
+ within_construct do |c|
66
+ c.file 'vendor/stylesheets/file-1.css', '1'
67
+ c.file 'stylesheets/file-2.css', '2'
68
+ c.file 'stylesheets/file-3.css', '3'
69
+
70
+ Rack::Pack.configure
71
+ Rack::Pack.update_packages
72
+ File.read('public/stylesheets/application.css').should == '123'
73
+ end
74
+ end
75
+ end
76
+
17
77
  context 'with default settings' do
18
78
  it 'packs javascripts' do
19
79
  within_construct do |c|
@@ -188,7 +248,7 @@ describe Rack::Pack do
188
248
  end
189
249
  end
190
250
 
191
- context 'with :always_update on' do
251
+ context 'with :always_update => true' do
192
252
  it 'packs the files on each request' do
193
253
  within_construct do |c|
194
254
  c.file 'app/javascripts/file.js', '1'
data/spec/spec_helper.rb CHANGED
@@ -13,11 +13,12 @@ require 'jsmin'
13
13
  require 'packr'
14
14
  require 'yui/compressor'
15
15
  require 'closure-compiler'
16
+ require 'uglifier'
16
17
  require 'rainpress'
17
18
  require 'rack/pack'
18
19
 
19
20
  $hidden_consts = {}
20
- [ :JSMin, :Packr, :YUI, :Closure, :Rainpress ].each do |const|
21
+ [ :JSMin, :Packr, :YUI, :Closure, :Uglifier, :Rainpress ].each do |const|
21
22
  $hidden_consts[const] = Object.const_get(const)
22
23
  Object.send :remove_const, const
23
24
  end
metadata CHANGED
@@ -2,12 +2,12 @@
2
2
  name: rack-pack
3
3
  version: !ruby/object:Gem::Version
4
4
  hash: 19
5
- prerelease: false
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 2
10
- version: 0.2.2
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pete Browne
@@ -15,10 +15,10 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-11 00:00:00 -06:00
19
- default_executable:
18
+ date: 2011-05-18 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
21
+ name: rack
22
22
  version_requirements: &id001 !ruby/object:Gem::Requirement
23
23
  none: false
24
24
  requirements:
@@ -31,43 +31,59 @@ dependencies:
31
31
  - 1
32
32
  version: 1.2.1
33
33
  requirement: *id001
34
- name: rack
35
34
  prerelease: false
36
35
  type: :runtime
37
36
  - !ruby/object:Gem::Dependency
37
+ name: rspec
38
38
  version_requirements: &id002 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
42
42
  - !ruby/object:Gem::Version
43
- hash: 15
43
+ hash: 23
44
44
  segments:
45
45
  - 2
46
+ - 6
46
47
  - 0
47
- - 0
48
- version: 2.0.0
48
+ version: 2.6.0
49
49
  requirement: *id002
50
- name: rspec
51
50
  prerelease: false
52
51
  type: :development
53
52
  - !ruby/object:Gem::Dependency
53
+ name: activesupport
54
54
  version_requirements: &id003 !ruby/object:Gem::Requirement
55
55
  none: false
56
56
  requirements:
57
57
  - - ~>
58
58
  - !ruby/object:Gem::Version
59
- hash: 7
59
+ hash: 9
60
60
  segments:
61
61
  - 3
62
62
  - 0
63
- - 0
64
- version: 3.0.0
63
+ - 7
64
+ version: 3.0.7
65
65
  requirement: *id003
66
- name: activesupport
67
66
  prerelease: false
68
67
  type: :development
69
68
  - !ruby/object:Gem::Dependency
69
+ name: i18n
70
70
  version_requirements: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ hash: 11
76
+ segments:
77
+ - 0
78
+ - 5
79
+ - 0
80
+ version: 0.5.0
81
+ requirement: *id004
82
+ prerelease: false
83
+ type: :development
84
+ - !ruby/object:Gem::Dependency
85
+ name: test-construct
86
+ version_requirements: &id005 !ruby/object:Gem::Requirement
71
87
  none: false
72
88
  requirements:
73
89
  - - ~>
@@ -78,12 +94,12 @@ dependencies:
78
94
  - 2
79
95
  - 0
80
96
  version: 1.2.0
81
- requirement: *id004
82
- name: test-construct
97
+ requirement: *id005
83
98
  prerelease: false
84
99
  type: :development
85
100
  - !ruby/object:Gem::Dependency
86
- version_requirements: &id005 !ruby/object:Gem::Requirement
101
+ name: jsmin
102
+ version_requirements: &id006 !ruby/object:Gem::Requirement
87
103
  none: false
88
104
  requirements:
89
105
  - - ~>
@@ -94,12 +110,12 @@ dependencies:
94
110
  - 0
95
111
  - 1
96
112
  version: 1.0.1
97
- requirement: *id005
98
- name: jsmin
113
+ requirement: *id006
99
114
  prerelease: false
100
115
  type: :development
101
116
  - !ruby/object:Gem::Dependency
102
- version_requirements: &id006 !ruby/object:Gem::Requirement
117
+ name: packr
118
+ version_requirements: &id007 !ruby/object:Gem::Requirement
103
119
  none: false
104
120
  requirements:
105
121
  - - ~>
@@ -110,12 +126,12 @@ dependencies:
110
126
  - 1
111
127
  - 0
112
128
  version: 3.1.0
113
- requirement: *id006
114
- name: packr
129
+ requirement: *id007
115
130
  prerelease: false
116
131
  type: :development
117
132
  - !ruby/object:Gem::Dependency
118
- version_requirements: &id007 !ruby/object:Gem::Requirement
133
+ name: yui-compressor
134
+ version_requirements: &id008 !ruby/object:Gem::Requirement
119
135
  none: false
120
136
  requirements:
121
137
  - - ~>
@@ -126,12 +142,12 @@ dependencies:
126
142
  - 9
127
143
  - 1
128
144
  version: 0.9.1
129
- requirement: *id007
130
- name: yui-compressor
145
+ requirement: *id008
131
146
  prerelease: false
132
147
  type: :development
133
148
  - !ruby/object:Gem::Dependency
134
- version_requirements: &id008 !ruby/object:Gem::Requirement
149
+ name: closure-compiler
150
+ version_requirements: &id009 !ruby/object:Gem::Requirement
135
151
  none: false
136
152
  requirements:
137
153
  - - ~>
@@ -142,12 +158,28 @@ dependencies:
142
158
  - 3
143
159
  - 2
144
160
  version: 0.3.2
145
- requirement: *id008
146
- name: closure-compiler
161
+ requirement: *id009
147
162
  prerelease: false
148
163
  type: :development
149
164
  - !ruby/object:Gem::Dependency
150
- version_requirements: &id009 !ruby/object:Gem::Requirement
165
+ name: uglifier
166
+ version_requirements: &id010 !ruby/object:Gem::Requirement
167
+ none: false
168
+ requirements:
169
+ - - ~>
170
+ - !ruby/object:Gem::Version
171
+ hash: 13
172
+ segments:
173
+ - 0
174
+ - 5
175
+ - 3
176
+ version: 0.5.3
177
+ requirement: *id010
178
+ prerelease: false
179
+ type: :development
180
+ - !ruby/object:Gem::Dependency
181
+ name: rainpress
182
+ version_requirements: &id011 !ruby/object:Gem::Requirement
151
183
  none: false
152
184
  requirements:
153
185
  - - ~>
@@ -158,8 +190,7 @@ dependencies:
158
190
  - 0
159
191
  - 0
160
192
  version: 1.0.0
161
- requirement: *id009
162
- name: rainpress
193
+ requirement: *id011
163
194
  prerelease: false
164
195
  type: :development
165
196
  description: Rack::Pack is a piece of Rack Middleware that packages and optionally compresses assets such as javascripts and stylesheets into single files. In a development environment, assets will be packaged on each request if there have been changes to the source files. In a production environment, assets will only be packaged one time, and only if there have been changes.
@@ -174,7 +205,7 @@ files:
174
205
  - .gitignore
175
206
  - .rspec
176
207
  - Gemfile
177
- - Gemfile.lock
208
+ - LICENSE
178
209
  - README.md
179
210
  - Rakefile
180
211
  - lib/rack-pack.rb
@@ -189,8 +220,7 @@ files:
189
220
  - spec/rack/pack/stylesheet_spec.rb
190
221
  - spec/rack/pack_spec.rb
191
222
  - spec/spec_helper.rb
192
- has_rdoc: true
193
- homepage: http://rubygems.org/gems/rack-pack
223
+ homepage: http://github.com/petebrowne/rack-pack
194
224
  licenses: []
195
225
 
196
226
  post_install_message:
@@ -221,7 +251,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
221
251
  requirements: []
222
252
 
223
253
  rubyforge_project: rack-pack
224
- rubygems_version: 1.3.7
254
+ rubygems_version: 1.7.1
225
255
  signing_key:
226
256
  specification_version: 3
227
257
  summary: Rack Middleware for packaging assets such as javascripts and stylesheets.
data/Gemfile.lock DELETED
@@ -1,45 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- rack-pack (0.2.2)
5
- rack (~> 1.2.1)
6
-
7
- GEM
8
- remote: http://rubygems.org/
9
- specs:
10
- activesupport (3.0.1)
11
- closure-compiler (0.3.3)
12
- diff-lcs (1.1.2)
13
- jsmin (1.0.1)
14
- oyster (0.9.4)
15
- packr (3.1.0)
16
- oyster
17
- rack (1.2.1)
18
- rainpress (1.0)
19
- rspec (2.0.1)
20
- rspec-core (~> 2.0.1)
21
- rspec-expectations (~> 2.0.1)
22
- rspec-mocks (~> 2.0.1)
23
- rspec-core (2.0.1)
24
- rspec-expectations (2.0.1)
25
- diff-lcs (>= 1.1.2)
26
- rspec-mocks (2.0.1)
27
- rspec-core (~> 2.0.1)
28
- rspec-expectations (~> 2.0.1)
29
- test-construct (1.2.0)
30
- yui-compressor (0.9.1)
31
-
32
- PLATFORMS
33
- ruby
34
-
35
- DEPENDENCIES
36
- activesupport (~> 3.0.0)
37
- closure-compiler (~> 0.3.2)
38
- jsmin (~> 1.0.1)
39
- packr (~> 3.1.0)
40
- rack (~> 1.2.1)
41
- rack-pack!
42
- rainpress (~> 1.0.0)
43
- rspec (~> 2.0.0)
44
- test-construct (~> 1.2.0)
45
- yui-compressor (~> 0.9.1)