Google Closure Tools Explained

Google Closure Tools
Logo Upright:yes
Author:Google
Released:November 5, 2009[1]
Programming Language:Java
Language:JavaScript
Genre:Ajax framework
License:Apache License 2.0

Google Closure Tools[2] is a set of tools built with the goal of helping developers optimize rich web applications with JavaScript. It was developed by Google for use in their web applications such as Gmail, Google Docs and Google Maps.[3] As of 2023, the project had over 230K LOCs not counting the embedded Mozilla Rhino compiler.

Closure Compiler

The Closure Compiler is a tool that attempts to compress and optimize JavaScript code, at the expense of human readability. Unlike an actual compiler, it does not compile from JavaScript to machine code but rather minifies JavaScript.

The process executes the following steps:

  1. Parses the submitted JavaScript
  2. Analyzes the JavaScript
  3. Removes any dead code
  4. Rewrites and minifies what is left

It also checks syntax, variable references, and types and warns about common JavaScript pitfalls.

It supports transpiling modern ECMAScript code to ECMAScript 5 to achieve a wider range of browser compatibility, similar to Babel. This obviated Traceur Compiler, another project that supported transpiling ES6 to ES3.[4]

The Closure compiler also supports type checking via JSDoc type annotations.[5]

CLI

The Closure Compiler is available for use through command line tools:

Closure Compiler Service

The Closure Compiler Service application provides a form for a user to input a URL pointing to a JavaScript source or enter JavaScript source code in a text box. The website will display the compiled JavaScript on the right side for the user to copy.[6]

An API is available, accessible via POST requests, parameters include:

The service is marked as deprecated and will eventually be removed.[7]

Ecosystem

Programming languages that transpile to JavaScript can use the Closure Compiler in their toolchain. For example, the Closure Compiler is used in ClojureScript to optimize the compiled JavaScript.[8]

Internals

Closure Compiler is built upon a modified version of the Rhino JS engine built by Mozilla, Google Guava, a Java standard library, Protocol Buffers, Gson and various other tools for testing. It also ships with built-in JavaScript JSDoc annotations for various popular projects like Node.js' standard API library, JQuery, and Google Map APIs.

Closure Library

The Closure Library is a JavaScript library, written specifically to take advantage of the Closure Compiler, based on a modular architecture. It provides cross-browser functions for DOM manipulations and events, Ajax and JSON, as well as more high-level objects such as User Interface widgets and Controls.

Closure Templates

Closure Templates are a templating system for dynamically generating HTML in both Java[9] and JavaScript.[10]

Since the language is apparently referred to as "Soy" internally by Google, and "Soy" remains in some of the documentation and classes,[11] sometimes Closure Templates are referred to as "Soy Templates".

Closure Stylesheets

Closure Stylesheets provide extensions to CSS, which are transpired to ordinary CSS. Internally in Google, this extended version of CSS is referred to as GSS.

As of November 2021, Closure Stylesheets have been deprecated in favor of tools such as Sass and PostCSS.[12]

See also

External links

Notes and References

  1. Web site: Introducing Closure Tools - The official Google Code blog . 5 November 2009.
  2. Bolin, Michael, "Closure: The Definitive Guide", O'Reilly Media Inc., Sebastopol, CA, 2010
  3. Web site: FAQ - Closure Tools. Google Developers.
  4. Web site: Traceur is a JavaScript.next-to-JavaScript-of-today compiler. GitHub.
  5. Web site: Annotating JavaScript for the Closure Compiler . .
  6. Web site: Closure Compiler Service.
  7. Web site: Closure Compiler Service API Reference . 2023-07-18.
  8. Web site: Motivations for using Google's Closure Tools . .
  9. Web site: Hello World Using Java . github.com.
  10. Web site: Hello World Using JavaScript . github.com.
  11. Web site: Google Code Archive - Long-term storage for Google Code Project Hosting. . code.google.com.
  12. Web site: Officially mark this repository as end-of-life . Weizenbaum, Natalie . 2021-11-01 . 2023-07-18.