Hate UML?

Draw sequence diagrams in seconds.

Type-checked CoffeeScript with jzbuild
Posted on: 2011-09-08 14:42:02
Coffeescript is a faster way to write Javascript. You write a program in its terse syntax, and it gets expanded into javascript. Here's a coffeescript file, Animal.coffee
class Animal
    constructor: (@sound) ->

    makeSound: -> 
        alert @sound

class Cow extends Animal
    constructor: -> 
        super "moo"

    ###* @param {number} numHours ###
    graze: (numHours) ->

animal = new Cow()

animal.graze "16"
There are some errors in this file that wouldn't normally be detected until you run it.

Let's run jzbuild.py on it (Download jzbuild.py). It works on Linux or Windows as long as you have java and python installed. You do not need anything else.

jzbuild --closure --out out.js Animal.coffee
Creating /home/smhanov/.jzbuild
/home/smhanov/.jzbuild/rhino.jar not found! Downloading from ftp://ftp.mozilla.org/pub/mozilla.org/js/rhino1_7R2.zip
Could not find makefile.jz. Running jslint on input files.
Building release...
/home/smhanov/.jzbuild/jslint-rhino.js not found. Creating.
/home/smhanov/.jzbuild/compiler.jar not found! Downloading from http://closure-compiler.googlecode.com/files/compiler-latest.zip
Running closure compiler.
Fetching http://github.com/smhanov/jzbuild/raw/master/externs/jquery-mobile.js
Fetching http://closure-compiler.googlecode.com/svn/trunk/contrib/externs/jquery-1.5.js
Fetching http://closure-compiler.googlecode.com/svn/trunk/contrib/externs/json.js
java -jar /home/smhanov/.jzbuild/compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS --warning_level VERBOSE --externs /home/smhanov/.jzbuild/jquery-mobile.js --externs /home/smhanov/.jzbuild/jquery-1.5.js --externs /home/smhanov/.jzbuild/json.js --js /tmp/tmptYeoZk --js Animal.js --js /tmp/tmpk3Azo0.js

Animal.js:26: WARNING - actual parameter 1 of Cow.prototype.graze does not match formal parameter
found   : string
required: number

Animal.js:27: WARNING - Property eatGrass never defined on Cow

0 error(s), 2 warning(s), 72.4% typed

Here's what it gets compiled to, in out.js:
var __hasProp=Object.prototype.hasOwnProperty,__extends=function(b,a){function c(){this.constructor=b}for(
var d in a)__hasProp.call(a,d)&&(b[d]=a[d]);c.prototype=a.prototype;b.prototype=new 
c;b.__super__=a.prototype;return b},__bind=function(b,a){return function(){
return b.apply(a,arguments)}},__indexOf=Array.prototype.indexOf||function(b){for(
var a=0,c=this.length;a<c;a++)if(this[a]===b)return a;return-1},__slice=Array.prototype.slice;
var animal,Animal=function(b){this.sound=b};Animal.prototype.makeSound=function(){return alert(this.sound)};
var Cow=function(){Cow.__super__.constructor.call(this,"moo")};__extends(Cow,Animal);Cow.prototype.graze=function(){};
animal=new Cow;animal.makeSound();animal.graze("16");animal.eatGrass();
We can further minify and optimize the code by specifying the --release option.
jzbuild --closure --release --out out.js Animal.coffee
var c=Object.prototype.hasOwnProperty;var d;function g(){g.a.constructor.call(this,"moo")}(function(a,b){
function f(){this.constructor=a}for(var e in b)c.call(b,e)&&(a[e]=b[e]);f.prototype=b.prototype;a.prototype=
new f;a.a=b.prototype;return a})(g,function(a){this.b=a});d=new g;alert(d.b);d.c();


I created jzbuild a long time ago to support my workflow when I made zwibbler.com, which is about 8000 lines of javascript. I like to take the entire project, including all javascript files, and crunch them down to a single .js file that contains my web application. Though I do not use the Google closure library, the Closure compiler is great for doing this and catching errors.

I recently discovered Coffeescript, and it is a real time saver. Writing a file in Coffeescript takes only 2/3 of the lines of code. I can physically write code faster, and I can see 50% more of my code on the screen at a time. So naturally I wanted to update jzbuild to take coffeescript as well as .js files as input.

Jzbuild is nothing but a glorified installer for other programs. It is possible due to the work of many others, with special mentions for:

  • David Yeung, for creating jcoffeescript. This wrapper allows you to run coffeescript without installing anything except java. I forked it and added an option to allow external coffeescript code to be used.
  • Michael Bolin, whose fork of Coffeescript adds type annotations to coffeescript that are required by the closure compiler. I hope that he continues his excellent work. I forked his fork and removed dependencies on the Google javascript libraries.
