-
Notifications
You must be signed in to change notification settings - Fork 788
Google Closure
Clojure is nothing if not practical and ClojureScript is no different. As more and more applications target web and mobile environments, the role and reach of JavaScript likewise expands beyond the confines of the browser into even more constrained environments such as mobile with ubiquitous networked paths between nodes. The Google Closure tools provides a robust set of libraries, a strong dependency management system, and a sophisticated compiler each working toward an ultimate goal of reducing JavaScript code size. For ClosureScript, Google Closure provides a solution to the "library problem" for JavaScript in three distinct ways:
- Libraries
- Dependency management
- Aggressive code minification
Any one of these features represents thousands of man-hours of research, design, development, and testing and therefore ClosureScript leverages them to maximum effect in its implementation thus allowing the Clojure/core team and contributors to focus on enabling the power of Clojure wherever JavaScript is found.
A ClojureScript implementation philosophy is one built around the rock-solid Google Closure libraries. That is, ClojureScript is written in Clojure and ClojureScript interoperating with the Google Clojure javascript libraries. However, ClosureScript can interoperate with any JavaScript library or framework. The use of the Google Closure libraries in the ClojureScript core is an implementation strategy that allows us to reduce dependencies and ensure compatibility with advanced mode compilation (see below).
ClosureScript also encapsulates Google Closure's dependency management system offering three primitives addDependency
, provide
, and require
that enable compile-time dependency inclusions. This capability is leveraged to allow fine-grained imports and exports of ClosureScript namespaces and functions and also external JavaScript libraries for interoperability purposes.
Google's Closure Tools also provide a minification compiler available to the ClojureScript compilation process via command-line flags. In summary, the minification compiler receives generated JavaScript produced by the ClojureScript compiler and renames variables to shorter names, removes whitespace, removes comments, etc. in an effort to reduce the source size while maintaining the program semantics. While useful, rote minification of this sort does not scale. That is, while it certainly reduces gross size, the size of the total source will always increase as more source code is added.
Google's Closure compiler provides an advanced setting allowing a more more aggressive minification strategy than that outlined above. In fact, the Google Closure compiler uses highly sophisticated techniques for identifying and eliminating dead code (i.e. code that is never called nor reachable). The downside of this fact is that the style of the JavaScript code must conform to a strict standard in order to achieve maximum minification potential. Therefore, it has been the goal of the ClojureScript core developers to ensure that the generated JavaScript code is maximally minifiable through an observance to said idioms and through the use of the Closure libraries and dependency system.
The primary reference for the Google Closure tools is O'Reilly's Closure: The Definitive Guide written by Michael Bolin. This book is highly recommended for anyone hoping to leverage the full power of ClojureScript interoperability, ClojureScript contributors, and those simply curious about advanced JavaScript techniques and tools.
- Rationale
- Quick Start
- Differences from Clojure
- [Usage of Google Closure](Google Closure)