Skip to content

Instantly share code, notes, and snippets.

@orlin
Created December 11, 2009 12:59
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 orlin/254196 to your computer and use it in GitHub Desktop.
Save orlin/254196 to your computer and use it in GitHub Desktop.
auto-compressed sprockets
#!/usr/bin/env ruby
#
# sprockets watch
#
# changed to:
# * use yui/compressor
# * load config/sprockets.yml
# * rake watch[lib/javascripts,public/javascripts,sprockets,config/sprockets.yml]
#
# ------------------------------------------------------
# inspired by Compass from Chris Eppstein
# minify and concat code from Mark Story
# put together by Kjell Bublitz
# using Sprockets!
#
# sources:
# http://chriseppstein.github.com/blog/2009/09/07/building-a-jekyll-watcher-with-fssm/
# http://mark-story.com/posts/view/creating-deployment-ready-javascript-with-sprockets
# ------------------------------------------------------
#
# usage:
# 1) create a structure like "js/src", "js/dist".
# 2) copy this rakefile to "js/"
# 3) start watching "js/src" for changes by calling "rake watch"
# 4) work in "js/src".
# any change will trigger the build process and create "build.js" in "dist"
#
# ------------------------------------------------------
#
# installation (if you dont have them already.):
# sudo gem install sprockets
# sudo gem install jsmin
# sudo gem install -r yui-compressor
# sudo gem install ttilley-fssm
#
# ------------------------------------------------------
#
# options:
# while i find the style to write them ugly, these may still come handy.
# you can specify the src & dist folder (path or dir-name) and the build filename
#
# rake watch[SOURCE-FOLDER, BUILD-FOLDER, FILE-NAME]
#
#
# defaults (if no options given):
# ./src
# ./dist
# build
#
# to change these you can call rake like this:
# rake watch[source,deploy,site]
#
# which would translate to:
# ./source
# ./deploy
# site
#
# please note that you have to omit the file extension in the filename argument.
# given the "site" example above, the filename would become "site.js" and "site-min.js" automatically.
#
# as said you can use full paths too:
# rake watch[webroot/js/app,webroot/js/build]
# rake watch[../sources,js]
#
# ------------------------------------------------------
#
# enjoy!
#
require 'rubygems'
require 'sprockets'
# require 'jsmin'
require 'yui/compressor'
def rebuild(relative, srcFolder, distFolder, distFilename, confFilepath)
puts ">>> Change Detected to: #{srcFolder}/#{relative}"
concatFile = "#{distFolder}/#{distFilename}.js"
minifiedFile = "#{distFolder}/#{distFilename}-min.js"
secretary = Sprockets::Secretary.new(YAML.load_file(confFilepath))
concatenation = secretary.concatenation
gluedFiles = concatenation.to_s
concatenation.save_to(concatFile)
# File.open(minifiedFile, 'w') { |file| file.write(JSMin.minify(gluedFiles)) }
compressor = YUI::JavaScriptCompressor.new(:munge => true)
File.open(minifiedFile, 'w') { |file| file.write(compressor.compress(gluedFiles)) }
puts "#{concatFile} updated."
end
desc "Watch the src folder and concat and minify sources through sprockets"
task :watch, :src_folder, :dist_folder, :filename, :config_filepath do |t, args|
require 'fssm'
srcFolder = args.src_folder.nil? ? "./lib/javascripts" : args.src_folder
distFolder = args.dist_folder.nil? ? "./public/javascripts" : args.dist_folder
distFilename = args.filename.nil? ? "sprockets" : args.filename
confFilepath = args.config_filepath.nil? ? "./config/sprockets.yml" : args.config_filepath
puts ">>> Watching #{srcFolder} for changes <<<"
FSSM.monitor(srcFolder, '**/*.js') do
update {|base, relative| rebuild(relative, srcFolder, distFolder, distFilename, confFilepath)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment