Skip to content

Instantly share code, notes, and snippets.

@c-spencer
Created March 1, 2012 14:47
Show Gist options
  • Save c-spencer/1950231 to your computer and use it in GitHub Desktop.
Save c-spencer/1950231 to your computer and use it in GitHub Desktop.
watch script for cake compile with selected files
fs = require 'fs'
coffee = require 'coffee-script'
uglify = require 'uglify-js'
option '-w', '--watch', 'watch files for changes'
task 'build:js', 'Build the javascript files', (options) ->
compileCoffeeFiles = (files, callback) ->
container = {}
runCallback = -> callback(container)
compileTimer = null
triggerCompile = ->
clearTimeout(compileTimer) if compileTimer
compileTimer = setTimeout runCallback, 50
for file in files
if options.watch
do (file) ->
fs.watchFile file, {interval: 500}, (curr, prev) ->
if curr.mtime.getTime() != prev.mtime.getTime()
try
container[file] = coffee.compile(fs.readFileSync(file, "utf-8"))
console.log "#{file} changed: #{container[file].length} bytes."
triggerCompile()
catch exp
console.log "#{file}: #{exp.message}"
try
container[file] = coffee.compile(fs.readFileSync(file, "utf-8"))
console.log "#{file} compiled: #{container[file].length} bytes."
catch exp
console.log "#{file}: #{exp.message}"
triggerCompile()
createCompiler = (static_js, target, namespace) ->
return (compiled) ->
startCompile = +new Date
console.log "Starting #{startCompile} for #{target}"
jsout = static_js
for name, content of compiled
jsout += "\n\n" + content
if namespace
jsout = """(function () {
var Sysdea = {};
#{jsout};
if (typeof window !== 'undefined') {
(window.#{namespace} = Sysdea)
};
}());"""
else
jsout = """(function () {
var Sysdea = {};
#{jsout}
}());"""
fs.writeFileSync(target + ".js", jsout)
console.log "Compiled in #{(+new Date) - startCompile} milliseconds. #{jsout.length} bytes."
jslib = [
'javascript/jquery.js'
'javascript/underscore.js'
]
coffeelib = [
'coffee/stuff.coffee'
]
jsbase = ""
for file in jslib
jsfile = fs.readFileSync(file, 'utf-8')
###
if file == "./lib/ace/build/src/ace-uncompressed.js"
jsfile = jsfile.replace(/\/\/fonts\.googleapis\.com\/css\?family=Droid\+Sans\+Mono/g, "/css/droid-font.css")
###
console.log "Static js #{file}: #{jsfile.length} bytes."
if jsfile.charCodeAt(0) is 0xFEFF # detect UTF byte-ordered mask
jsfile = jsfile.substring 1 # strip off mask before lexing
jsbase += jsfile + ";\n\n"
compileCoffeeFiles coffeelib, createCompiler(jsbase, "./public/build/app")
task 'minify:js', 'Minify the javascript files', (options) ->
round2 = (n) -> Math.round(n*100)/100
minify = (target) ->
startCompile = +new Date
jsout = fs.readFileSync "#{target}.js", 'utf-8'
ast = uglify.parser.parse(jsout); # parse code and get the initial AST
ast = uglify.uglify.ast_mangle(ast, {toplevel: true}); # get a new AST with mangled names
ast = uglify.uglify.ast_squeeze(ast); # get an AST with compression optimizations
final_code = uglify.uglify.gen_code(ast, {beautify: false, indent_level: 2}); # compressed code here
fs.writeFileSync(target + ".min.js", final_code)
console.log "Minified #{target} in #{(+new Date) - startCompile}"
console.log " original:\t#{round2 jsout.length/1024}kb"
console.log " minified:\t#{round2 final_code.length/1024}kb"
minify 'public/build/app'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment