Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement UglifyJS support. Closes #18.

  • Loading branch information...
commit 102977edfb9f357c8f7bc7b366027bfb6ebd131e 1 parent 98e8adc
@rstacruz rstacruz authored
View
29 README.md
@@ -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
}
```
@@ -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
------
View
3  examples/basic/app.rb
@@ -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',
View
3  lib/sinatra/assetpack/compressor.rb
@@ -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
@@ -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
View
12 lib/sinatra/assetpack/engines/uglify.rb
@@ -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
View
1  sinatra-assetpack.gemspec
@@ -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
View
7 test/app/app/js/ugly.js
@@ -0,0 +1,7 @@
+(function() {
+ var kitsch = 1;
+ var noodle = 2;
+ var banana = 3;
+ console.log(noodle, banana);
+})();
+
View
23 test/uglifier_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.