| OLD | NEW |
| 1 # Closure Compilation | 1 # Closure Compilation |
| 2 | 2 |
| 3 ## I just need to fix the compile! | 3 ## I just need to fix the compile! |
| 4 | 4 |
| 5 ### Pre-requisites | 5 ### Pre-requisites |
| 6 | 6 |
| 7 You'll need Java 7 (preferably the OpenJDK version). To install on Ubuntu: | 7 You'll need Java 7 (preferably the OpenJDK version). To install on Ubuntu: |
| 8 | 8 |
| 9 ```shell | 9 ```shell |
| 10 sudo apt-get install openjdk-7-jre | 10 sudo apt-get install openjdk-7-jre |
| 11 ``` | 11 ``` |
| 12 | 12 |
| 13 On Mac or Windows, visit: | 13 On Mac or Windows, visit: |
| 14 [http://www.oracle.com/technetwork/java/javase/downloads/index.html](http://www.
oracle.com/technetwork/java/javase/downloads/index.html) | 14 [http://www.oracle.com/technetwork/java/javase/downloads/index.html](http://www.
oracle.com/technetwork/java/javase/downloads/index.html) |
| 15 | 15 |
| 16 ### Using ninja to compile the code | 16 ### Using ninja to compile the code |
| 17 | 17 |
| 18 We use GYP and ninja as our build system. To generate the ninja files from GYP: | 18 We use GYP and ninja as our build system. To generate the ninja files from GYP: |
| 19 | 19 |
| 20 ```shell | 20 ```shell |
| 21 # notice the 2 in compiled_resources.gyp | 21 # notice the 2 in compiled_resources2.gyp |
| 22 GYP_GENERATORS=ninja tools/gyp/gyp --depth . third_party/closure_compiler/compil
ed_resources2.gyp | 22 GYP_GENERATORS=ninja tools/gyp/gyp --depth . third_party/closure_compiler/compil
ed_resources2.gyp |
| 23 ``` | 23 ``` |
| 24 | 24 |
| 25 To compile the JavaScript: | 25 To compile the JavaScript: |
| 26 | 26 |
| 27 ```shell | 27 ```shell |
| 28 ninja -C out/Default -j4 | 28 ninja -C out/Default -j4 |
| 29 ``` | 29 ``` |
| 30 | 30 |
| 31 The output should look something like this: | 31 The output should look something like this: |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 92 |
| 93 and `src/my_product/my_file.js` contains: | 93 and `src/my_product/my_file.js` contains: |
| 94 | 94 |
| 95 ```javascript | 95 ```javascript |
| 96 /** @type {number} */ var mensa = wit + 50; | 96 /** @type {number} */ var mensa = wit + 50; |
| 97 alert(mensa); // '100 IQ50' instead of 150 | 97 alert(mensa); // '100 IQ50' instead of 150 |
| 98 ``` | 98 ``` |
| 99 | 99 |
| 100 In order to check that our code acts as we'd expect, we can create a | 100 In order to check that our code acts as we'd expect, we can create a |
| 101 | 101 |
| 102 my_project/compiled_resources.gyp | 102 my_project/compiled_resources2.gyp |
| 103 | 103 |
| 104 with the contents: | 104 with the contents: |
| 105 | 105 |
| 106 ``` | 106 ``` |
| 107 # Copyright 2015 The Chromium Authors. All rights reserved. | 107 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 108 # Use of this source code is governed by a BSD-style license that can be | 108 # Use of this source code is governed by a BSD-style license that can be |
| 109 # found in the LICENSE file. | 109 # found in the LICENSE file. |
| 110 { | 110 { |
| 111 'targets': [ | 111 'targets': [ |
| 112 { | 112 { |
| 113 'target_name': 'my_file', # file name without ".js" | 113 'target_name': 'my_file', # file name without ".js" |
| 114 | 114 'dependencies': [ # No need to specify empty lists. |
| 115 'variables': { # Only use if necessary (no need to specify empty lists). | 115 '../compiled_resources2.gyp:other_file', |
| 116 'depends': [ | 116 '<(EXTERNS_GYP):any_needed_externs' # e.g. chrome.send(), chrome.app.wi
ndow, etc. |
| 117 'other_file.js', # or 'other_project/compiled_resources.gyp:target', | 117 ], |
| 118 ], | 118 'includes': ['../third_party/closure_compiler/compile_js2.gypi'], |
| 119 'externs': [ | |
| 120 '<(CLOSURE_DIR)/externs/any_needed_externs.js' # e.g. chrome.send(),
chrome.app.window, etc. | |
| 121 ], | |
| 122 }, | |
| 123 | |
| 124 'includes': ['../third_party/closure_compiler/compile_js.gypi'], | |
| 125 }, | 119 }, |
| 126 ], | 120 ], |
| 127 } | 121 } |
| 128 ``` | 122 ``` |
| 129 | 123 |
| 130 You should get results like: | 124 You should get results like: |
| 131 | 125 |
| 132 ``` | 126 ``` |
| 133 (ERROR) Error in: my_project/my_file.js | 127 (ERROR) Error in: my_project/my_file.js |
| 134 ## /my/home/chromium/src/my_project/my_file.js:1: ERROR - initializing variable | 128 ## /my/home/chromium/src/my_project/my_file.js:1: ERROR - initializing variable |
| (...skipping 12 matching lines...) Expand all Loading... |
| 147 /third_party/closure_compiler/compiled_resources.gyp | 141 /third_party/closure_compiler/compiled_resources.gyp |
| 148 like this: | 142 like this: |
| 149 | 143 |
| 150 ``` | 144 ``` |
| 151 { | 145 { |
| 152 'targets': [ | 146 'targets': [ |
| 153 { | 147 { |
| 154 'target_name': 'compile_all_resources', | 148 'target_name': 'compile_all_resources', |
| 155 'dependencies': [ | 149 'dependencies': [ |
| 156 # ... other projects ... | 150 # ... other projects ... |
| 157 ++ '../my_project/compiled_resources.gyp:*', | 151 ++ '../my_project/compiled_resources2.gyp:*', |
| 158 ], | 152 ], |
| 159 } | 153 } |
| 160 ] | 154 ] |
| 161 } | 155 } |
| 162 ``` | 156 ``` |
| 163 | 157 |
| 164 and the | 158 and the |
| 165 [Closure compiler bot](http://build.chromium.org/p/chromium.fyi/builders/Closure
%20Compilation%20Linux) | 159 [Closure compiler bot](http://build.chromium.org/p/chromium.fyi/builders/Closure
%20Compilation%20Linux) |
| 166 will [re-]compile your code whenever relevant .js files change. | 160 will [re-]compile your code whenever relevant .js files change. |
| 167 | 161 |
| 168 ## Using Compiled JavaScript | 162 ## Using Compiled JavaScript |
| 169 | 163 |
| 170 Compiled JavaScript is output in | 164 Compiled JavaScript is output in |
| 171 `src/out/<Debug|Release>/gen/closure/my_project/my_file.js` along with a source | 165 `src/out/<Debug|Release>/gen/closure/my_project/my_file.js` along with a source |
| 172 map for use in debugging. In order to use the compiled JavaScript, we can create | 166 map for use in debugging. In order to use the compiled JavaScript, we can create |
| 173 a | 167 a |
| 174 | 168 |
| 175 my_project/my_project_resources.gpy | 169 my_project/my_project_resources.gyp |
| 176 | 170 |
| 177 with the contents: | 171 with the contents: |
| 178 | 172 |
| 179 ``` | 173 ``` |
| 180 # Copyright 2015 The Chromium Authors. All rights reserved. | 174 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 181 # Use of this source code is governed by a BSD-style license that can be | 175 # Use of this source code is governed by a BSD-style license that can be |
| 182 # found in the LICENSE file. | 176 # found in the LICENSE file. |
| 183 | 177 |
| 184 { | 178 { |
| 185 'targets': [ | 179 'targets': [ |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 with the contents: | 212 with the contents: |
| 219 | 213 |
| 220 ``` | 214 ``` |
| 221 <?xml version="1.0" encoding="utf-8"?> | 215 <?xml version="1.0" encoding="utf-8"?> |
| 222 <grit-part> | 216 <grit-part> |
| 223 <include name="IDR_MY_FILE_GEN_JS" file="${my_file_gen_js}" use_base_dir="fals
e" type="BINDATA" /> | 217 <include name="IDR_MY_FILE_GEN_JS" file="${my_file_gen_js}" use_base_dir="fals
e" type="BINDATA" /> |
| 224 </grit-part> | 218 </grit-part> |
| 225 ``` | 219 ``` |
| 226 | 220 |
| 227 In your C++, the resource can be retrieved like this: | 221 In your C++, the resource can be retrieved like this: |
| 222 |
| 228 ``` | 223 ``` |
| 229 base::string16 my_script = | 224 base::string16 my_script = |
| 230 base::UTF8ToUTF16( | 225 base::UTF8ToUTF16( |
| 231 ResourceBundle::GetSharedInstance() | 226 ResourceBundle::GetSharedInstance() |
| 232 .GetRawDataResource(IDR_MY_FILE_GEN_JS) | 227 .GetRawDataResource(IDR_MY_FILE_GEN_JS) |
| 233 .as_string()); | 228 .as_string()); |
| 234 ``` | 229 ``` |
| 235 | 230 |
| 236 ## Debugging Compiled JavaScript | 231 ## Debugging Compiled JavaScript |
| 237 | 232 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 also override the `disabled_closure_args` for more strict compilation. | 275 also override the `disabled_closure_args` for more strict compilation. |
| 281 | 276 |
| 282 For example, if you would like to specify multiple sources, strict compilation, | 277 For example, if you would like to specify multiple sources, strict compilation, |
| 283 and an output wrapper, you would create a | 278 and an output wrapper, you would create a |
| 284 | 279 |
| 285 ``` | 280 ``` |
| 286 my_project/compiled_resources.gyp | 281 my_project/compiled_resources.gyp |
| 287 ``` | 282 ``` |
| 288 | 283 |
| 289 with contents similar to this: | 284 with contents similar to this: |
| 285 |
| 290 ``` | 286 ``` |
| 291 # Copyright 2015 The Chromium Authors. All rights reserved. | 287 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 292 # Use of this source code is governed by a BSD-style license that can be | 288 # Use of this source code is governed by a BSD-style license that can be |
| 293 # found in the LICENSE file. | 289 # found in the LICENSE file. |
| 294 { | 290 { |
| 295 'targets' :[ | 291 'targets' :[ |
| 296 { | 292 { |
| 297 'target_name': 'my_file', | 293 'target_name': 'my_file', |
| 298 'variables': { | 294 'variables': { |
| 299 'source_files': [ | 295 'source_files': [ |
| 300 'my_file.js', | 296 'my_file.js', |
| 301 'my_file2.js', | 297 'my_file2.js', |
| 302 ], | 298 ], |
| 303 'script_args': ['--no-single-file'], # required to process multiple file
s at once | 299 'script_args': ['--no-single-file'], # required to process multiple file
s at once |
| 304 'closure_args': [ | 300 'closure_args': [ |
| 305 'output_wrapper=\'(function(){%output%})();\'', | 301 'output_wrapper=\'(function(){%output%})();\'', |
| 306 'jscomp_error=reportUnknownTypes', # the following three provide m
ore strict compilation | 302 'jscomp_error=reportUnknownTypes', # the following three provide m
ore strict compilation |
| 307 'jscomp_error=duplicate', | 303 'jscomp_error=duplicate', |
| 308 'jscomp_error=misplacedTypeAnnotation', | 304 'jscomp_error=misplacedTypeAnnotation', |
| 309 ], | 305 ], |
| 310 'disabled_closure_args': [], # remove the disabled closure args for more
strict compilation | 306 'disabled_closure_args': [], # remove the disabled closure args for more
strict compilation |
| 311 }, | 307 }, |
| 312 'includes': ['../third_party/closure_compiler/compile_js.gypi'], | 308 'includes': ['../third_party/closure_compiler/compile_js.gypi'], |
| 313 }, | 309 }, |
| 314 ], | 310 ], |
| 315 } | 311 } |
| 316 ``` | 312 ``` |
| OLD | NEW |