Skip to content

Commit

Permalink
Implement UglifyJS support. Closes rstacruz#18.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Sep 17, 2011
1 parent 98e8adc commit 102977e
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 3 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ compressors in the `assets` block:

``` ruby
assets {
js_compression :jsmin # :jsmin | :yui | :closure
js_compression :jsmin # :jsmin | :yui | :closure | :uglify
css_compression :simple # :simple | :sass | :yui | :sqwish
}
```
Expand Down Expand Up @@ -196,6 +196,33 @@ assets {
}
```

### UglifyJS compression

This uses the [UglifyJS](https://github.com/mishoo/UglifyJS) compressor to
compress your JavaScript. You will need to install the
[uglifier](http://rubygems.org/gems/uglifier) gem.

For options, refer to the [Uglifier
documentation](https://github.com/lautis/uglifier).

``` ruby
assets {
js_compression :uglify
js_compression :uglify, [options]
}
```

#### Gem
This depends on the `uglifier` gem. In your Gemfile, you will need to add it.
For Heroku support, you will need to add the `therubyracer-heroku` gem as well.

``` ruby
# Gemfile
gem 'uglifier'
gem "therubyracer-heroku", "0.8.1.pre3", require: false
```


Images
------

Expand Down
3 changes: 2 additions & 1 deletion examples/basic/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class App < Sinatra::Base
register Sinatra::AssetPack

assets do
js_compression :closure
#js_compression :closure
js_compression :uglify

js :main, '/js/main.js', [
'/js/vendor/*.js',
Expand Down
3 changes: 2 additions & 1 deletion lib/sinatra/assetpack/compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def compress(str, type, engine=nil, options={})

# Ensure that the engine exists.
klass = compressors[[type, engine]]
raise Error, "Registered engine #{engine} (#{type}) doesn't have an engine class." unless klass
raise Error, "Engine #{engine} (#{type}) doesn't exist." unless klass

# Ensure that the engine can support that type.
engine = klass.new
Expand Down Expand Up @@ -49,5 +49,6 @@ def register(type, engine, meth)
require "#{AssetPack::PREFIX}/assetpack/engines/sass"
require "#{AssetPack::PREFIX}/assetpack/engines/sqwish"
require "#{AssetPack::PREFIX}/assetpack/engines/closure"
require "#{AssetPack::PREFIX}/assetpack/engines/uglify"
end
end
12 changes: 12 additions & 0 deletions lib/sinatra/assetpack/engines/uglify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Sinatra::AssetPack
class UglifyEngine < Engine
def js(str, options={})
require 'uglifier'
Uglifier.compile str, options
rescue => e
nil
end
end

Compressor.register :js, :uglify, UglifyEngine
end
1 change: 1 addition & 0 deletions sinatra-assetpack.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "contest"
s.add_development_dependency "mocha"
s.add_development_dependency "stylus"
s.add_development_dependency "uglifier"
end
7 changes: 7 additions & 0 deletions test/app/app/js/ugly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(function() {
var kitsch = 1;
var noodle = 2;
var banana = 3;
console.log(noodle, banana);
})();

23 changes: 23 additions & 0 deletions test/uglifier_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require File.expand_path('../test_helper', __FILE__)

class UglifyTest < UnitTest
class App < UnitTest::App
register Sinatra::AssetPack

assets do
js_compression :uglify, :mangle => true
js :main, '/main.js', [
'/js/ugly.js'
]
end
end

def app
App
end

test "build" do
get '/main.js'
assert !body.include?("noodle")
end
end

0 comments on commit 102977e

Please sign in to comment.