Tue 26 Sep 2017

Closure Compiler

This is a nice optimising compiler with modules and type hints.

It doesn't understand:

  • with
  • eval
  • 3rd party JS libraries
  • Accessing a property with a mixture of quoted strings and dot notation.
  • That window.thing and var thing can be the same thing.
  • Functions that are only accessed by looping over an object.
  • Only compiling part of your code (how would it know which things are used?).

Exports

Store things you want to export on the Window object?

e.g. window['my-exports'] = blah;.

This seems like a bad approach to me. Maybe it will work with ES6 modules instead?

Or you can use goog.exportSymbol() or goog.exportProperty().

Annotations

These go inside comments and turn everything into Java.

Some examples, before I got bored / decided it was useless to read them all:

  • @const
  • @const {string}
  • not for ES6 classes or goog.defineClass
  • @define {boolean}
  • @deprecated
  • use only indexing syntax
  • dictionary where the values must match the type
  • @export
  • @export {string}
  • @extends {type}
  • an externs file
  • @final
  • @nosideeffects
  • @override

Types

Generics look like Java.

Union types with |

Record types look like a dictionary.

?string nullable types

!string not-nullable types

Various function types.

Top and bottom types.

Closure Library

goog.provide()
sets up a namespace.
goog.inherits()
subclassing.
goog.events
standardized events across browsers.
  • goog.events.listen
  • goog.events.EventType
goog.dom
DOM manipulation functions.
  • goog.dom.createDom