Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CoffeeScript together with Google Closure

Recently I have started to use Google Closure Tools for my javascript development. Until now, I have used to write my code in CoffeeScript, however, the javascript generated by CoffeeScript seems to be incompatible with Google Closure Compiler's advanced mode.

Is there any extension to the CoffeeScript compiler adding Google Closure support?

like image 929
Samuel Hapak Avatar asked Mar 12 '13 13:03

Samuel Hapak


People also ask

Who uses Coffeescript?

1 – WHAT IS COFFEESCRIPT Basically, CoffeeScript was made by people who hate JavaScript syntax, intended to be used by other people who hate JavaScript syntax.

What is closure Google what is closure in JavaScript?

The Closure Compiler is a tool for making JavaScript download and run faster. Instead of compiling from a source language to machine code, it compiles from JavaScript to better JavaScript. It parses your JavaScript, analyzes it, removes dead code and rewrites and minimizes what's left.


1 Answers

There are various tools that aiming to make CoffeeScript usable with Google Closure Tools. I will describe three of them:

Bolinfest's CoffeeScript fork

Features:

  • Fixed function binding, loops, comprehensions, in operator and various other incompatibilities
  • Fixed classes syntax for Google Closure
  • Automatic generation of @constructor and @extends annotations
  • Automatically inserts goog.provide statement for each class declared
  • Python's like include namespace as alias support translated to goog.require and goog.scope

Drawbacks:

  • Constructor has to be the very first statement in the class
  • Cannot use short aliases for classes inside the class (i.e. class My.Long.Named.Car cannot be refered as Car in class definition as pure CoffeeScript allows)
  • User written JsDoc comments don't get merged with compiler generated ones
  • Missing provide equivalent for include
  • No support for type casting, this can be done only by inserting pure javascript code inside backticks "`"
  • Based on outdated CoffeeScript 1.0

Read more at http://bolinfest.com/coffee/

My CoffeeScript fork

Disclaimer: I am the author of this solution

This solution is inspired by the Bolinfest's work and extends it in these ways:

  • Constructor can be placed anywhere inside the class
  • Short aliases for classes work using goog.scope
  • User written JsDoc comments get merged with compiler generated, user written @constructor and @extends annotations are replaced by generated
  • Each namespace is provided or included mostly once, namespace, that is provided is never included. You can provide namespace by keyword provide
  • Support for typecasting using cast<typeToCastTo>(valueToBeCast) syntax
  • Based on CoffeeScript 1.6

Read more at https://github.com/hleumas/coffee-script/wiki

Steida's Coffee2Closure

Unlike the two solutions above, Steida's Coffee2Closure is postprocessor of javascript code generated by upstream nontweaked CoffeeScript. This approach has a one major advantage, that it will need no or only slight updates with continued development of CoffeeScript and still be actual. However, by the very nature of this approach, some of the features cannot be delivered. Currently it fixes only classes and bindings, loops, in operator and few other incompatibilities. It has no support for automatic annotation generation, type casting or custom keywords.

https://github.com/Steida/coffee2closure

like image 60
Samuel Hapak Avatar answered Sep 30 '22 17:09

Samuel Hapak