Content with Style

Web Technique

Genstatic, my first sip of coffee

by Pascal Opitz on March 6, 06:16

Having to spend more time at home means I can try out things. I know I'm late to the party, but I really wanted to have a go at using coffeescript. Sceptical at first, I really got to like it and quickly threw together a little static site generator: genstatic.

I had seen .coffee files in many node projects lately, but it was a little twitter converstaion with our own Mike Stenhouse of course that finally brought me to taking a closer look.

I had to do a pretty tedious static HTML job that didn't fit the bog standard blog structure, so I decided to roll my little static site generator, and that's how genstatic came about.

Coffescript creates Javascript

The first way I used coffee scrip was to compile it down to javascript, then run javascript in node. For this I'd have a a folder called ./lib and a folder called ./src. Then you run:


coffee -w -c -o ./lib ./src

This will watch the files in ./src and automatically compile them down to js into the ./lib folder.

I figure that with today's requirement of minification and concatination most people have to have some sort of compilation step in their deployment process anyway. Using coffee script could also be used in that. The -j flag will join scripts together for you as well, so in that respect it might make testing even easier.

Coffescript in node

While the above is great, in node we even can use coffeescript at runtime:


var coffee = require('coffee-script');
require('my.coffee');
str = coffee.compile(mystr);

This is very useful of course, and I have made good use of it in genstatic where you define meta stuff and helpers in coffeescript as well. These will then be read into strings and executed via node's brilliant vm.runInNewContext().

Wrapping it up

Having this little go my initial scepticism vanished pretty quickly and I think I'll use it from now on where ever I can.

If you want to see coffeescript in action, go to the coffeescript project pages or clone the git of genstatic.

Of course if you need to generate a static site, maybe give genstatic a go and let me know how it worked for you. A simple npm install genstatic and you should be on your way.

So there you have it: genstatic, my first sip of coffee

Comments