Skip to content

Instantly share code, notes, and snippets.

@presstube
Created April 7, 2012 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save presstube/2332073 to your computer and use it in GitHub Desktop.
Save presstube/2332073 to your computer and use it in GitHub Desktop.
rakefile for bundling less/css fand javascript files
# This is a simple rakefile that compiles LESS to CSS, minifies CSS and javascript files using YUI
# compressor, and bundles them into a single file to speed up loading.
#
# The manifest files are just simple YAML blobs that describe how to build the bundles. All paths are
# relative to the CSS_DIR (or JS_DIR), depending on what kind of bundle you're creating. You can create
# as many bundles as you like.
#
# The format for the manifest files is:
#
# bundle
# - file
# - file
# - ...
# ...
#
# For example:
#
# bundle1.js
# - lib/jquery.js
# - lib/jquery.ui.js
# - stuff.js
# bundle2.js
# - lib/jquery.js
# - morestuff.js
CSS_DIR = "src/zen.web/content/css";
JS_DIR = "src/zen.web/content/js";
CSS_MANIFEST = "config/stylesheets.yml";
JS_MANIFEST = "config/scripts.yml";
YUI_COMPRESSOR_PATH = "tools/yuicompressor-2.4.2.jar"
require "rake"
require "yaml"
require "less"
desc "Minifies and bundles CSS and javascript"
task :minify => [ :minify_css, :minify_js ]
desc "Converts LESS templates to a minified bundle of CSS"
task :minify_css do
basepath = File.join(File.dirname(__FILE__), CSS_DIR)
YAML.load_file(CSS_MANIFEST).each do |bundle, files|
puts "Bundling #{files.length} stylesheet(s) to #{bundle}:"
compressor = IO.popen("java -jar #{YUI_COMPRESSOR_PATH} --type css", "w+")
files.each do |file|
puts " + #{file}"
path = File.join(basepath, file)
Dir.chdir(File.dirname(path))
compressor.puts Less::Engine.new(IO.read(path)).to_css
end
compressor.close_write
File.open(File.join(basepath, bundle), "w") do |f|
f.write(compressor.gets)
end
puts "Wrote #{bundle}."
end
chdir(File.dirname(__FILE__))
end
desc "Bundles and minifies javascript"
task :minify_js do
path = File.join(File.dirname(__FILE__), JS_DIR)
YAML.load_file(JS_MANIFEST).each do |bundle, files|
puts "Bundling #{files.length} script(s) to #{bundle}:"
compressor = IO.popen("java -jar #{YUI_COMPRESSOR_PATH} --type js", "w+")
files.each do |file|
puts " + #{file}"
compressor.puts IO.read(File.join(path, file))
end
compressor.close_write
File.open(File.join(JS_DIR, bundle), "w") do |f|
f.write(compressor.gets)
end
puts "Wrote #{bundle}."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment