The Closure Compiler is a tool for making JavaScript download and run faster. It is a true compiler for JavaScript. 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. It also checks syntax, variable references, and types, and warns about common JavaScript pitfalls.
Compilation modes other than ADVANCED were always an afterthought and we
have deprecated those modes. We believe that other tools perform comparably
for non-ADVANCED modes and are better integrated into the broader JS
ecosystem.
Closure Compiler is not suitable for arbitrary JavaScript. For ADVANCED
mode to generate working JavaScript, the input JS code must be written with
closure-compiler in mind.
Closure Compiler is 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.
It will aggressively remove and rename variables and properties in order to make the output code as small as possible. This will result in broken output JS, if uses of global variables or properties are hidden from it.
Although one can write custom externs files to tell the compiler to leave
some names unchanged so they can safely be accessed by code that is not part
of the compilation, this is often tedious to maintain.
obj[p] or obj.propName, but not both.When you access a property with square brackets (e.g. obj[p]) or using some
other indirect method like let {p} = obj; this hides the literal name of
the property being referenced from the compiler. It cannot know if
obj.propName is referring to the same property as obj[p]. In some cases
it will notice this problem and stop the compilation with an error. In other
cases it will rename propName to something shorter, without noticing this
problem, resulting in broken output JS code.
myFoo.some.sub.property ->
myFoo$some$sub$property), to make reasoning about them easier for detecting
unused code.It tries to either back off from doing this or halt with an error when doing it will generate broken JS output, but there are cases where it will fail to recognize the problem and simply generate broken JS without warning. This is much more likely to happen in code that was not explicitly written with Closure Compiler in mind.
WebWorkers are supported also, but the compiler will likely fail to warn you if you try to use features that aren't actually available to a WebWorker.
Some externs files and features have been added to Closure Compiler to support the NodeJS environment, but they are not actively supported and never worked very well.
JavaScript that does not use the goog.module() and goog.require() from
base.js to declare and use modules is not well supported.
The ECMAScript import and export syntax did not exist until 2015.
Closure compiler and closure-library developed their own means for
declaring and using modules, and this remains the only well supported
way of defining modules.
The compiler does implement some understanding of ECMAScript modules,
but 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. So, effectively the ECMAScript modules
support is unused within Google. This means we are unlikely to notice
or fix bugs in the support for ECMAScript modules.
Support for CommonJS modules as input was added in the past, but is not used within Google, and is likely to be entirely removed sometime in 2024.
Closure Compiler is used by Google projects to:
Drastically reduce the code size of very large JavaScript applications
Check the JS code for errors and for conformance to general and/or project-specific best practices.
Define user-visible messages in a way that makes it possible to replace them with translated versions to create localized versions of an application.
Transpile newer JS features into a form that will run on browsers that lack support for those features.
Break the output application into chunks that may be individually loaded as needed.
NOTE: These chunks are plain JavaScript scripts. They do not use the
ECMAScript import and export syntax.
To achieve these goals closure compiler places many restrictions on its input:
Use goog.module() and goog.require() to declare and use modules.
Support for the import and export syntax added in ES6 is not actively
maintained.
Use annotations in comments to declare type information and provide
information the compiler needs to avoid breaking some code patterns
(e.g. @nocollapse and @noinline).
Either use only dot-access (e.g. object.property) or only use dynamic
access (e.g. object[propertyName] or Object.keys(object)) to access
the properties of a particular object type.
Mixing these will hide some uses of a property from the compiler, resulting in broken output code when it renames the property.
In general the compiler expects to see an entire application as a single compilation. Interfaces must be carefully and explicitly constructed in order to allow interoperation with code outside of the compilation unit.
The compiler assumes it can see all uses of all variables and properties and will freely rename them or remove them if they appear unused.
Use externs files to inform the compiler of any variables or properties that it must not remove or rename.
There are default externs files declaring the standard JS and DOM global APIs. More externs files are necessary if you are using less common APIs or expect some external JavaScript code to access an API in the code you are compiling.
NOTE: NPM releases were put on hold in early 2024 and are not likely to resume until early 2025.
The easiest way to install the compiler is with NPM or Yarn:
yarn global add google-closure-compiler
# OR
npm i -g google-closure-compiler
The package manager will link the binary for you, and you can access the compiler with:
google-closure-compiler
This starts the compiler in interactive mode. Type:
var x = 17 + 25;
Hit Enter, then Ctrl+Z (on Windows) or Ctrl+D (on Mac/Linux), then Enter
again. The Compiler will respond with the compiled output (using SIMPLE mode
by default):
var x=42;
NOTE: Maven releases were put on hold in early 2024 and are not likely to resume until early 2025. See https://github.com/google/closure-compiler/issues/4220.
A pre-compiled release of the compiler is also available via Maven.
https://jscompressor.treblereel.dev/ is a web-based UI and REST API for Closure Compiler, developed and maintained by at https://github.com/treblereel/jscompressor.
The Closure Compiler has many options for reading input from a file, writing output to a file, checking your code, and running optimizations. Here is a simple example of compressing a JS program:
google-closure-compiler --js file.js --js_output_file file.out.js
We get the most benefit from the compiler if we give it all of our source
code (see Compiling Multiple Scripts), which
allows us to use ADVANCED optimizations:
google-closure-compiler -O ADVANCED rollup.js --js_output_file rollup.min.js
NOTE: The output below is just an example and not kept up-to-date. The Flags and Options wiki page is updated during each release.
To see all of the compiler's options, type:
google-closure-compiler --help
--flag |
Description |
|---|---|
--compilation_level (-O) |
Specifies the compilation level to use.
Options: BUNDLE, WHITESPACE_ONLY,
SIMPLE (default), ADVANCED
|
--env |
Determines the set of builtin externs to load.
Options: BROWSER, CUSTOM.
Defaults to BROWSER.
|
--externs |
The file containing JavaScript externs. You may specify multiple |
--js |
The JavaScript filename. You may specify multiple. The flag name is
optional, because args are interpreted as files by default. You may also
use minimatch-style glob patterns. For example, use
--js='**.js' --js='!**_test.js' to recursively include all
js files that do not end in _test.js
|
--js_output_file |
Primary output filename. If not specified, output is written to stdout. |
--language_in |
Sets the language spec to which input sources should conform.
Options: ECMASCRIPT3, ECMASCRIPT5,
ECMASCRIPT5_STRICT, ECMASCRIPT_2015,
ECMASCRIPT_2016, ECMASCRIPT_2017,
ECMASCRIPT_2018, ECMASCRIPT_2019,
STABLE, ECMASCRIPT_NEXT
|
--language_out |
Sets the language spec to which output should conform.
Options: ECMASCRIPT3, ECMASCRIPT5,
ECMASCRIPT5_STRICT, ECMASCRIPT_2015,
ECMASCRIPT_2016, ECMASCRIPT_2017,
ECMASCRIPT_2018, ECMASCRIPT_2019,
STABLE
|
--warning_level (-W) |
Specifies the warning level to use.
Options: QUIET, DEFAULT, VERBOSE
|
You can access the compiler in a JS program by importing
google-closure-compiler:
import closureCompiler from 'google-closure-compiler';
const { compiler } = closureCompiler;
new compiler({
js: 'file-one.js',
compilation_level: 'ADVANCED'
});
This package will provide programmatic access to the native Graal binary in most cases, and will fall back to the Java version otherwise.
If you have multiple scripts, you should compile them all together with one compile command.
google-closure-compiler in1.js in2.js in3.js --js_output_file out.js
You can also use minimatch-style globs.
# Recursively include all js files in subdirs
google-closure-compiler 'src/**.js' --js_output_file out.js
# Recursively include all js files in subdirs, excluding test files.
# Use single-quotes, so that bash doesn't try to expand the '!'
google-closure-compiler 'src/**.js' '!**_test.js' --js_output_file out.js
The Closure Compiler will concatenate the files in the order they're passed at the command line.
If you're using globs or many files, you may start to run into problems with
managing dependencies between scripts. In this case, you should use the
included lib/base.js that provides functions for enforcing
dependencies between scripts (namely goog.module and goog.require). Closure
Compiler will re-order the inputs automatically.
The Closure Compiler releases with lib/base.js that provides JavaScript functions and variables that serve as primitives enabling certain features of the Closure Compiler. This file is a derivative of the [identicall
$ claude mcp add closure-compiler \
-- python -m otcore.mcp_server <graph>