Skip to content

Commit ddddda1

Browse files
author
Pavel Pravosud
committed
added uglifierjs support
1 parent 45fd7dd commit ddddda1

8 files changed

Lines changed: 56 additions & 10 deletions

File tree

jammit.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
2929

3030
s.add_dependency 'yui-compressor', ['>= 0.9.3']
3131
s.add_dependency 'closure-compiler', ['>= 0.1.0']
32+
s.add_dependency 'uglifier', ['>= 0.3.0']
3233

3334
s.files = Dir['lib/**/*', 'bin/*', 'rails/*', 'jammit.gemspec', 'LICENSE', 'README']
3435
end

lib/jammit.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module Jammit
2222

2323
DEFAULT_JST_NAMESPACE = "window.JST"
2424

25-
AVAILABLE_COMPRESSORS = [:yui, :closure]
25+
AVAILABLE_COMPRESSORS = [:yui, :closure, :uglifier]
2626

2727
DEFAULT_COMPRESSOR = :yui
2828

lib/jammit/compressor.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,19 @@ class Compressor
4242
JST_END = "})();"
4343

4444
COMPRESSORS = {
45-
:yui => YUI::JavaScriptCompressor,
46-
:closure => Closure::Compiler
45+
:yui => YUI::JavaScriptCompressor,
46+
:closure => Closure::Compiler,
47+
:uglifier => ::Uglifier
4748
}
4849

4950
DEFAULT_OPTIONS = {
50-
:yui => {:munge => true},
51-
:closure => {}
51+
:yui => {:munge => true},
52+
:closure => {},
53+
:uglifier => {}
5254
}
53-
54-
# Creating a compressor initializes the internal YUI Compressor from
55-
# the "yui-compressor" gem, or the internal Closure Compiler from the
56-
# "closure-compiler" gem.
55+
56+
# The css comressor if always YUI Compressor. Js compression can be provided with
57+
# YUI Compressor, Google Closure Compiler or UglifyJS
5758
def initialize
5859
@css_compressor = YUI::CssCompressor.new(Jammit.css_compressor_options || {})
5960
flavor = Jammit.javascript_compressor || Jammit::DEFAULT_COMPRESSOR
@@ -69,7 +70,7 @@ def compress_js(paths)
6970
else
7071
js = concatenate(paths - jst_paths) + compile_jst(jst_paths)
7172
end
72-
Jammit.compress_assets ? @js_compressor.compress(js) : js
73+
Jammit.compress_assets ? compressed_js(js) : js
7374
end
7475

7576
# Concatenate and compress a list of CSS stylesheets. When compressing a
@@ -109,6 +110,11 @@ def compile_jst(paths)
109110

110111

111112
private
113+
114+
# Returns a compressed javascript source using appropriate method of js compressor
115+
def compressed_js(js)
116+
@js_compressor.respond_to?(:compress) ? @js_compressor.compress(js) : @js_compressor.compile(js)
117+
end
112118

113119
# Given a set of paths, find a common prefix path.
114120
def find_base_path(paths)

lib/jammit/dependencies.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# Gem Dependencies:
1111
require 'yui/compressor'
1212
require 'closure-compiler'
13+
require 'uglifier'
1314

1415
# Load initial configuration before the rest of Jammit.
1516
Jammit.load_configuration(Jammit::DEFAULT_CONFIG_PATH, true) if defined?(Rails)

test/config/assets-uglifier.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
javascript_compressor: uglifier
2+
embed_assets: on
3+
4+
javascripts:
5+
js_test:
6+
- fixtures/src/*.js
7+
jst_test:
8+
- fixtures/src/*.jst
9+
10+
stylesheets:
11+
css_test:
12+
- fixtures/src/*.css

test/fixtures/jammed/js_test-uglifier.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/unit/test_jammit_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'active_support'
33
require 'action_pack'
44
require 'action_controller'
5+
require 'active_support/core_ext/module/deprecation'
56
require 'action_controller/base'
67
require 'action_controller/test_case'
78
require 'jammit/controller'

test/unit/test_uglifier.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'test_helper'
2+
3+
class UglifierText < Test::Unit::TestCase
4+
5+
def setup
6+
Jammit.load_configuration('test/config/assets-uglifier.yml').reload!
7+
@compressor = Compressor.new
8+
end
9+
10+
def teardown
11+
Jammit.load_configuration('test/config/assets.yml').reload!
12+
end
13+
14+
def test_javascript_compression
15+
packed = @compressor.compress_js(glob('test/fixtures/src/*.js'))
16+
assert packed == File.read('test/fixtures/jammed/js_test-uglifier.js')
17+
end
18+
19+
def test_jst_compilation
20+
packed = @compressor.compile_jst(glob('test/fixtures/src/*.jst'))
21+
assert packed == File.read('test/fixtures/jammed/jst_test.js')
22+
end
23+
24+
end

0 commit comments

Comments
 (0)