Created
December 24, 2011 08:25
-
-
Save wookiehangover/1516805 to your computer and use it in GitHub Desktop.
a pretty-good build script, with Cake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Dependencies | |
fs = require('fs') | |
{_} = require('underscore') | |
path = require('path') | |
tmpl = require('handlebars-jst') | |
{log} = require('util') | |
growl = require('growl') | |
stitch = require('stitch') | |
uglify = require('uglify-js') | |
# Configuration | |
APP_PATH = path.resolve('./assets/app') # <--- Main Application Dir | |
LIB_PATH = path.resolve('./assets/js/libs') # <--- External Deps Dir | |
SRC_DIR = 'assets/js/src' # <--- Where compiled output goes | |
VIEWS_DIR = 'assets/app/views' # <--- Where your templates live | |
# Helpers for minifying with [uglify](https://github.com/mishoo/UglifyJS) and | |
# building with [Stitch](https://github.com/sstephenson/stitch) | |
uglyStick = ( file, minfile, no_squeeze ) -> | |
jsp = uglify.parser | |
pro = uglify.uglify | |
fs.readFile file, 'utf8', (err, fileContents) -> | |
try | |
ast = jsp.parse( fileContents ) | |
ast = pro.ast_mangle( ast ) | |
unless no_squeeze | |
ast = pro.ast_squeeze( ast ) | |
final_code = pro.gen_code( ast ) | |
fs.writeFile( minfile, final_code ) | |
log "Uglified #{minfile}" | |
catch e | |
growl "Uglify ERROR: \n#{e}" | |
sew = ( filename, paths, deps ) -> | |
pkg = stitch.createPackage( paths: paths, dependencies: deps || [] ) | |
pkg.compile ( err, src ) -> | |
fs.writeFile filename, src, ( err ) -> | |
throw err if err | |
msg = "Compiled #{filename}" | |
log( msg ) | |
growl( msg ) | |
uglyStick filename, filename.replace(/\.js/, '.min.js') | |
# Build Tasks | |
task 'build', 'create minified application, libraries and templates files', -> | |
invoke 'build:app' | |
invoke 'build:libs' | |
invoke 'build:templates' | |
task 'build:app', 'concat application with Stitch', -> | |
paths = [ APP_PATH ] | |
sew "#{SRC_DIR}/app.js", paths | |
task 'build:libs', 'concat all external libs with Stitch',-> | |
root = LIB_PATH | |
### | |
# THIS IS WHERE YOU DECLARE AND EXTERNAL DEPENDENCIES | |
### | |
paths = [] | |
deps = [ | |
"#{root}/underscore.js" # <---- Right Here | |
"#{root}/backbone.js" | |
] | |
sew "#{SRC_DIR}/libs.js", paths, deps | |
task 'build:templates', 'compile all Handlebars templates', -> | |
filename = "#{SRC_DIR}/templates.js" | |
tmpl.build VIEWS_DIR, ( data ) -> | |
tmpl.process data, SRC_DIR, -> | |
uglyStick( filename, filename.replace(/\.js/, '.min.js'), true ) | |
# Watch Tasks | |
task 'watch', 'run appropriate build whenever app or template files change', -> | |
root = APP_PATH | |
files = fs.readdirSync( root ) | |
dirs = [ root ] | |
for f in files | |
if fs.statSync("#{root}/#{f}").isDirectory() is true | |
dirs.push("#{root}/#{f}") | |
for d in dirs then do (d) -> | |
handler = (event, filename) -> | |
if /views/.test( d ) | |
invoke 'build:templates' | |
else | |
invoke 'build:app' | |
fs.watch( d, _.throttle( handler, 50 ) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment