With the script below, just set the #config params and point it at your JavaScript source folder. If you create a file called application.js in your source folder, it will be the first file that is written in the final file.
puts 'Google Closure Compiler';
puts 'Concatenate and minify all JavaScript in src directory';
# config
closure = 'C:/closure-compiler/compiler.jar'
src_dir = '../assets/js/src'
src_dest = '../assets/js/application.min.js'
# load all files from src_dir
files_arr = Dir.glob(File.join(src_dir, "*.js"));
# apply sort order so application is first
files_arr.sort! do |file_a, file_b|
if File.basename(file_a) == "application.js" then
-1
else
0
end
end
# compile all application src code
cmd = "java -jar #{closure} --js " + files_arr.join(" ") + " --js_output_file #{src_dest}"
puts "running: #{cmd}"
IO.popen(cmd);
Along with this script, here is a version for all the vendor scripts in your project. Point this at your 'vendor' or 'libs' folder and it will compress and create a *.min.js file for each script in that folder.
puts 'Google Closure Compiler';
puts 'Minifying all vendor src code';
# config
closure = 'C:/closure-compiler/compiler.jar'
src_dir = '../assets/js/vendor'
src_dest = '../assets/js/vendor'
# compile all vendor src code
Dir.glob(File.join(src_dir, "*.js")).each do |f|
unless f.include? '.min.js'
name = File.basename f
new_name = name.gsub(/.js/, '.min.js');
cmd = "java -jar #{closure} --js #{src_dir}/#{name} --js_output_file #{src_dest}/#{new_name}"
puts "running: #{cmd}"
IO.popen(cmd);
end
end;
No comments:
Post a Comment