Debian Codegraph @

util/closureCompiler/README.md

raw symbols SCIP
1 # [Google Closure Compiler](https://developers.google.com/closure/compiler/)
2
3 [![Build Status](https://github.com/google/closure-compiler/workflows/Compiler%20CI/badge.svg)](https://github.com/google/closure-compiler/actions)
4 [![Open Source Helpers](https://www.codetriage.com/google/closure-compiler/badges/users.svg)](https://www.codetriage.com/google/closure-compiler)
5 [![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg)](https://github.com/google/closure-compiler/blob/master/code_of_conduct.md)
6
7 The [Closure Compiler](https://developers.google.com/closure/compiler/) is a
8 tool for making JavaScript download and run faster. It is a true compiler for
9 JavaScript. Instead of compiling from a source language to machine code, it
10 compiles from JavaScript to better JavaScript. It parses your JavaScript,
11 analyzes it, removes dead code and rewrites and minimizes what's left. It also
12 checks syntax, variable references, and types, and warns about common JavaScript
13 pitfalls.
14
15 ## Getting Started
16
17 The easiest way to install the compiler is with [NPM](https://npmjs.com) or
18 [Yarn](https://yarnpkg.com):
19
20 ```bash
21 yarn global add google-closure-compiler
22 # OR
23 npm i -g google-closure-compiler
24 ```
25
26 The package manager will link the binary for you, and you can access the
27 compiler with:
28
29 ```bash
30 google-closure-compiler
31 ```
32
33 This starts the compiler in interactive mode. Type:
34
35 ```javascript
36 var x = 17 + 25;
37 ```
38
39 Hit `Enter`, then `Ctrl+Z` (on Windows) or `Ctrl+D` (on Mac/Linux), then `Enter`
40 again. The Compiler will respond with the compiled output (using `SIMPLE` mode
41 by default):
42
43 ```javascript
44 var x=42;
45 ```
46
47 #### Downloading from Maven Repository
48
49 A pre-compiled release of the compiler is also available via
50 [Maven](https://mvnrepository.com/artifact/com.google.javascript/closure-compiler).
51
52 ### Basic usage
53
54 The Closure Compiler has many options for reading input from a file, writing
55 output to a file, checking your code, and running optimizations. Here is a
56 simple example of compressing a JS program:
57
58 ```bash
59 google-closure-compiler --js file.js --js_output_file file.out.js
60 ```
61
62 We get the **most benefit** from the compiler if we give it **all of our source
63 code** (see [Compiling Multiple Scripts](#compiling-multiple-scripts)), which
64 allows us to use `ADVANCED` optimizations:
65
66 ```bash
67 google-closure-compiler -O ADVANCED rollup.js --js_output_file rollup.min.js
68 ```
69
70 To see all of the compiler's options, type:
71
72 ```bash
73 google-closure-compiler --help
74 ```
75
76 <table>
77 <thead>
78 <tr>
79 <th><code>--flag</code></th>
80 <th>Description</th>
81 </tr>
82 </thead>
83 <tbody>
84 <tr>
85 <td><code>--compilation_level (-O)</code></td>
86 <td>
87 Specifies the compilation level to use.
88 Options: <code>BUNDLE</code>, <code>WHITESPACE_ONLY</code>,
89 <code>SIMPLE</code> (default), <code>ADVANCED</code>
90 </td>
91 </tr>
92 <tr>
93 <td><code>--env</code></td>
94 <td>
95 Determines the set of builtin externs to load.
96 Options: <code>BROWSER</code>, <code>CUSTOM</code>.
97 Defaults to <code>BROWSER</code>.
98 </td>
99 </tr>
100 <tr>
101 <td><code>--externs</code></td>
102 <td>The file containing JavaScript externs. You may specify multiple</td>
103 </tr>
104 <tr>
105 <td><code>--js</code></td>
106 <td>
107 The JavaScript filename. You may specify multiple. The flag name is
108 optional, because args are interpreted as files by default. You may also
109 use minimatch-style glob patterns. For example, use
110 <code>--js='**.js' --js='!**_test.js'</code> to recursively include all
111 js files that do not end in <code>_test.js</code>
112 </td>
113 </tr>
114 <tr>
115 <td><code>--js_output_file</code></td>
116 <td>
117 Primary output filename. If not specified, output is written to stdout.
118 </td>
119 </tr>
120 <tr>
121 <td><code>--language_in</code></td>
122 <td>
123 Sets the language spec to which input sources should conform.
124 Options: <code>ECMASCRIPT3</code>, <code>ECMASCRIPT5</code>,
125 <code>ECMASCRIPT5_STRICT</code>, <code>ECMASCRIPT_2015</code>,
126 <code>ECMASCRIPT_2016</code>, <code>ECMASCRIPT_2017</code>,
127 <code>ECMASCRIPT_2018</code>, <code>ECMASCRIPT_2019</code>,
128 <code>STABLE</code>, <code>ECMASCRIPT_NEXT</code>
129 </td>
130 </tr>
131 <tr>
132 <td><code>--language_out</code></td>
133 <td>
134 Sets the language spec to which output should conform.
135 Options: <code>ECMASCRIPT3</code>, <code>ECMASCRIPT5</code>,
136 <code>ECMASCRIPT5_STRICT</code>, <code>ECMASCRIPT_2015</code>,
137 <code>ECMASCRIPT_2016</code>, <code>ECMASCRIPT_2017</code>,
138 <code>ECMASCRIPT_2018</code>, <code>ECMASCRIPT_2019</code>,
139 <code>STABLE</code>
140 </td>
141 </tr>
142 <tr>
143 <td><code>--warning_level (-W)</code></td>
144 <td>Specifies the warning level to use.
145 Options: <code>QUIET</code>, <code>DEFAULT</code>, <code>VERBOSE</code>
146 </td>
147 </tr>
148 </tbody>
149 </table>
150
151 #### 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.
152
153 ### NodeJS API
154
155 You can access the compiler in a JS program by importing
156 `google-closure-compiler`:
157
158 ```javascript
159 import closureCompiler from 'google-closure-compiler';
160 const { compiler } = closureCompiler;
161
162 new compiler({
163 js: 'file-one.js',
164 compilation_level: 'ADVANCED'
165 });
166 ```
167
168 This package will provide programmatic access to the native Graal binary in most
169 cases, and will fall back to the Java version otherwise.
170
171 #### Please see the [closure-compiler-npm](https://github.com/google/closure-compiler-npm/tree/master/packages/google-closure-compiler) repository for documentation on accessing the compiler in JS.
172
173 ## Compiling Multiple Scripts
174
175 If you have multiple scripts, you should compile them all together with one
176 compile command.
177
178 ```bash
179 google-closure-compiler in1.js in2.js in3.js --js_output_file out.js
180 ```
181
182 You can also use minimatch-style globs.
183
184 ```bash
185 # Recursively include all js files in subdirs
186 google-closure-compiler 'src/**.js' --js_output_file out.js
187
188 # Recursively include all js files in subdirs, excluding test files.
189 # Use single-quotes, so that bash doesn't try to expand the '!'
190 google-closure-compiler 'src/**.js' '!**_test.js' --js_output_file out.js
191 ```
192
193 The Closure Compiler will concatenate the files in the order they're passed at
194 the command line.
195
196 If you're using globs or many files, you may start to run into problems with
197 managing dependencies between scripts. In this case, you should use the
198 [Closure Library](https://developers.google.com/closure/library/). It contains
199 functions for enforcing dependencies between scripts, and Closure Compiler will
200 re-order the inputs automatically.
201
202 ## Getting Help
203
204 1. Post in the
205 [Closure Compiler Discuss Group](https://groups.google.com/forum/#!forum/closure-compiler-discuss).
206 2. Ask a question on
207 [Stack Overflow](https://stackoverflow.com/questions/tagged/google-closure-compiler).
208 3. Consult the [FAQ](https://github.com/google/closure-compiler/wiki/FAQ).
209
210 ## Building the Compiler
211
212 To build the compiler yourself, you will need the following:
213
214 Prerequisite | Description
215 -------------------------------------------------------------------------- | -----------
216 [Java 8 or later](https://java.com) | Used to compile the compiler's source code.
217 [Git](https://git-scm.com/) | Used by Bazel to download dependencies.
218 [Bazelisk](https://docs.bazel.build/versions/master/install-bazelisk.html) | Used to build the various compiler targets.
219
220 ### Installing Bazelisk
221
222 Bazelisk is a wrapper around Bazel that dynamically loads the appropriate
223 version of Bazel for a given repository. Using it prevents spurious errors that
224 result from using the wrong version of Bazel to build the compiler, as well as
225 makes it easy to use different Bazel versions for other projects.
226
227 Bazelisk is available through many package managers. Feel free to use whichever
228 you're most comfortable with.
229
230 [Instructions for installing Bazelisk](https://docs.bazel.build/versions/master/install-bazelisk.html).
231
232 ### Building from a terminal
233
234 You can trigger the build process easily with package.json scripts or by calling
235 Bazel manually.
236
237 ```bash
238 # bazelisk build //:compiler_unshaded_deploy.jar
239 yarn build
240
241 # bazelisk build :all
242 yarn build:all
243 ```
244
245 ### Testing from a terminal
246
247 Tests can be executed in a similar way. The following command will run all tests
248 in the repo.
249
250 ```bash
251 # bazelisk test //:all
252 ```
253
254 There are hundreds of individual test targets, so it will take a few
255 minutes to run all of them. While developing, it's usually better to specify
256 the exact tests you're interested in.
257
258 ```bash
259 bazelisk test //:$path_to_test_file
260 ```
261
262 ### Building from an IDE
263
264 See [Bazel IDE Integrations](https://docs.bazel.build/versions/master/ide.html).
265
266 ### Running
267
268 Once the compiler has been built, the compiled JAR will be in the `bazel-bin/`
269 directory. You can access it with a call to `java -jar ...` or by using the
270 package.json script:
271
272 ```bash
273 # java -jar bazel-bin/compiler_unshaded_deploy.jar [...args]
274 yarn compile [...args]
275 ```
276
277 #### Running using Eclipse
278
279 1. Open the class `src/com/google/javascript/jscomp/CommandLineRunner.java` or
280 create your own extended version of the class.
281 2. Run the class in Eclipse.
282 3. See the instructions above on how to use the interactive mode - but beware
283 of the
284 [bug](https://stackoverflow.com/questions/4711098/passing-end-of-transmission-ctrl-d-character-in-eclipse-cdt-console)
285 regarding passing "End of Transmission" in the Eclipse console.
286
287 ## Contributing
288
289 ### Contributor code of conduct
290
291 However you choose to contribute, please abide by our
292 [code of conduct](https://github.com/google/closure-compiler/blob/master/code_of_conduct.md) to
293 keep our community a healthy and welcoming place.
294
295 ### Reporting a bug
296
297 1. First make sure that it is really a bug and not simply the way that Closure
298 Compiler works (especially true for ADVANCED_OPTIMIZATIONS).
299 * Check the
300 [official documentation](https://developers.google.com/closure/compiler/)
301 * Consult the [FAQ](https://github.com/google/closure-compiler/wiki/FAQ)
302 * Search on
303 [Stack Overflow](https://stackoverflow.com/questions/tagged/google-closure-compiler)
304 and in the
305 [Closure Compiler Discuss Group](https://groups.google.com/forum/#!forum/closure-compiler-discuss)
306 * Look through the list of
307 [compiler assumptions](https://github.com/google/closure-compiler/wiki/Compiler-Assumptions).
308 2. If you still think you have found a bug, make sure someone hasn't already
309 reported it. See the list of
310 [known issues](https://github.com/google/closure-compiler/issues).
311 3. If it hasn't been reported yet, post a new issue. Make sure to add enough
312 detail so that the bug can be recreated. The smaller the reproduction code,
313 the better.
314
315 ### Suggesting a feature
316
317 1. Consult the [FAQ](https://github.com/google/closure-compiler/wiki/FAQ) to
318 make sure that the behaviour you would like isn't specifically excluded
319 (such as string inlining).
320 2. Make sure someone hasn't requested the same thing. See the list of
321 [known issues](https://github.com/google/closure-compiler/issues).
322 3. Read up on
323 [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).
324 4. Submit your request as an issue.
325
326 ### Submitting patches
327
328 1. All contributors must sign a contributor license agreement (CLA). A CLA
329 basically says that you own the rights to any code you contribute, and that
330 you give us permission to use that code in Closure Compiler. You maintain
331 the copyright on that code. If you own all the rights to your code, you can
332 fill out an
333 [individual CLA](https://code.google.com/legal/individual-cla-v1.0.html). If
334 your employer has any rights to your code, then they also need to fill out a
335 [corporate CLA](https://code.google.com/legal/corporate-cla-v1.0.html). If
336 you don't know if your employer has any rights to your code, you should ask
337 before signing anything. By default, anyone with an @google.com email
338 address already has a CLA signed for them.
339 2. To make sure your changes are of the type that will be accepted, ask about
340 your patch on the
341 [Closure Compiler Discuss Group](https://groups.google.com/forum/#!forum/closure-compiler-discuss)
342 3. Fork the repository.
343 4. Make your changes. Check out our
344 [coding conventions](https://github.com/google/closure-compiler/wiki/Contributors#coding-conventions)
345 for details on making sure your code is in correct style.
346 5. Submit a pull request for your changes. A project developer will review your
347 work and then merge your request into the project.
348
349 ## Closure Compiler License
350
351 Copyright 2009 The Closure Compiler Authors.
352
353 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
354 this file except in compliance with the License. You may obtain a copy of the
355 License at http://www.apache.org/licenses/LICENSE-2.0.
356
357 Unless required by applicable law or agreed to in writing, software distributed
358 under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
359 CONDITIONS OF ANY KIND, either express or implied. See the License for the
360 specific language governing permissions and limitations under the License.
361
362 ## Dependency Licenses
363
364 ### Rhino
365
366 <table>
367 <tr>
368 <td>Code Path</td>
369 <td>
370 <code>src/com/google/javascript/rhino</code>, <code>test/com/google/javascript/rhino</code>
371 </td>
372 </tr>
373
374 <tr>
375 <td>URL</td>
376 <td>https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino</td>
377 </tr>
378
379 <tr>
380 <td>Version</td>
381 <td>1.5R3, with heavy modifications</td>
382 </tr>
383
384 <tr>
385 <td>License</td>
386 <td>Netscape Public License and MPL / GPL dual license</td>
387 </tr>
388
389 <tr>
390 <td>Description</td>
391 <td>A partial copy of Mozilla Rhino. Mozilla Rhino is an
392 implementation of JavaScript for the JVM. The JavaScript parse tree data
393 structures were extracted and modified significantly for use by Google's
394 JavaScript compiler.</td>
395 </tr>
396
397 <tr>
398 <td>Local Modifications</td>
399 <td>The packages have been renamespaced. All code not
400 relevant to the parse tree has been removed. A JsDoc parser and static typing
401 system have been added.</td>
402 </tr>
403 </table>
404
405 ### Args4j
406
407 <table>
408 <tr>
409 <td>URL</td>
410 <td>http://args4j.kohsuke.org/</td>
411 </tr>
412
413 <tr>
414 <td>Version</td>
415 <td>2.33</td>
416 </tr>
417
418 <tr>
419 <td>License</td>
420 <td>MIT</td>
421 </tr>
422
423 <tr>
424 <td>Description</td>
425 <td>args4j is a small Java class library that makes it easy to parse command line
426 options/arguments in your CUI application.</td>
427 </tr>
428
429 <tr>
430 <td>Local Modifications</td>
431 <td>None</td>
432 </tr>
433 </table>
434
435 ### Guava Libraries
436
437 <table>
438 <tr>
439 <td>URL</td>
440 <td>https://github.com/google/guava</td>
441 </tr>
442
443 <tr>
444 <td>Version</td>
445 <td>31.0.1</td>
446 </tr>
447
448 <tr>
449 <td>License</td>
450 <td>Apache License 2.0</td>
451 </tr>
452
453 <tr>
454 <td>Description</td>
455 <td>Google's core Java libraries.</td>
456 </tr>
457
458 <tr>
459 <td>Local Modifications</td>
460 <td>None</td>
461 </tr>
462 </table>
463
464 ### JSR 305
465
466 <table>
467 <tr>
468 <td>URL</td>
469 <td>https://github.com/findbugsproject/findbugs</td>
470 </tr>
471
472 <tr>
473 <td>Version</td>
474 <td>3.0.1</td>
475 </tr>
476
477 <tr>
478 <td>License</td>
479 <td>BSD License</td>
480 </tr>
481
482 <tr>
483 <td>Description</td>
484 <td>Annotations for software defect detection.</td>
485 </tr>
486
487 <tr>
488 <td>Local Modifications</td>
489 <td>None</td>
490 </tr>
491 </table>
492
493 ### JUnit
494
495 <table>
496 <tr>
497 <td>URL</td>
498 <td>http://junit.org/junit4/</td>
499 </tr>
500
501 <tr>
502 <td>Version</td>
503 <td>4.12</td>
504 </tr>
505
506 <tr>
507 <td>License</td>
508 <td>Common Public License 1.0</td>
509 </tr>
510
511 <tr>
512 <td>Description</td>
513 <td>A framework for writing and running automated tests in Java.</td>
514 </tr>
515
516 <tr>
517 <td>Local Modifications</td>
518 <td>None</td>
519 </tr>
520 </table>
521
522 ### Protocol Buffers
523
524 <table>
525 <tr>
526 <td>URL</td>
527 <td>https://github.com/google/protobuf</td>
528 </tr>
529
530 <tr>
531 <td>Version</td>
532 <td>3.0.2</td>
533 </tr>
534
535 <tr>
536 <td>License</td>
537 <td>New BSD License</td>
538 </tr>
539
540 <tr>
541 <td>Description</td>
542 <td>Supporting libraries for protocol buffers,
543 an encoding of structured data.</td>
544 </tr>
545
546 <tr>
547 <td>Local Modifications</td>
548 <td>None</td>
549 </tr>
550 </table>
551
552 ### RE2/J
553
554 <table>
555 <tr>
556 <td>URL</td>
557 <td>https://github.com/google/re2j</td>
558 </tr>
559
560 <tr>
561 <td>Version</td>
562 <td>1.3</td>
563 </tr>
564
565 <tr>
566 <td>License</td>
567 <td>New BSD License</td>
568 </tr>
569
570 <tr>
571 <td>Description</td>
572 <td>Linear time regular expression matching in Java.</td>
573 </tr>
574
575 <tr>
576 <td>Local Modifications</td>
577 <td>None</td>
578 </tr>
579 </table>
580
581 ### Truth
582
583 <table>
584 <tr>
585 <td>URL</td>
586 <td>https://github.com/google/truth</td>
587 </tr>
588
589 <tr>
590 <td>Version</td>
591 <td>1.1</td>
592 </tr>
593
594 <tr>
595 <td>License</td>
596 <td>Apache License 2.0</td>
597 </tr>
598
599 <tr>
600 <td>Description</td>
601 <td>Assertion/Proposition framework for Java unit tests</td>
602 </tr>
603
604 <tr>
605 <td>Local Modifications</td>
606 <td>None</td>
607 </tr>
608 </table>
609
610 ### Ant
611
612 <table>
613 <tr>
614 <td>URL</td>
615 <td>https://ant.apache.org/bindownload.cgi</td>
616 </tr>
617
618 <tr>
619 <td>Version</td>
620 <td>1.10.11</td>
621 </tr>
622
623 <tr>
624 <td>License</td>
625 <td>Apache License 2.0</td>
626 </tr>
627
628 <tr>
629 <td>Description</td>
630 <td>Ant is a Java based build tool. In theory it is kind of like "make"
631 without make's wrinkles and with the full portability of pure java code.</td>
632 </tr>
633
634 <tr>
635 <td>Local Modifications</td>
636 <td>None</td>
637 </tr>
638 </table>
639
640 ### GSON
641
642 <table>
643 <tr>
644 <td>URL</td>
645 <td>https://github.com/google/gson</td>
646 </tr>
647
648 <tr>
649 <td>Version</td>
650 <td>2.7</td>
651 </tr>
652
653 <tr>
654 <td>License</td>
655 <td>Apache license 2.0</td>
656 </tr>
657
658 <tr>
659 <td>Description</td>
660 <td>A Java library to convert JSON to Java objects and vice-versa</td>
661 </tr>
662
663 <tr>
664 <td>Local Modifications</td>
665 <td>None</td>
666 </tr>
667 </table>
668
669 ### Node.js Closure Compiler Externs
670
671 <table>
672 <tr>
673 <td>Code Path</td>
674 <td><code>contrib/nodejs</code></td>
675 </tr>
676
677 <tr>
678 <td>URL</td>
679 <td>https://github.com/dcodeIO/node.js-closure-compiler-externs</td>
680 </tr>
681
682 <tr>
683 <td>Version</td>
684 <td>e891b4fbcf5f466cc4307b0fa842a7d8163a073a</td>
685 </tr>
686
687 <tr>
688 <td>License</td>
689 <td>Apache 2.0 license</td>
690 </tr>
691
692 <tr>
693 <td>Description</td>
694 <td>Type contracts for NodeJS APIs</td>
695 </tr>
696
697 <tr>
698 <td>Local Modifications</td>
699 <td>Substantial changes to make them compatible with NpmCommandLineRunner.</td>
700 </tr>
701 </table>