Google Closure Compiler: ADVANCED Mode JavaScript Optimization and Its Strict Code Requirements
A JavaScript checker and optimizer.
At a glance
- What is it?
- Google Closure Compiler is a Java-based JavaScript optimizer that removes dead code, renames variables aggressively, and checks types. Its ADVANCED compilation mode, the only well-supported mode, produces the smallest possible output but requires JavaScript written specifically to work with it.
- Who is it for?
- Teams working on large JavaScript applications already using Google's Closure Library and goog.module() conventions should look at Closure Compiler for maximum code size reduction. Teams using standard ECMAScript modules, CommonJS, or arbitrary third-party JavaScript libraries should use Terser or esbuild instead, both of which work with standard module formats without requiring purpose-written input code.
- Can I use it commercially?
- Yes. Apache-2.0 is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 1 day ago.
- What is it written in?
- Mainly JavaScript, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on September 29, 2026, and from our analysis. They are not legal advice.
Editorial analysis
What Closure Compiler Does and the Problem It Solves
The Closure Compiler README describes it as 'a tool for making JavaScript download and run faster' and 'a true compiler for JavaScript.' Unlike a minifier, it does not simply compress whitespace and shorten variable names in a single pass. It performs whole-program analysis: parsing the entire codebase, building a type-annotated model of all variables and their uses, then applying transformations informed by that model.
The transformations include dead code elimination (removing functions and variables that are never called or read), aggressive renaming (replacing long property and variable names with single-character identifiers), constant inlining (replacing variable references with their values where safe), and property chain flattening (converting myFoo.some.sub.property to myFoo$some$sub$property for easier analysis). The result is JavaScript that is smaller and, in some configurations, faster to execute.
The compiler also checks syntax, verifies variable references, validates types against JSDoc-style type annotations, and warns about common JavaScript pitfalls. This type-checking capability is a separate feature from the optimization. Teams have used it purely for type safety enforcement without using the output JavaScript at all.
Google uses Closure Compiler on its own applications to reduce the size of very large JavaScript codebases. The README lists the supported use cases: reducing code size, checking for errors, defining localizable messages, transpiling newer JavaScript features for older browsers, and generating separately loadable chunks.
ADVANCED Mode: The Only Well-Supported Compilation Mode
The README states plainly that modes other than ADVANCED 'were always an afterthought and we have deprecated those modes.' SIMPLE and WHITESPACE_ONLY modes still exist but receive no active development, and the README says 'we believe that other tools perform comparably for non-ADVANCED modes and are better integrated into the broader JS ecosystem.'
ADVANCED mode is what makes Closure Compiler different from other JavaScript tools. It applies the most aggressive transformations: it assumes the compiler can see every use of every global variable and every property in the entire application, and it uses that assumption to rename and remove aggressively. This produces output that is meaningfully smaller than what SIMPLE mode produces.
The catch is described directly in the README: 'For ADVANCED mode to generate working JavaScript, the input JS code must be written with closure-compiler in mind.' This is not a minor caveat. It means that code written for standard JavaScript environments, using standard ECMAScript module syntax or CommonJS, and using third-party libraries not built for Closure Compiler, will not compile correctly in ADVANCED mode without significant changes.
The README describes Closure Compiler as a 'whole world optimizer.' It expects to 'directly see or at least receive information about every possible use of every global or exported variable and every property name.' Code that hides property accesses from the compiler, for example through dynamic property lookups, will cause the compiler to rename something it should not, producing broken output.
Getting Started and Building from Source
The README directs users to the official documentation at developers.google.com/closure/compiler/ for getting started. The repository's README section on getting started begins with 'IMPORTANT: Th' and is truncated, suggesting the full instruction text refers to the documentation site.
The repository uses Bazel as its build system. The top-level entries include MODULE.bazel, .bazelrc, .bazelversion, BUILD.bazel, and a build_test.sh script. Building the compiler from source requires a working Bazel installation.
The package.json in the repository root shows the compiled artifact path:
{
"scripts": {
"compile": "java -jar bazel-bin/compiler_uberjar_deploy.jar"
}
}Running the compiler requires Java. The output of the Bazel build is a JAR file called compiler_uberjar_deploy.jar. The `compile` script in package.json shows the invocation pattern: run the JAR with `java -jar`.
For users who do not want to build from source, the README mentions the closure-compiler-npm package as a devDependency in the repository's own package.json. The externs/ directory contains declarations for browser and JavaScript standard library APIs that the compiler uses to understand external interfaces it must not rename.
The goog.module() Constraint and ECMAScript Module Limitations
Closure Compiler has its own module system: goog.module() to declare a module and goog.require() to import one. Both come from the Closure Library's base.js. The README explains that this 'remains the only well supported way of defining modules' for Closure Compiler.
The ECMAScript import and export syntax (ES modules) did exist by the time this module system was established, but the README notes that 'changing Google's projects to use the newer syntax has never offered a benefit that was worth the cost of the change.' Google's TypeScript code uses ECMAScript modules, but they are converted to goog.module() syntax before Closure Compiler sees them.
The consequence is that the ECMAScript module support in Closure Compiler is effectively untested within Google. The README states: 'This means we are unlikely to notice or fix bugs in the support for ECMAScript modules.'
CommonJS module support was added at some point but is 'likely to be entirely removed sometime in 2024' according to the README. Teams evaluating this tooling for a CommonJS project should check the current status of that removal.
This module system requirement is the primary reason Closure Compiler does not fit general JavaScript projects. Most modern JavaScript toolchains use ES modules or CommonJS. Adopting Closure Compiler in such a project would require either converting the entire codebase to goog.module(), or converting libraries that use ECMAScript modules before they reach the compiler.
Property Access Consistency and the Externs System
One of the most counterintuitive requirements of ADVANCED mode concerns property access syntax. The README describes it: 'Closure Compiler property renaming requires you to consistently access a property with either obj[p] or obj.propName, but not both.'
When a property is accessed with square bracket notation (obj[propertyName]), the compiler cannot see the literal property name being referenced. If the same property is also accessed with dot notation somewhere else (obj.propName), the compiler cannot link these two accesses and may rename the dot-notation use while leaving the bracket-notation access unrenamed, resulting in a broken reference.
The compiler detects some of these conflicts and stops compilation with an error. Others it misses, silently producing broken output. The README notes: 'There are cases where it will fail to recognize the problem and simply generate broken JS without warning.'
Externs files solve a related problem: code outside the compilation unit that should not be renamed. The browser and JavaScript standard library APIs are declared in externs files in the externs/ directory. Teams using less common APIs or interfacing with external code that accesses renamed properties need additional externs files. Writing and maintaining externs files for every third-party library used in a project is one of the primary operational costs of using Closure Compiler.
The target environment assumption also matters: the compiler's default externs assume a web browser window. NodeJS externs exist but are not actively supported. The README states that NodeJS support 'never worked very well.'
Closure Compiler vs Alternative JavaScript Optimizers
Terser is the most common alternative for JavaScript minification. It works with standard ES modules and CommonJS without requiring goog.module() conventions, does not require externs files, and integrates into webpack and Rollup without a Java runtime. Its output is larger than Closure Compiler ADVANCED mode for equivalent code, because it does not perform whole-program property renaming. For most JavaScript projects, Terser or esbuild produces acceptable output size without the adoption cost that Closure Compiler requires.
webpack and Rollup with tree-shaking perform dead code elimination at the module level, removing entire modules or exports that are not imported anywhere. This is coarser than Closure Compiler's function-level dead code removal but works with standard module syntax and third-party packages from npm. For most applications, module-level tree shaking reduces bundle size enough that the incremental gain from ADVANCED property renaming is not worth the constraints.
Closure Compiler is the right tool for a specific context: very large JavaScript applications where the team controls the entire codebase and has written it using Google's Closure conventions from the start. For any other context, the set-up cost exceeds the code-size benefit, especially given that the README explicitly acknowledges limitations with ECMAScript modules and third-party code.
Maintenance Status and Apache-2.0 License
The last push to the repository was on 2026-09-25. The repository has no GitHub releases; version management for downstream users is handled through the closure-compiler-npm package on npm. The Apache-2.0 license permits commercial use, modification, and redistribution, with the requirement that copyright notices and the license text are preserved.
The README acknowledges several limitations explicitly as ongoing constraints rather than known bugs to be fixed: ECMAScript module support is unlikely to improve because it is unused at Google; CommonJS support may be removed; NodeJS support never worked well and remains unsupported. These statements reflect the reality that Closure Compiler is an internal Google tool that is open-sourced but not primarily developed for the general JavaScript community.
The externs/ directory and the src/ directory contain the bulk of the implementation. The contrib/ directory suggests there are community-contributed features. The THIRD_PARTY_NOTICES file indicates third-party dependencies whose licenses need to be preserved in distributions.
For teams evaluating adoption: the significant gain from Closure Compiler is its aggressive ADVANCED mode property renaming on codebases written with its constraints in mind. That gain is real and substantial for the codebases Google maintains. For code written to standard JavaScript patterns, the constraints outweigh the benefits.
Editorial conclusion
Teams working on large JavaScript applications already using Google's Closure Library and goog.module() conventions should look at Closure Compiler for maximum code size reduction. Teams using standard ECMAScript modules, CommonJS, or arbitrary third-party JavaScript libraries should use Terser or esbuild instead, both of which work with standard module formats without requiring purpose-written input code. Before adopting Closure Compiler, read the Important Caveats section of the README and verify that all property accesses in the codebase consistently use either dot notation or bracket notation, not both.
Frequently asked questions
How do you use Google Closure Compiler to compile JavaScript?
After building from source with Bazel, you run the output JAR with Java: the repository's package.json shows the pattern as java -jar bazel-bin/compiler_uberjar_deploy.jar. The compiler requires JavaScript input written with goog.module() conventions for ADVANCED mode. The official documentation at developers.google.com/closure/compiler/ covers invocation options.
What is Google Closure Compiler?
Google Closure Compiler is a Java-based tool that compiles JavaScript to smaller, faster JavaScript by removing dead code, renaming variables aggressively, and checking types. The README describes it as 'a true compiler for JavaScript' rather than a simple minifier, because it analyzes the entire program before transforming it.
What is a good alternative to Google Closure Compiler?
Terser is the most common alternative for JavaScript minification without the constraints Closure Compiler imposes. It works with standard ES modules and CommonJS, requires no Java runtime, and integrates directly into webpack and Rollup. The README itself acknowledges that other tools perform comparably for non-ADVANCED compilation modes.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/google-closure-compiler)