Permalink
Browse files

Support older MultiJson versions

Add feature detection to MultiJson API. Support older versions to
maintain compatibility with Rails 3.1
  • Loading branch information...
1 parent 71a8591 commit c32b571de97a40d443d32c6650754b5235e025e5 @lautis committed Sep 2, 2012
Showing with 16 additions and 5 deletions.
  1. +1 −1 Gemfile
  2. +15 −4 lib/uglifier.rb
View
@@ -1,7 +1,7 @@
source "http://rubygems.org"
gem "execjs", ">=0.3.0"
-gem "multi_json", "~> 1.3"
+gem "multi_json", "~> 1.0", ">= 1.0.2"
# Depend on defined ExecJS runtime
execjs_runtimes = {
View
@@ -62,7 +62,7 @@ def compile(source)
js = []
js << "var result = '';"
- js << "var source = #{MultiJson.dump(source)};"
+ js << "var source = #{json_encode(source)};"
js << "var ast = UglifyJS.parser.parse(source);"
if @options[:lift_vars]
@@ -79,17 +79,17 @@ def compile(source)
JS
end
- js << "ast = UglifyJS.uglify.ast_mangle(ast, #{MultiJson.dump(mangle_options)});"
+ js << "ast = UglifyJS.uglify.ast_mangle(ast, #{json_encode(mangle_options)});"
if @options[:squeeze]
- js << "ast = UglifyJS.uglify.ast_squeeze(ast, #{MultiJson.dump(squeeze_options)});"
+ js << "ast = UglifyJS.uglify.ast_squeeze(ast, #{json_encode(squeeze_options)});"
end
if @options[:unsafe]
js << "ast = UglifyJS.uglify.ast_squeeze_more(ast);"
end
- js << "result += UglifyJS.uglify.gen_code(ast, #{MultiJson.dump(gen_code_options)});"
+ js << "result += UglifyJS.uglify.gen_code(ast, #{json_encode(gen_code_options)});"
if !@options[:beautify] && @options[:max_line_length]
js << "result = UglifyJS.uglify.split_lines(result, #{@options[:max_line_length].to_i})"
@@ -149,4 +149,15 @@ def gen_code_options
options
end
end
+
+ # MultiJson API detection
+ if MultiJson.respond_to? :dump
+ def json_encode(obj)
+ MultiJson.dump(obj)
+ end
+ else
+ def json_encode(obj)
+ MultiJson.encode(obj)
+ end
+ end
end

0 comments on commit c32b571

Please sign in to comment.