1# [Google Closure Compiler](https://developers.google.com/closure/compiler/) 2 3[![Build Status](https://travis-ci.org/google/closure-compiler.svg?branch=master)](https://travis-ci.org/google/closure-compiler) 4 5The [Closure Compiler](https://developers.google.com/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. 6 7## Getting Started 8 * [Download the latest version](http://dl.google.com/closure-compiler/compiler-latest.zip) ([Release details here](https://github.com/google/closure-compiler/wiki/Releases)) 9 * [Download a specific version](https://github.com/google/closure-compiler/wiki/Binary-Downloads). Also available via: 10 - [Maven](https://github.com/google/closure-compiler/wiki/Maven) 11 - [NPM](https://www.npmjs.com/package/google-closure-compiler) 12 * See the [Google Developers Site](https://developers.google.com/closure/compiler/docs/gettingstarted_app) for documentation including instructions for running the compiler from the command line. 13 14## Options for Getting Help 151. Post in the [Closure Compiler Discuss Group](https://groups.google.com/forum/#!forum/closure-compiler-discuss) 162. Ask a question on [Stack Overflow](http://stackoverflow.com/questions/tagged/google-closure-compiler) 173. Consult the [FAQ](https://github.com/google/closure-compiler/wiki/FAQ) 18 19## Building it Yourself 20 21Note: The Closure Compiler requires [Java 7 or higher](http://www.java.com/). 22 23### Using [Maven](http://maven.apache.org/) 24 251. Download [Maven](http://maven.apache.org/download.cgi). 26 272. Add sonatype snapshots repository to `~/.m2/settings.xml`: 28 ``` 29 <profile> 30 <id>allow-snapshots</id> 31 <activation><activeByDefault>true</activeByDefault></activation> 32 <repositories> 33 <repository> 34 <id>snapshots-repo</id> 35 <url>https://oss.sonatype.org/content/repositories/snapshots</url> 36 <releases><enabled>false</enabled></releases> 37 <snapshots><enabled>true</enabled></snapshots> 38 </repository> 39 </repositories> 40 </profile> 41 ``` 42 433. Run `mvn -DskipTests` (omit the `-DskipTests` if you want to run all the 44unit tests too). 45 46 This will produce a jar file called `target/closure-compiler-1.0-SNAPSHOT.jar`. 47 48### Using [Eclipse](http://www.eclipse.org/) 49 501. Download and open the [Eclipse IDE](http://www.eclipse.org/). 512. Navigate to `File > New > Project ...` and create a Java Project. Give 52 the project a name. 533. Select `Create project from existing source` and choose the root of the 54 checked-out source tree as the existing directory. 553. Navigate to the `build.xml` file. You will see all the build rules in 56 the Outline pane. Run the `jar` rule to build the compiler in 57 `build/compiler.jar`. 58 59## Running 60 61On the command line, at the root of this project, type 62 63``` 64java -jar target/closure-compiler-1.0-SNAPSHOT.jar 65``` 66 67This starts the compiler in interactive mode. Type 68 69```javascript 70var x = 17 + 25; 71``` 72 73then hit "Enter", then hit "Ctrl-Z" (on Windows) or "Ctrl-D" (on Mac or Linux) 74and "Enter" again. The Compiler will respond: 75 76```javascript 77var x=42; 78``` 79 80The Closure Compiler has many options for reading input from a file, writing 81output to a file, checking your code, and running optimizations. To learn more, 82type 83 84``` 85java -jar compiler.jar --help 86``` 87 88More detailed information about running the Closure Compiler is available in the 89[documentation](http://code.google.com/closure/compiler/docs/gettingstarted_app.html). 90 91## Compiling Multiple Scripts 92 93If you have multiple scripts, you should compile them all together with one 94compile command. 95 96```bash 97java -jar compiler.jar --js_output_file=out.js in1.js in2.js in3.js ... 98``` 99 100You can also use minimatch-style globs. 101 102```bash 103# Recursively include all js files in subdirs 104java -jar compiler.jar --js_output_file=out.js 'src/**.js' 105 106# Recursively include all js files in subdirs, excluding test files. 107# Use single-quotes, so that bash doesn't try to expand the '!' 108java -jar compiler.jar --js_output_file=out.js 'src/**.js' '!**_test.js' 109``` 110 111The Closure Compiler will concatenate the files in the order they're passed at 112the command line. 113 114If you're using globs or many files, you may start to run into 115problems with managing dependencies between scripts. In this case, you should 116use the [Closure Library](https://developers.google.com/closure/library/). It 117contains functions for enforcing dependencies between scripts, and Closure Compiler 118will re-order the inputs automatically. 119 120## How to Contribute 121### Reporting a bug 1221. First make sure that it is really a bug and not simply the way that Closure Compiler works (especially true for ADVANCED_OPTIMIZATIONS). 123 * Check the [official documentation](https://developers.google.com/closure/compiler/) 124 * Consult the [FAQ](https://github.com/google/closure-compiler/wiki/FAQ) 125 * Search on [Stack Overflow](http://stackoverflow.com/questions/tagged/google-closure-compiler) and in the [Closure Compiler Discuss Group](https://groups.google.com/forum/#!forum/closure-compiler-discuss) 1262. If you still think you have found a bug, make sure someone hasn't already reported it. See the list of [known issues](https://github.com/google/closure-compiler/issues). 1273. If it hasn't been reported yet, post a new issue. Make sure to add enough detail so that the bug can be recreated. The smaller the reproduction code, the better. 128 129### Suggesting a Feature 1301. Consult the [FAQ](https://github.com/google/closure-compiler/wiki/FAQ) to make sure that the behaviour you would like isn't specifically excluded (such as string inlining). 1312. Make sure someone hasn't requested the same thing. See the list of [known issues](https://github.com/google/closure-compiler/issues). 1323. Read up on [what type of feature requests are accepted](https://github.com/google/closure-compiler/wiki/FAQ#how-do-i-submit-a-feature-request-for-a-new-type-of-optimization). 1334. Submit your request as an issue. 134 135### Submitting patches 1361. All contributors must sign a contributor license agreement (CLA). 137 A CLA basically says that you own the rights to any code you contribute, 138 and that you give us permission to use that code in Closure Compiler. 139 You maintain the copyright on that code. 140 If you own all the rights to your code, you can fill out an 141 [individual CLA](http://code.google.com/legal/individual-cla-v1.0.html). 142 If your employer has any rights to your code, then they also need to fill out 143 a [corporate CLA](http://code.google.com/legal/corporate-cla-v1.0.html). 144 If you don't know if your employer has any rights to your code, you should 145 ask before signing anything. 146 By default, anyone with an @google.com email address already has a CLA 147 signed for them. 1482. To make sure your changes are of the type that will be accepted, ask about your patch on the [Closure Compiler Discuss Group](https://groups.google.com/forum/#!forum/closure-compiler-discuss) 1493. Fork the repository. 1504. Make your changes. 1515. Submit a pull request for your changes. A project developer will review your work and then merge your request into the project. 152 153## Closure Compiler License 154 155Copyright 2009 The Closure Compiler Authors. 156 157Licensed under the Apache License, Version 2.0 (the "License"); 158you may not use this file except in compliance with the License. 159You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. 160 161Unless required by applicable law or agreed to in writing, software 162distributed under the License is distributed on an "AS IS" BASIS, 163WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 164See the License for the specific language governing permissions and 165limitations under the License. 166 167## Dependency Licenses 168 169### Rhino 170 171<table> 172 <tr> 173 <td>Code Path</td> 174 <td> 175 <code>src/com/google/javascript/rhino</code>, <code>test/com/google/javascript/rhino</code> 176 </td> 177 </tr> 178 179 <tr> 180 <td>URL</td> 181 <td>http://www.mozilla.org/rhino</td> 182 </tr> 183 184 <tr> 185 <td>Version</td> 186 <td>1.5R3, with heavy modifications</td> 187 </tr> 188 189 <tr> 190 <td>License</td> 191 <td>Netscape Public License and MPL / GPL dual license</td> 192 </tr> 193 194 <tr> 195 <td>Description</td> 196 <td>A partial copy of Mozilla Rhino. Mozilla Rhino is an 197implementation of JavaScript for the JVM. The JavaScript 198parse tree data structures were extracted and modified 199significantly for use by Google's JavaScript compiler.</td> 200 </tr> 201 202 <tr> 203 <td>Local Modifications</td> 204 <td>The packages have been renamespaced. All code not 205relevant to the parse tree has been removed. A JsDoc parser and static typing 206system have been added.</td> 207 </tr> 208</table> 209 210### Args4j 211 212<table> 213 <tr> 214 <td>Code Path</td> 215 <td><code>lib/args4j.jar</code></td> 216 </tr> 217 218 <tr> 219 <td>URL</td> 220 <td>https://args4j.dev.java.net/</td> 221 </tr> 222 223 <tr> 224 <td>Version</td> 225 <td>2.0.26</td> 226 </tr> 227 228 <tr> 229 <td>License</td> 230 <td>MIT</td> 231 </tr> 232 233 <tr> 234 <td>Description</td> 235 <td>args4j is a small Java class library that makes it easy to parse command line 236options/arguments in your CUI application.</td> 237 </tr> 238 239 <tr> 240 <td>Local Modifications</td> 241 <td>None</td> 242 </tr> 243</table> 244 245### Guava Libraries 246 247<table> 248 <tr> 249 <td>Code Path</td> 250 <td><code>lib/guava.jar</code></td> 251 </tr> 252 253 <tr> 254 <td>URL</td> 255 <td>https://github.com/google/guava</td> 256 </tr> 257 258 <tr> 259 <td>Version</td> 260 <td>20.0</td> 261 </tr> 262 263 <tr> 264 <td>License</td> 265 <td>Apache License 2.0</td> 266 </tr> 267 268 <tr> 269 <td>Description</td> 270 <td>Google's core Java libraries.</td> 271 </tr> 272 273 <tr> 274 <td>Local Modifications</td> 275 <td>None</td> 276 </tr> 277</table> 278 279### JSR 305 280 281<table> 282 <tr> 283 <td>Code Path</td> 284 <td><code>lib/jsr305.jar</code></td> 285 </tr> 286 287 <tr> 288 <td>URL</td> 289 <td>http://code.google.com/p/jsr-305/</td> 290 </tr> 291 292 <tr> 293 <td>Version</td> 294 <td>svn revision 47</td> 295 </tr> 296 297 <tr> 298 <td>License</td> 299 <td>BSD License</td> 300 </tr> 301 302 <tr> 303 <td>Description</td> 304 <td>Annotations for software defect detection.</td> 305 </tr> 306 307 <tr> 308 <td>Local Modifications</td> 309 <td>None</td> 310 </tr> 311</table> 312 313### JUnit 314 315<table> 316 <tr> 317 <td>Code Path</td> 318 <td><code>lib/junit.jar</code></td> 319 </tr> 320 321 <tr> 322 <td>URL</td> 323 <td>http://sourceforge.net/projects/junit/</td> 324 </tr> 325 326 <tr> 327 <td>Version</td> 328 <td>4.11</td> 329 </tr> 330 331 <tr> 332 <td>License</td> 333 <td>Common Public License 1.0</td> 334 </tr> 335 336 <tr> 337 <td>Description</td> 338 <td>A framework for writing and running automated tests in Java.</td> 339 </tr> 340 341 <tr> 342 <td>Local Modifications</td> 343 <td>None</td> 344 </tr> 345</table> 346 347### Protocol Buffers 348 349<table> 350 <tr> 351 <td>Code Path</td> 352 <td><code>lib/protobuf-java.jar</code></td> 353 </tr> 354 355 <tr> 356 <td>URL</td> 357 <td>https://github.com/google/protobuf</td> 358 </tr> 359 360 <tr> 361 <td>Version</td> 362 <td>2.5.0</td> 363 </tr> 364 365 <tr> 366 <td>License</td> 367 <td>New BSD License</td> 368 </tr> 369 370 <tr> 371 <td>Description</td> 372 <td>Supporting libraries for protocol buffers, 373an encoding of structured data.</td> 374 </tr> 375 376 <tr> 377 <td>Local Modifications</td> 378 <td>None</td> 379 </tr> 380</table> 381 382### Truth 383 384<table> 385 <tr> 386 <td>Code Path</td> 387 <td><code>lib/truth.jar</code></td> 388 </tr> 389 390 <tr> 391 <td>URL</td> 392 <td>https://github.com/google/truth</td> 393 </tr> 394 395 <tr> 396 <td>Version</td> 397 <td>0.24</td> 398 </tr> 399 400 <tr> 401 <td>License</td> 402 <td>Apache License 2.0</td> 403 </tr> 404 405 <tr> 406 <td>Description</td> 407 <td>Assertion/Proposition framework for Java unit tests</td> 408 </tr> 409 410 <tr> 411 <td>Local Modifications</td> 412 <td>None</td> 413 </tr> 414</table> 415 416### Ant 417 418<table> 419 <tr> 420 <td>Code Path</td> 421 <td> 422 <code>lib/ant.jar</code>, <code>lib/ant-launcher.jar</code> 423 </td> 424 </tr> 425 426 <tr> 427 <td>URL</td> 428 <td>http://ant.apache.org/bindownload.cgi</td> 429 </tr> 430 431 <tr> 432 <td>Version</td> 433 <td>1.8.1</td> 434 </tr> 435 436 <tr> 437 <td>License</td> 438 <td>Apache License 2.0</td> 439 </tr> 440 441 <tr> 442 <td>Description</td> 443 <td>Ant is a Java based build tool. In theory it is kind of like "make" 444without make's wrinkles and with the full portability of pure java code.</td> 445 </tr> 446 447 <tr> 448 <td>Local Modifications</td> 449 <td>None</td> 450 </tr> 451</table> 452 453### GSON 454 455<table> 456 <tr> 457 <td>Code Path</td> 458 <td><code>lib/gson.jar</code></td> 459 </tr> 460 461 <tr> 462 <td>URL</td> 463 <td>https://github.com/google/gson</td> 464 </tr> 465 466 <tr> 467 <td>Version</td> 468 <td>2.2.4</td> 469 </tr> 470 471 <tr> 472 <td>License</td> 473 <td>Apache license 2.0</td> 474 </tr> 475 476 <tr> 477 <td>Description</td> 478 <td>A Java library to convert JSON to Java objects and vice-versa</td> 479 </tr> 480 481 <tr> 482 <td>Local Modifications</td> 483 <td>None</td> 484 </tr> 485</table> 486 487### Node.js Closure Compiler Externs 488 489<table> 490 <tr> 491 <td>Code Path</td> 492 <td><code>contrib/nodejs</code></td> 493 </tr> 494 495 <tr> 496 <td>URL</td> 497 <td>https://github.com/dcodeIO/node.js-closure-compiler-externs</td> 498 </tr> 499 500 <tr> 501 <td>Version</td> 502 <td>e891b4fbcf5f466cc4307b0fa842a7d8163a073a</td> 503 </tr> 504 505 <tr> 506 <td>License</td> 507 <td>Apache 2.0 license</td> 508 </tr> 509 510 <tr> 511 <td>Description</td> 512 <td>Type contracts for NodeJS APIs</td> 513 </tr> 514 515 <tr> 516 <td>Local Modifications</td> 517 <td>Substantial changes to make them compatible with NpmCommandLineRunner.</td> 518 </tr> 519</table> 520