Matt Curcio

Software Engineer

Open Source Work github/mcurcio

Professional Connections linkedin/curciomatt

Javascript Preprocessor with UglifyJS

Tuesday May 29, 2012

In a recent project, I was looking for a way to support Debug/Release builds with compile time branching. For compression/security reasons, I was trying to remove debug code from the final build. Since I am already using CoffeeScript on this project, I looked over the feature list to see anything like that is supported. To me, CoffeeScript seems like a natural fit for a preprocessor-type feature. Alas, a Google search for CoffeeScript preprocessor takes you to this year old feature request which remains open.

Enter UglifyJS.

UglifyJS includes a command line argument (and an API, if you're into that sort of thing) to define constant values at build time. And since UglifyJS supports a number of very cool advanced code-folding techniques, defining the right values ahead of time can accomplish exactly what I was looking for.

Time for a demo:

var a = prompt("Enter a fruit:"),
    b = "banana";

if (DEBUG){
    log.trace("a=" + a);
    log.trace("b=" + b);
} else {
    alert("a is " + a);
    alert("b is " + b);
}

Running this trivial script through UglifyJS via:

UglifyJS --define DEBUG=false

will result in production-ready code similar to:

var a = prompt("Enter a fruit:"),
    b = "banana";

alert("a is " + a);
alert("b is banana");

The UglifyJS README includes a more in-depth look at the possibilities that defines offers, and other features. If you are looking for a dead simple methodology to support dual-builds from a single codebase, this is it.

Jekyll Is Awesome

Wednesday May 09, 2012

After experimenting with Jekyll for several days now, I feel fully converted. There are some minor nuances that could be better documented, but that could be said for many open source projects.

A couple of tips

Retrieve other post data

This is the technique that powers the Next and Previous links at the bottom of this page. To retrieve the post data for the pages that come before or after, use:

    page.next
    page.previous

This doesnt seem to be documented directly, but it simple enough to find via Google.

Variables can be passed from pages up to the enclosing layout

I wanted to include the ubiquitous "fork me" banner across the page, but have the option of hiding it on a given page. I tried several techniques for setting a default variable value, and overring it later, which does not work. However, if you leave the variable undefined, then you can define it within a page, and the value will be available within the layout. There may be a more effective way to achieve this effect, but this worked for me.

Dear GitHub

Wednesday May 09, 2012

Pages is awesome. Well, pretty much all of GitHub is awesome. It's a fact.

So now that we have that out of the way, can you please send me the full error log when a a page build fails? K Thx.

The only way that I could find to track down the error was through a somewhat targeted trial-and-error. In my case, it seems to be related to the liquid {raw} tag, which I abandoned completely. The only relevant information that I could find on the help documentation specifies the Jekyll version GitHub is using, and nothing relating to the other dependencies.