OLD | NEW |
---|---|
1 # I just need to fix the compile! | 1 # Closure Compilation |
2 | 2 |
3 ## I just need to fix the compile! | |
4 | |
3 To locally run closure compiler like the bots, do this: | 5 To locally run closure compiler like the bots, do this: |
4 | 6 |
5 ``` | 7 ```shell |
6 cd $CHROMIUM_SRC | 8 cd $CHROMIUM_SRC |
7 # sudo apt-get install openjdk-7-jre # may be required | 9 # sudo apt-get install openjdk-7-jre # may be required |
8 GYP_GENERATORS=ninja tools/gyp/gyp --depth . third_party/closure_compiler/compil ed_resources.gyp | 10 GYP_GENERATORS=ninja tools/gyp/gyp --depth . \ |
11 third_party/closure_compiler/compiled_resources.gyp | |
9 ninja -C out/Default | 12 ninja -C out/Default |
10 ``` | 13 ``` |
11 | 14 |
12 # Background | 15 ## Background |
13 | 16 |
14 In C++ and Java, compiling the code gives you _some_ level of protection against misusing variables based on their type information. JavaScript is loosely type d and therefore doesn't offer this safety. This makes writing JavaScript more e rror prone as it's _one more thing_ to mess up. | 17 In C++ and Java, compiling the code gives you _some_ level of protection against |
18 misusing variables based on their type information. JavaScript is loosely typed | |
19 and therefore doesn't offer this safety. This makes writing JavaScript more | |
20 error prone as it's _one more thing_ to mess up. | |
15 | 21 |
16 Because having this safety is handy, Chrome now has a way to optionally typechec k your JavaScript and produce compiled output with [Closure Compiler](https://de velopers.google.com/closure/compiler/). | 22 Because having this safety is handy, Chrome now has a way to optionally |
23 typecheck your JavaScript and produce compiled output with | |
24 [Closure Compiler](https://developers.google.com/closure/compiler/). | |
17 | 25 |
18 See also: [the design doc](https://docs.google.com/a/chromium.org/document/d/1Ee 9ggmp6U-lM-w9WmxN5cSLkK9B5YAq14939Woo-JY0/edit). | 26 See also: |
27 [the design doc](https://docs.google.com/a/chromium.org/document/d/1Ee9ggmp6U-lM -w9WmxN5cSLkK9B5YAq14939Woo-JY0/edit). | |
19 | 28 |
20 # Assumptions | 29 ## Assumptions |
21 | 30 |
22 A working Chrome checkout. See here: http://www.chromium.org/developers/how-tos /get-the-code | 31 A working Chrome checkout. See here: |
32 http://www.chromium.org/developers/how-tos/get-the-code | |
23 | 33 |
24 # Typechecking Your Javascript | 34 ## Typechecking Your Javascript |
25 | 35 |
26 So you'd like to compile your JavaScript! | 36 So you'd like to compile your JavaScript! |
27 | 37 |
28 Maybe you're working on a page that looks like this: | 38 Maybe you're working on a page that looks like this: |
29 | 39 |
30 ``` | 40 ```html |
31 <script src="other_file.js"></script> | 41 <script src="other_file.js"></script> |
32 <script src="my_product/my_file.js"></script> | 42 <script src="my_product/my_file.js"></script> |
33 ``` | 43 ``` |
34 | 44 |
35 Where `other_file.js` contains: | 45 Where `other_file.js` contains: |
36 | 46 |
37 ``` | 47 ```javascript |
38 var wit = 100; | 48 var wit = 100; |
39 | 49 |
40 // ... later on, sneakily ... | 50 // ... later on, sneakily ... |
41 | 51 |
42 wit += ' IQ'; // '100 IQ' | 52 wit += ' IQ'; // '100 IQ' |
43 ``` | 53 ``` |
44 | 54 |
45 and `src/my_product/my_file.js` contains: | 55 and `src/my_product/my_file.js` contains: |
46 | 56 |
47 ``` | 57 ```javascript |
48 /** @type {number} */ var mensa = wit + 50; | 58 /** @type {number} */ var mensa = wit + 50; |
49 alert(mensa); // '100 IQ50' instead of 150 | 59 alert(mensa); // '100 IQ50' instead of 150 |
50 ``` | 60 ``` |
51 | 61 |
52 In order to check that our code acts as we'd expect, we can create a | 62 In order to check that our code acts as we'd expect, we can create a |
53 | 63 |
54 ``` | 64 ``` |
55 my_project/compiled_resources.gyp | 65 my_project/compiled_resources.gyp |
nodir
2015/08/26 16:26:17
remove ```, indent with 4 spaces
Bons
2015/08/26 19:43:14
Done.
| |
56 ``` | 66 ``` |
57 | 67 |
58 with the contents: | 68 with the contents: |
59 | 69 |
60 ``` | 70 ``` |
61 # Copyright 2015 The Chromium Authors. All rights reserved. | 71 # Copyright 2015 The Chromium Authors. All rights reserved. |
62 # Use of this source code is governed by a BSD-style license that can be | 72 # Use of this source code is governed by a BSD-style license that can be |
63 # found in the LICENSE file. | 73 # found in the LICENSE file. |
64 { | 74 { |
65 'targets': [ | 75 'targets': [ |
(...skipping 19 matching lines...) Expand all Loading... | |
85 | 95 |
86 ``` | 96 ``` |
87 (ERROR) Error in: my_project/my_file.js | 97 (ERROR) Error in: my_project/my_file.js |
88 ## /my/home/chromium/src/my_project/my_file.js:1: ERROR - initializing variable | 98 ## /my/home/chromium/src/my_project/my_file.js:1: ERROR - initializing variable |
89 ## found : string | 99 ## found : string |
90 ## required: number | 100 ## required: number |
91 ## /** @type {number} */ var mensa = wit + 50; | 101 ## /** @type {number} */ var mensa = wit + 50; |
92 ## ^ | 102 ## ^ |
93 ``` | 103 ``` |
94 | 104 |
95 Yay! We can easily find our unexpected type errors and write less error-prone c ode! | 105 Yay! We can easily find our unexpected type errors and write less error-prone |
106 code! | |
96 | 107 |
97 # Continuous Checking | 108 ## Continuous Checking |
98 | 109 |
99 To compile your code on every commit, add a line to [third\_party/closure\_compi ler/compiled\_resources.gyp](https://code.google.com/p/chromium/codesearch#chrom ium/src/third_party/closure_compiler/compiled_resources.gyp&sq=package:chromium& type=cs) like this: | 110 To compile your code on every commit, add a line to |
111 [third\_party/closure\_compiler/compiled\_resources.gyp](https://code.google.com /p/chromium/codesearch#chromium/src/third_party/closure_compiler/compiled_resour ces.gyp&sq=package:chromium&type=cs) | |
nodir
2015/08/26 16:26:17
replace link with
/third_party/closure_compiler
nodir
2015/08/26 16:26:17
replace \_ with _
MD is smart enough not to make b
Bons
2015/08/26 19:43:14
Done.
| |
112 like this: | |
100 | 113 |
101 ``` | 114 ``` |
102 { | 115 { |
103 'targets': [ | 116 'targets': [ |
104 { | 117 { |
105 'target_name': 'compile_all_resources', | 118 'target_name': 'compile_all_resources', |
106 'dependencies': [ | 119 'dependencies': [ |
107 # ... other projects ... | 120 # ... other projects ... |
108 ++ '../my_project/compiled_resources.gyp:*', | 121 ++ '../my_project/compiled_resources.gyp:*', |
109 ], | 122 ], |
110 } | 123 } |
111 ] | 124 ] |
112 } | 125 } |
113 ``` | 126 ``` |
114 | 127 |
115 and the [Closure compiler bot](http://build.chromium.org/p/chromium.fyi/builders /Closure%20Compilation%20Linux) will [re-]compile your code whenever relevant .j s files change. | 128 and the |
129 [Closure compiler bot](http://build.chromium.org/p/chromium.fyi/builders/Closure %20Compilation%20Linux) | |
130 will [re-]compile your code whenever relevant .js files change. | |
116 | 131 |
117 # Using Compiled JavaScript | 132 ## Using Compiled JavaScript |
118 | 133 |
119 Compiled JavaScript is output in src/out/<Debug|Release>/gen/closure/my\_project /my\_file.js along with a source map for use in debugging. In order to use the c ompiled JavaScript, we can create a | 134 Compiled JavaScript is output in |
135 `src/out/<Debug|Release>/gen/closure/my_project/my_file.js` along with a source | |
136 map for use in debugging. In order to use the compiled JavaScript, we can create | |
137 a | |
120 | 138 |
121 ``` | 139 ``` |
122 my_project/my_project_resources.gpy | 140 my_project/my_project_resources.gpy |
123 ``` | 141 ``` |
nodir
2015/08/26 16:26:17
4 line indent
Bons
2015/08/26 19:43:14
Done.
| |
124 | 142 |
125 with the contents: | 143 with the contents: |
126 | 144 |
127 ``` | 145 ``` |
128 # Copyright 2015 The Chromium Authors. All rights reserved. | 146 # Copyright 2015 The Chromium Authors. All rights reserved. |
129 # Use of this source code is governed by a BSD-style license that can be | 147 # Use of this source code is governed by a BSD-style license that can be |
130 # found in the LICENSE file. | 148 # found in the LICENSE file. |
131 | 149 |
132 { | 150 { |
133 'targets': [ | 151 'targets': [ |
(...skipping 17 matching lines...) Expand all Loading... | |
151 }, | 169 }, |
152 'includes': [ '../build/grit_action.gypi' ], | 170 'includes': [ '../build/grit_action.gypi' ], |
153 }, | 171 }, |
154 ], | 172 ], |
155 'includes': [ '../build/grit_target.gypi' ], | 173 'includes': [ '../build/grit_target.gypi' ], |
156 }, | 174 }, |
157 ], | 175 ], |
158 } | 176 } |
159 ``` | 177 ``` |
160 | 178 |
161 The variables can also be defined in an existing .gyp file if appropriate. The v ariables can then be used in to create a | 179 The variables can also be defined in an existing .gyp file if appropriate. The |
180 variables can then be used in to create a | |
162 | 181 |
163 ``` | 182 ``` |
164 my_project/my_project_resources.grd | 183 my_project/my_project_resources.grd |
165 ``` | 184 ``` |
166 | 185 |
167 with the contents: | 186 with the contents: |
168 | 187 |
169 ``` | 188 ``` |
170 <?xml version="1.0" encoding="utf-8"?> | 189 <?xml version="1.0" encoding="utf-8"?> |
171 <grit-part> | 190 <grit-part> |
172 <include name="IDR_MY_FILE_GEN_JS" file="${my_file_gen_js}" use_base_dir="fals e" type="BINDATA" /> | 191 <include name="IDR_MY_FILE_GEN_JS" file="${my_file_gen_js}" use_base_dir="fals e" type="BINDATA" /> |
173 </grit-part> | 192 </grit-part> |
174 ``` | 193 ``` |
175 | 194 |
176 In your C++, the resource can be retrieved like this: | 195 In your C++, the resource can be retrieved like this: |
177 ``` | 196 ``` |
178 base::string16 my_script = | 197 base::string16 my_script = |
179 base::UTF8ToUTF16( | 198 base::UTF8ToUTF16( |
180 ResourceBundle::GetSharedInstance() | 199 ResourceBundle::GetSharedInstance() |
181 .GetRawDataResource(IDR_MY_FILE_GEN_JS) | 200 .GetRawDataResource(IDR_MY_FILE_GEN_JS) |
182 .as_string()); | 201 .as_string()); |
183 ``` | 202 ``` |
184 | 203 |
185 # Debugging Compiled JavaScript | 204 ## Debugging Compiled JavaScript |
186 | 205 |
187 Along with the compiled JavaScript, a source map is created: src/out/<Debug|Rele ase>/gen/closure/my\_project/my\_file.js.map | 206 Along with the compiled JavaScript, a source map is created: |
207 `src/out/<Debug|Release>/gen/closure/my_project/my_file.js.map` | |
188 | 208 |
189 Chrome DevTools has built in support for working with source maps: [https://deve loper.chrome.com/devtools/docs/javascript-debugging#source-maps](https://develop er.chrome.com/devtools/docs/javascript-debugging#source-maps) | 209 Chrome DevTools has built in support for working with source maps: |
210 https://developer.chrome.com/devtools/docs/javascript-debugging#source-maps | |
190 | 211 |
191 In order to use the source map, you must first manually edit the path to the 'so urces' in the .js.map file that was generated. For example, if the source map lo oks like this: | 212 In order to use the source map, you must first manually edit the path to the |
213 'sources' in the .js.map file that was generated. For example, if the source map | |
214 looks like this: | |
215 | |
192 ``` | 216 ``` |
193 { | 217 { |
194 "version":3, | 218 "version":3, |
195 "file":"/tmp/gen/test_script.js", | 219 "file":"/tmp/gen/test_script.js", |
196 "lineCount":1, | 220 "lineCount":1, |
197 "mappings":"A,aAAA,IAAIA,OAASA,QAAQ,EAAG,CACtBC,KAAA,CAAM,OAAN,CADsB;", | 221 "mappings":"A,aAAA,IAAIA,OAASA,QAAQ,EAAG,CACtBC,KAAA,CAAM,OAAN,CADsB;", |
198 "sources":["/tmp/tmp70_QUi"], | 222 "sources":["/tmp/tmp70_QUi"], |
199 "names":["fooBar","alert"] | 223 "names":["fooBar","alert"] |
200 } | 224 } |
201 ``` | 225 ``` |
202 | 226 |
203 sources should be changed to: | 227 sources should be changed to: |
228 | |
204 ``` | 229 ``` |
205 ... | 230 ... |
206 "sources":["/tmp/test_script.js"], | 231 "sources":["/tmp/test_script.js"], |
207 ... | 232 ... |
208 ``` | 233 ``` |
209 | 234 |
210 In your browser, the source map can be loaded through the Chrome DevTools contex t menu that appears when you right click in the compiled JavaScript source body. A dialog will pop up prompting you for the path to the source map file. Once th e source map is loaded, the uncompiled version of the JavaScript will appear in the Sources panel on the left. You can set break points in the uncompiled versio n to help debug; behind the scenes Chrome will still be running the compiled ver sion of the JavaScript. | 235 In your browser, the source map can be loaded through the Chrome DevTools |
236 context menu that appears when you right click in the compiled JavaScript source | |
237 body. A dialog will pop up prompting you for the path to the source map file. | |
238 Once the source map is loaded, the uncompiled version of the JavaScript will | |
239 appear in the Sources panel on the left. You can set break points in the | |
240 uncompiled version to help debug; behind the scenes Chrome will still be running | |
241 the compiled version of the JavaScript. | |
211 | 242 |
212 # Additional Arguments | 243 ## Additional Arguments |
213 | 244 |
214 compile\_js.gypi accepts an optional script\_args variable, which passes additio nal arguments to compile.py, as well as an optional closure\_args variable, whic h passes additional arguments to the closure compiler. You may also override the disabled\_closure\_args for more strict compilation. | 245 `compile_js.gypi` accepts an optional `script_args` variable, which passes |
246 additional arguments to `compile.py`, as well as an optional `closure_args` | |
247 variable, which passes additional arguments to the closure compiler. You may | |
248 also override the `disabled_closure_args` for more strict compilation. | |
215 | 249 |
216 For example, if you would like to specify multiple sources, strict compilation, and an output wrapper, you would create a | 250 For example, if you would like to specify multiple sources, strict compilation, |
251 and an output wrapper, you would create a | |
217 | 252 |
218 ``` | 253 ``` |
219 my_project/compiled_resources.gyp | 254 my_project/compiled_resources.gyp |
220 ``` | 255 ``` |
221 | 256 |
222 with contents similar to this: | 257 with contents similar to this: |
223 ``` | 258 ``` |
224 # Copyright 2015 The Chromium Authors. All rights reserved. | 259 # Copyright 2015 The Chromium Authors. All rights reserved. |
225 # Use of this source code is governed by a BSD-style license that can be | 260 # Use of this source code is governed by a BSD-style license that can be |
226 # found in the LICENSE file. | 261 # found in the LICENSE file. |
(...skipping 12 matching lines...) Expand all Loading... | |
239 'jscomp_error=reportUnknownTypes', # the following three provide m ore strict compilation | 274 'jscomp_error=reportUnknownTypes', # the following three provide m ore strict compilation |
240 'jscomp_error=duplicate', | 275 'jscomp_error=duplicate', |
241 'jscomp_error=misplacedTypeAnnotation', | 276 'jscomp_error=misplacedTypeAnnotation', |
242 ], | 277 ], |
243 'disabled_closure_args': [], # remove the disabled closure args for more strict compilation | 278 'disabled_closure_args': [], # remove the disabled closure args for more strict compilation |
244 }, | 279 }, |
245 'includes': ['../third_party/closure_compiler/compile_js.gypi'], | 280 'includes': ['../third_party/closure_compiler/compile_js.gypi'], |
246 }, | 281 }, |
247 ], | 282 ], |
248 } | 283 } |
249 ``` | 284 ``` |
OLD | NEW |