Samuel edited this page Aug 14, 2013 · 89 revisions

This project aims to provide support for compilation coffee code to javascript compatible with Google Closure Tools.

The examples on this page assumes person reading them is familiar with the Google Closure Compiler and its requirements. Some of the examples implicitly assume the existence of other files with other code. This is not mentioned explicitly, but it is clear enough for programmers already familiar with Google Closure Compiler.

Current Features


Just use coffeescript with -g or -goog commandline option.


Closure mode adds two new keywords to coffeescript: include and provide to deal with namespaces. Each include statement is translated to the corresponding goog.require and each provide statement is translated to the corresponding goog.provide statement.

  • You can include/provide multiple namespaces using single include/provide statement using syntax include (package1, package2, ..., packageN)
  • You can include namespaces under short aliased names with syntax include long.named.namespace as alias. This feature uses goog.scope for alias creation.
  • Compiler will ensure no package is provided/included more than once (even if you use goog.provide and goog.require directly)

Example code in coffeescript

include my.awesome.pack
provide my.awesome.pack
include your.awesome.pack
include (some.package1, some.package2)
provide (package1, package2, package3)
include as shortalias


Result of compilation in closure mode:

// Generated by CoffeeScript 1.6.1
goog.scope(function() {
var shortalias =;


Creating Class

Currently classes in coffeescript are rewritten to the Closure compatible javascript having:

  • goog.provide statement inserted for each class
  • goog.require statement inserted for each extends statement if not provided in the same file
  • @constructor and @extends annotations automatically inserted
  • whole class encapsulated into goog.scope allowing to use short alias name for refering to class (e.g. you can use someClass instead of project.sub.someClass)

Example code in coffeescript:

class project.sub.someClass extends project.sub2.anotherClass
  @staticMethod: ->
    alert someClass.staticProperty

  @staticProperty: "Some value"
    This is an awesome constructor
  constructor: ->

  doSomeStuff: ->
    alert "Doing some stuff!"

Result of compilation using closure mode:

// Generated by CoffeeScript 1.6.1


This is an awesome constructor
@extends project.sub2.anotherClass
project.sub.someClass = function() {
goog.inherits(project.sub.someClass, project.sub2.anotherClass);
var someClass = project.sub.someClass;

project.sub.someClass.staticMethod = function() {
  return alert(someClass.staticProperty);

project.sub.someClass.staticProperty = "Some value";

project.sub.someClass.prototype.doSomeStuff = function() {
  return alert("Doing some stuff!");


Type Casting

Google Closure type casting is supported through the cast<type> function. Understand cast<type> as function taking one parameter and returning the same value, but cast to the type.

Example code in coffeescript:

a = cast<Array.<string>> ['Some', 'example', 'array', '.']

Result of compilation using closure mode:

// Generated by CoffeeScript 1.6.1
var a;

a = /** @type {Array.<string>} */(['Some', 'example', 'array', '.']);

Future Work

  • Enum support

If you have any ideas, add them to the issues.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.