-
Notifications
You must be signed in to change notification settings - Fork 2
Home
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.
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 usesgoog.scopefor alias creation. - Compiler will ensure no package is provided/included more than once (even if you use
goog.provideandgoog.requiredirectly)
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 some.pack.having.the.very.long.name as shortalias
do_some_stuff()
Result of compilation in closure mode:
// Generated by CoffeeScript 1.6.1
goog.provide('my.awesome.pack');
goog.provide('package1');
goog.provide('package2');
goog.provide('package3');
goog.require('your.awesome.pack');
goog.require('some.package1');
goog.require('some.package2');
goog.require('some.pack.having.the.very.long.name');
goog.scope(function() {
var shortalias = some.pack.having.the.very.long.name;
do_some_stuff();
});
Currently classes in coffeescript are rewritten to the Closure compatible javascript having:
-
goog.providestatement inserted for each class -
goog.requirestatement inserted for each extends statement if not provided in the same file -
@constructorand@extendsannotations automatically inserted - whole class encapsulated into
goog.scopeallowing 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()
super()
doSomeStuff: ->
alert "Doing some stuff!"
Result of compilation using closure mode:
// Generated by CoffeeScript 1.6.1
goog.provide('project.sub.someClass');
goog.require('project.sub2.anotherClass');
goog.scope(function(){
/**
This is an awesome constructor
@constructor
@extends project.sub2.anotherClass
*/
project.sub.someClass = function() {
this.doSomeStuff();
project.sub.someClass.superClass_.constructor.call(this);
}
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!");
};
});
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', '.']);
- Enum support
If you have any ideas, add them to the issues.