andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 1 | # Closure Compilation |
| 2 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 3 | ## What is type safety? |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 4 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 5 | [Strongly-typed languages](https://en.wikipedia.org/wiki/Strong_and_weak_typing) |
| 6 | like C++ and Java have the notion of variable types. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 7 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 8 | This is typically baked into how you declare variables: |
dbeam | 707cf27 | 2016-03-10 04:12:13 | [diff] [blame] | 9 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 10 | ```c++ |
| 11 | const int32 kUniversalAnswer = 42; // type = 32-bit integer |
dbeam | e3d03b7c | 2016-02-06 03:08:24 | [diff] [blame] | 12 | ``` |
| 13 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 14 | or as [templates](https://en.wikipedia.org/wiki/Template_metaprogramming) for |
| 15 | containers or generics: |
dbeam | e3d03b7c | 2016-02-06 03:08:24 | [diff] [blame] | 16 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 17 | ```c++ |
| 18 | std::vector<int64> fibonacci_numbers; // a vector of 64-bit integers |
dbeam | 43f31dd | 2016-03-09 22:05:59 | [diff] [blame] | 19 | ``` |
| 20 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 21 | When differently-typed variables interact with each other, the compiler can warn |
| 22 | you if there's no sane default action to take. |
dbeam | 707cf27 | 2016-03-10 04:12:13 | [diff] [blame] | 23 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 24 | Typing can also be manually annotated via mechanisms like `dynamic_cast` and |
| 25 | `static_cast` or older C-style casts (i.e. `(Type)`). |
| 26 | |
| 27 | Using stongly-typed languages provide _some_ level of protection against |
| 28 | accidentally using variables in the wrong context. |
| 29 | |
| 30 | JavaScript is weakly-typed and doesn't offer this safety by default. This makes |
| 31 | writing JavaScript more error prone, and various type errors have resulted in |
| 32 | real bugs seen by many users. |
| 33 | |
| 34 | ## Chrome's solution to typechecking JavaScript |
| 35 | |
| 36 | Enter [Closure Compiler](https://developers.google.com/closure/compiler/), a |
| 37 | tool for analyzing JavaScript and checking for syntax errors, variable |
| 38 | references, and other common JavaScript pitfalls. |
| 39 | |
| 40 | To get the fullest type safety possible, it's often required to annotate your |
| 41 | JavaScript explicitly with [Closure-flavored @jsdoc |
| 42 | tags](https://developers.google.com/closure/compiler/docs/js-for-compiler) |
| 43 | |
| 44 | ```js |
| 45 | /** |
| 46 | * @param {string} version A software version number (i.e. "50.0.2661.94"). |
qyearsley | c0dc6f4 | 2016-12-02 22:13:39 | [diff] [blame] | 47 | * @return {!Array<number>} Numbers corresponding to |version| (i.e. [50, 0, 2661, 94]). |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 48 | */ |
| 49 | function versionSplit(version) { |
| 50 | return version.split('.').map(Number); |
| 51 | } |
dbeam | 43f31dd | 2016-03-09 22:05:59 | [diff] [blame] | 52 | ``` |
| 53 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 54 | See also: |
| 55 | [the design doc](https://docs.google.com/a/chromium.org/document/d/1Ee9ggmp6U-lM-w9WmxN5cSLkK9B5YAq14939Woo-JY0/edit). |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 56 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 57 | ## Typechecking Your Javascript |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 58 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 59 | Given an example file structure of: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 60 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 61 | + lib/does_the_hard_stuff.js |
| 62 | + ui/makes_things_pretty.js |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 63 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 64 | `lib/does_the_hard_stuff.js`: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 65 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 66 | ```javascript |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 67 | var wit = 100; |
| 68 | |
| 69 | // ... later on, sneakily ... |
| 70 | |
| 71 | wit += ' IQ'; // '100 IQ' |
| 72 | ``` |
| 73 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 74 | `ui/makes_things_pretty.js`: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 75 | |
andybons | 6eaa0c0d | 2015-08-26 20:12:52 | [diff] [blame] | 76 | ```javascript |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 77 | /** @type {number} */ var mensa = wit + 50; |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 78 | |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 79 | alert(mensa); // '100 IQ50' instead of 150 |
| 80 | ``` |
| 81 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 82 | Closure compiler can notify us if we're using `string`s and `number`s in |
| 83 | dangerous ways. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 84 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 85 | To do this, we can create: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 86 | |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 87 | + ui/BUILD.gn |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 88 | |
| 89 | With these contents: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 90 | |
| 91 | ``` |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 92 | # Copyright 2018 The Chromium Authors. All rights reserved. |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 93 | # Use of this source code is governed by a BSD-style license that can be |
| 94 | # found in the LICENSE file. |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 95 | |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 96 | import("//third_party/closure_compiler/compile_js.gni") |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 97 | |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 98 | js_type_check("closure_compile") { |
| 99 | deps = [ |
| 100 | ":make_things_pretty", |
| 101 | ] |
| 102 | } |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 103 | |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 104 | js_library("make_things_pretty") { |
| 105 | deps = [ |
| 106 | "../lib:does_the_hard_stuff", |
| 107 | ] |
| 108 | |
| 109 | externs_list = [ |
| 110 | "$externs_path/extern_name_goes_here.js" |
| 111 | ] |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 112 | } |
| 113 | ``` |
| 114 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 115 | ## Running Closure compiler locally |
| 116 | |
| 117 | You can locally test that your code compiles on Linux or Mac. This requires |
| 118 | [Java](http://www.oracle.com/technetwork/java/javase/downloads/index.html) and a |
xiaoyin.l | 1003c0b | 2016-12-06 02:51:17 | [diff] [blame] | 119 | [Chrome checkout](https://www.chromium.org/developers/how-tos/get-the-code) (i.e. |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 120 | python, depot_tools). Note: on Ubuntu, you can probably just run `sudo apt-get |
| 121 | install openjdk-7-jre`. |
| 122 | |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 123 | After you set closure_compile = true in your gn args, you should be able to run: |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 124 | |
| 125 | ```shell |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 126 | ninja -C out/Default webui_closure_compile |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 127 | ``` |
| 128 | |
| 129 | and should see output like this: |
| 130 | |
| 131 | ```shell |
| 132 | ninja: Entering directory `out/Default/' |
| 133 | [0/1] ACTION Compiling ui/makes_things_pretty.js |
| 134 | ``` |
| 135 | |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 136 | To compile only a specific folder, add an argument after the script name: |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 137 | |
| 138 | ```shell |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 139 | ninja -C out/Default ui:closure_compile |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 140 | ``` |
| 141 | |
| 142 | In our example code, this error should appear: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 143 | |
| 144 | ``` |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 145 | (ERROR) Error in: ui/makes_things_pretty.js |
| 146 | ## /my/home/chromium/src/ui/makes_things_pretty.js:1: ERROR - initializing variable |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 147 | ## found : string |
| 148 | ## required: number |
| 149 | ## /** @type {number} */ var mensa = wit + 50; |
| 150 | ## ^ |
| 151 | ``` |
| 152 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 153 | Hooray! We can catch type errors in JavaScript! |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 154 | |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 155 | ## Preferred BUILD.gn structure |
| 156 | * Make all individual JS file targets a js\_library. |
| 157 | * The top level target should be called “closure\_compile”. |
| 158 | * If you have subfolders that need compiling, make “closure\_compile” a group(), |
| 159 | and any files in the current directory a js\_type\_check() called “<directory>\_resources”. |
| 160 | * Otherwise, just make “closure\_compile” a js\_type\_check with all your js\_library targets as deps |
| 161 | * Leave all closure targets below other kinds of targets becaure they’re less ‘important’ |
| 162 | |
| 163 | See also: |
| 164 | [Closure Compilation with GN](https://docs.google.com/a/chromium.org/document/d/1Ee9ggmp6U-lM-w9WmxN5cSLkK9B5YAq14939Woo-JY0/edit). |
| 165 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 166 | ## Trying your change |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 167 | |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 168 | Closure compilation runs in the compile step of Linux, Android and ChromeOS builds. |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 169 | |
| 170 | From the command line, you try your change with: |
| 171 | |
| 172 | ```shell |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 173 | git cl try -b linux_chromium_rel_ng |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 174 | ``` |
| 175 | |
dbeam | b0b1267 | 2016-05-03 21:33:13 | [diff] [blame] | 176 | ## Integrating with the continuous build |
| 177 | |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 178 | To compile your code on every commit, add your file to the |
| 179 | `'webui_closure_compile'` target in `src/BUILD.gn`: |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 180 | |
| 181 | ``` |
Christopher Lam | 213440d | 2018-06-14 03:26:18 | [diff] [blame] | 182 | group("webui_closure_compile") { |
| 183 | data_deps = [ |
| 184 | # Other projects |
| 185 | "my/project:closure_compile", |
| 186 | ] |
| 187 | } |
andybons | 3322f76 | 2015-08-24 21:37:09 | [diff] [blame] | 188 | ``` |
| 189 | |
michaelpg | c1e2d1e | 2017-05-01 23:40:59 | [diff] [blame] | 190 | ## Externs |
| 191 | |
| 192 | [Externs files](https://github.com/google/closure-compiler/wiki/FAQ#how-do-i-write-an-externs-file) |
| 193 | define APIs external to your JavaScript. They provide the compiler with the type |
| 194 | information needed to check usage of these APIs in your JavaScript, much like |
| 195 | forward declarations do in C++. |
| 196 | |
| 197 | Third-party libraries like Polymer often provide externs. Chrome must also |
| 198 | provide externs for its extension APIs. Whenever an extension API's `idl` or |
| 199 | `json` schema is updated in Chrome, the corresponding externs file must be |
| 200 | regenerated: |
| 201 | |
| 202 | ```shell |
| 203 | ./tools/json_schema_compiler/compiler.py -g externs \ |
| 204 | extensions/common/api/your_api_here.idl \ |
| 205 | > third_party/closure_compiler/externs/your_api_here.js |
| 206 | ``` |