If your project is a combination of javascript and coffeescript, and you want static type checking, I hope that jzbuild is useful. Report and fix issues: jzbuild on github.

Want more programming tech talk?
Add to Circles on Google Plus
Subscribe to posts

Post comment

Real Name:
Your Email (Not displayed):

Text only. No HTML. If you write "http:" your message will be ignored.
Choose an edit password if you want to be able to edit or delete your comment later.
Editing Password (Optional):


Other posts by Steve

Yes, You Absolutely Might Possibly Need an EIN to Sell Software to the US How Asana Breaks the Rules About Per-Seat Pricing 5 Ways PowToon Made Me Want to Buy Their Software How I run my business selling software to Americans 0, 1, Many, a Zillion Give your Commodore 64 new life with an SD card reader 20 lines of code that will beat A/B testing every time [comic] Appreciation of xkcd comics vs. technical ability VP trees: A data structure for finding stuff fast Why you should go to the Business of Software Conference Next Year Four ways of handling asynchronous operations in node.js Type-checked CoffeeScript with jzbuild Zero load time file formats Finding the top K items in a list efficiently An instant rhyming dictionary for any web site Succinct Data Structures: Cramming 80,000 words into a Javascript file. Throw away the keys: Easy, Minimal Perfect Hashing Why don't web browsers do this? Fun with Colour Difference Compressing dictionaries with a DAWG Fast and Easy Levenshtein distance using a Trie The Curious Complexity of Being Turned On Cross-domain communication the HTML5 way Five essential steps to prepare for your next programming interview Minimal usable Ubuntu with one command Finding awesome developers in programming interviews Compress your JSON with automatic type extraction JZBUILD - An Easy Javascript Build System Pssst! Want to stream your videos to your iPod? "This is stupid. Your program doesn't work," my wife told me The simple and obvious way to walk through a graph Asking users for steps to reproduce bugs, and other dumb ideas Creating portable binaries on Linux Bending over: How to sell your software to large companies Regular Expression Matching can be Ugly and Slow C++: A language for next generation web apps qb.js: An implementation of QBASIC in Javascript Zwibbler: A simple drawing program using Javascript and Canvas You don't need a project/solution to use the VC++ debugger Boring Date (comic) barcamp (comic) How IE <canvas> tag emulation works I didn't know you could mix and match (comic) Sign here (comic) It's a dirty job... (comic) The PenIsland Problem: Text-to-speech for domain names Pitching to VCs #2 (comic) Building a better rhyming dictionary Does Android team with eccentric geeks? (comic) Comment spam defeated at last Pitching to VCs (comic) How QBASIC almost got me killed Blame the extensions (comic) How to run a linux based home web server Microsoft's generosity knows no end for a year (comic) Using the Acer Aspire One as a web server When programmers design web sites (comic) Finding great ideas for your startup Game Theory, Salary Negotiation, and Programmers Coding tips they don't teach you in school When a reporter mangles your elevator pitch Test Driven Development without Tears Drawing Graphs with Physics Free up disk space in Ubuntu Keeping Abreast of Pornographic Research in Computer Science Exploiting perceptual colour difference for edge detection Experiment: Deleting a post from the Internet Is 2009 the year of Linux malware? Email Etiquette How a programmer reads your resume (comic) How wide should you make your web page? Usability Nightmare: Xfce Settings Manager cairo blur image surface Automatically remove wordiness from your writing Why Perforce is more scalable than Git Optimizing Ubuntu to run from a USB key or SD card UMA Questions Answered Make Windows XP look like Ubuntu, with Spinning Cube Effect See sound without drugs Standby Preventer Stock Picking using Python Spoke.com scam Stackoverflow.com Copy a cairo surface to the windows clipboard Simulating freehand drawing with Cairo Free, Raw Stock Data Installing Ubuntu on the Via Artigo Why are all my lines fuzzy in cairo? A simple command line calculator Tool for Creating UML Sequence Diagrams Exploring sound with Wavelets UMA and free long distance UMA's dirty secrets Installing the Latest Debian on an Ancient Laptop Dissecting Adsense HTML/ Javascript/ CSS Pretty Printer Web Comic Aggregator Experiments in making money online How much cash do celebrities make? Draw waveforms and hear them Cell Phones on Airplanes Detecting C++ memory leaks What does your phone number spell? A Rhyming Engine Rules for Effective C++ Cell Phone Secrets