Setting up CoffeeScript and UglifyJS on your Windows Build Server February, 2012
Setting up CoffeeScript compilation and minification in you build pipeline could never be easier. For only $19.95 and a moment of your time I'll show you how. First off download the latest version of node.js and install it on your build server. You will probably have to restart you build server or agent(s) service so it picks up the node.js paths. Node ships with it's own package manager, NPM, so we'll use that to get the CoffeeScript and UglifyJS packages. Since this is a build server, and you will want your server or agent(s) to be able to access the modules, we can't just use the NPM global flag (-g) as this will install modules in the user directory. We will have to set the install path to be the same as node install directory (This path may be different on your system). The node install directory is set in your system path by the node installer so your build scripts will see modules installed there.
npm install coffee-script -g --prefix="C:\Program Files (x86)\nodejs"
npm install uglify-js -g --prefix="C:\Program Files (x86)\nodejs"
Now you can call these from your build script.
As a bonus (What else would you expect for $19.95?) here are a couple of rake tasks to compile all coffeescript files and minify all js files under a certain path:
require "fileutils" compile_coffeescript :compile_coffee do |options| options.path = 'src/Website' end uglifyjs :minify_js do |options| options.path = 'src/Website' end def compile_coffeescript(*args, &block) body = lambda { |*args| task = CoffeeCompiler.new block.call(task) task.run } Rake::Task.define_task(*args, &body) end class CoffeeCompiler attr_accessor :path def run() errors = false Dir.glob(File.join(@path, '**/*.coffee')) do |path| puts "Compiling coffee script #{path}..." command = "coffee -b -c \"#{File.expand_path(path)}\"" result = `#{command} 2>&1` puts result unless result.empty? if $? != 0 then puts command puts "Coffeescript compiliation failed: #{$?}." errors = true end end fail "Coffeescript compiliation failed." unless !errors end end def uglifyjs(*args, &block) body = lambda { |*args| task = UglifyJs.new block.call(task) task.run } Rake::Task.define_task(*args, &body) end class UglifyJs attr_accessor :path def run() errors = false Dir.glob(File.join(@path, '**/*.js')) do |path| puts "Uglifying javascript #{path}..." command = "uglifyjs --overwrite \"#{File.expand_path(path)}\"" result = `#{command} 2>&1` puts result unless result.empty? if $? != 0 then puts command puts "Uglifying failed: #{$?}." errors = true end end fail "Uglifying javascript failed." unless !errors end end