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