Skip to content

Commit

Permalink
Add rake task to build with closure advanced optimizations.
Browse files Browse the repository at this point in the history
You would use this to compile your code _with_ underscore, to
remove all those dead underscore functions you don't use.

Slight tweak to code to `root._` code to get working, and must
remove anonymous wrapper function for current closure compiler to
remove dead code.
  • Loading branch information
ratbeard committed Feb 23, 2010
1 parent 31f0f8c commit b5449ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
22 changes: 20 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
require 'rubygems'
require 'closure-compiler'

desc "Use the Closure Compiler to compress Underscore.js"
task :build do
js = File.open('underscore.js', 'r')
min = Closure::Compiler.new.compile(js)
File.open('underscore-min.js', 'w') {|f| f.write(min) }
end
end

task :build_advanced do
js = File.read('underscore.js')
# remove wrapping anonymous function as this messes with closure compiler
# see
# http://groups.google.com/group/closure-compiler-discuss/browse_thread/thread/b59b54c1a0073aa5
js.sub!('(function() {', '').chomp!("})();\n")
compiler = Closure::Compiler.new \
:compilation_level => 'ADVANCED_OPTIMIZATIONS',
:formatting => 'PRETTY_PRINT'
min = compiler.compile(js)
File.open('underscore-min2.js', 'w') {|f| f.write(min) }
#
original_size = js.length
minimized_size = min.length
puts original_size, minimized_size
end

5 changes: 3 additions & 2 deletions underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
var breaker = typeof StopIteration !== 'undefined' ? StopIteration : '__break__';

// Create a safe reference to the Underscore object for reference below.
var _ = root._ = function(obj) { return new wrapper(obj); };

var _ = function(obj) { return new wrapper(obj); };
root._ = _;

// Export the Underscore object for CommonJS.
if (typeof exports !== 'undefined') exports._ = _;

Expand Down

0 comments on commit b5449ca

Please sign in to comment.