OLD | NEW |
1 # Closure Compilation | 1 # Closure Compilation |
2 | 2 |
3 ## What is type safety? | 3 ## What is type safety? |
4 | 4 |
5 [Strongly-typed languages](https://en.wikipedia.org/wiki/Strong_and_weak_typing) | 5 [Strongly-typed languages](https://en.wikipedia.org/wiki/Strong_and_weak_typing) |
6 like C++ and Java have the notion of variable types. | 6 like C++ and Java have the notion of variable types. |
7 | 7 |
8 This is typically baked into how you declare variables: | 8 This is typically baked into how you declare variables: |
9 | 9 |
10 ```c++ | 10 ```c++ |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 75 |
76 ```javascript | 76 ```javascript |
77 /** @type {number} */ var mensa = wit + 50; | 77 /** @type {number} */ var mensa = wit + 50; |
78 | 78 |
79 alert(mensa); // '100 IQ50' instead of 150 | 79 alert(mensa); // '100 IQ50' instead of 150 |
80 ``` | 80 ``` |
81 | 81 |
82 Closure compiler can notify us if we're using `string`s and `number`s in | 82 Closure compiler can notify us if we're using `string`s and `number`s in |
83 dangerous ways. | 83 dangerous ways. |
84 | 84 |
85 To do this, we can create: | 85 To do this, we can create (or add to): |
86 | 86 |
87 + ui/compiled_resources2.gyp | 87 + ui/BUILD.gn |
88 | 88 |
89 With these contents: | 89 With these contents: |
90 | 90 |
91 ``` | 91 ``` |
| 92 |
92 # Copyright 2016 The Chromium Authors. All rights reserved. | 93 # Copyright 2016 The Chromium Authors. All rights reserved. |
93 # Use of this source code is governed by a BSD-style license that can be | 94 # Use of this source code is governed by a BSD-style license that can be |
94 # found in the LICENSE file. | 95 # found in the LICENSE file. |
95 { | 96 import("//third_party/closure_compiler/compile_js2.gni") |
96 'targets': [ | |
97 { | |
98 # Target names is typically just without ".js" | |
99 'target_name': 'makes_things_pretty', | |
100 | 97 |
101 'dependencies': [ | 98 # Target name is typically compile_<root file name> but can be anything |
102 '../lib/compiled_resources2.gyp:does_the_hard_stuff', | 99 compile_js("compile_make_things_pretty") { |
| 100 source_files = ["make_things_pretty.js"] |
| 101 deps = ["../lib:compile_does_the_hard_stuff"] |
| 102 externs = "extern_name_goes_here" |
| 103 } |
| 104 ... |
103 | 105 |
104 # Teaches closure about non-standard environments/APIs, e.g. | 106 # Create a group to include all compilations in this GN file |
105 # chrome.send(), chrome.app.window, etc. | 107 group("compile_js") { |
106 '<(EXTERNS_GYP):extern_name_goes_here' | 108 deps = [ |
107 ], | 109 :compile_make_things_pretty, |
| 110 ... |
| 111 ] |
| 112 } |
108 | 113 |
109 'includes': ['../path/to/third_party/closure_compiler/compile_js2.gypi'], | 114 ``` |
110 }, | 115 |
111 ], | 116 To include this in builds add your new target to the `'deps'` list in |
| 117 `src/third_party/closure_compiler/BUILD.gn`: |
| 118 |
| 119 ``` |
| 120 group("compile_js_all") { |
| 121 deps = [ |
| 122 # ... other projects ... |
| 123 ++ "//my_project:compile_js", |
| 124 ] |
112 } | 125 } |
113 ``` | 126 ``` |
114 | 127 |
115 ## Running Closure compiler locally | 128 ## Running Closure compiler locally |
116 | 129 |
117 You can locally test that your code compiles on Linux or Mac. This requires | 130 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 | 131 [Java](http://www.oracle.com/technetwork/java/javase/downloads/index.html) and a |
119 [Chrome checkout](http://www.chromium.org/developers/how-tos/get-the-code) (i.e. | 132 [Chrome checkout](http://www.chromium.org/developers/how-tos/get-the-code) (i.e. |
120 python, depot_tools). Note: on Ubuntu, you can probably just run `sudo apt-get | 133 python, depot_tools). Note: on Ubuntu, you can probably just run `sudo apt-get |
121 install openjdk-7-jre`. | 134 install openjdk-7-jre`. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 To automatically check that your code typechecks cleanly before submitting, you | 180 To automatically check that your code typechecks cleanly before submitting, you |
168 can add this line to your CL description: | 181 can add this line to your CL description: |
169 | 182 |
170 ``` | 183 ``` |
171 CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:closure_compilation | 184 CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:closure_compilation |
172 ``` | 185 ``` |
173 | 186 |
174 Working in common resource directories in Chrome automatically adds this line | 187 Working in common resource directories in Chrome automatically adds this line |
175 for you. | 188 for you. |
176 | 189 |
177 ## Integrating with the continuous build | |
178 | |
179 To compile your code on every commit, add your file to the `'dependencies'` list | |
180 in `src/third_party/closure_compiler/compiled_resources2.gyp`: | |
181 | |
182 ``` | |
183 { | |
184 'targets': [ | |
185 { | |
186 'target_name': 'compile_all_resources', | |
187 'dependencies': [ | |
188 # ... other projects ... | |
189 ++ '../my_project/compiled_resources2.gyp:*', | |
190 ], | |
191 } | |
192 ] | |
193 } | |
194 ``` | |
195 | 190 |
196 This file is used by the | 191 This file is used by the |
197 [Closure compiler bot](http://build.chromium.org/p/chromium.fyi/builders/Closure
%20Compilation%20Linux) | 192 [Closure compiler bot](http://build.chromium.org/p/chromium.fyi/builders/Closure
%20Compilation%20Linux) |
198 to automatically compile your code on every commit. | 193 to automatically compile your code on every commit. |
OLD | NEW |