3 ## I just need to fix the compile!
5 To locally run closure compiler like the bots, do this:
9 # sudo apt-get install openjdk-7-jre # may be required
10 GYP_GENERATORS=ninja tools/gyp/gyp --depth . \
11 third_party/closure_compiler/compiled_resources.gyp
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.
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/).
27 [the design doc](https://docs.google.com/a/chromium.org/document/d/1Ee9ggmp6U-lM-w9WmxN5cSLkK9B5YAq14939Woo-JY0/edit).
31 A working Chrome checkout. See here:
32 http://www.chromium.org/developers/how-tos/get-the-code
34 ## Typechecking Your Javascript
36 So you'd like to compile your JavaScript!
38 Maybe you're working on a page that looks like this:
41 <script src="other_file.js"></script>
42 <script src="my_product/my_file.js"></script>
45 Where `other_file.js` contains:
50 // ... later on, sneakily ...
52 wit += ' IQ'; // '100 IQ'
55 and `src/my_product/my_file.js` contains:
58 /** @type {number} */ var mensa = wit + 50;
59 alert(mensa); // '100 IQ50' instead of 150
62 In order to check that our code acts as we'd expect, we can create a
64 my_project/compiled_resources.gyp
69 # Copyright 2015 The Chromium Authors. All rights reserved.
70 # Use of this source code is governed by a BSD-style license that can be
71 # found in the LICENSE file.
75 'target_name': 'my_file', # file name without ".js"
77 'variables': { # Only use if necessary (no need to specify empty lists).
79 'other_file.js', # or 'other_project/compiled_resources.gyp:target',
82 '<(CLOSURE_DIR)/externs/any_needed_externs.js' # e.g. chrome.send(), chrome.app.window, etc.
86 'includes': ['../third_party/closure_compiler/compile_js.gypi'],
92 You should get results like:
95 (ERROR) Error in: my_project/my_file.js
96 ## /my/home/chromium/src/my_project/my_file.js:1: ERROR - initializing variable
99 ## /** @type {number} */ var mensa = wit + 50;
103 Yay! We can easily find our unexpected type errors and write less error-prone
106 ## Continuous Checking
108 To compile your code on every commit, add a line to
109 /third_party/closure_compiler/compiled_resources.gyp
116 'target_name': 'compile_all_resources',
118 # ... other projects ...
119 ++ '../my_project/compiled_resources.gyp:*',
127 [Closure compiler bot](http://build.chromium.org/p/chromium.fyi/builders/Closure%20Compilation%20Linux)
128 will [re-]compile your code whenever relevant .js files change.
130 ## Using Compiled JavaScript
132 Compiled JavaScript is output in
133 `src/out/<Debug|Release>/gen/closure/my_project/my_file.js` along with a source
134 map for use in debugging. In order to use the compiled JavaScript, we can create
137 my_project/my_project_resources.gpy
142 # Copyright 2015 The Chromium Authors. All rights reserved.
143 # Use of this source code is governed by a BSD-style license that can be
144 # found in the LICENSE file.
149 # GN version: //my_project/resources
150 'target_name': 'my_project_resources',
153 'grit_out_dir': '<(SHARED_INTERMEDIATE_DIR)/my_project',
154 'my_file_gen_js': '<(SHARED_INTERMEDIATE_DIR)/closure/my_project/my_file.js',
158 # GN version: //my_project/resources:my_project_resources
159 'action_name': 'generate_my_project_resources',
161 'grit_grd_file': 'resources/my_project_resources.grd',
162 'grit_additional_defines': [
163 '-E', 'my_file_gen_js=<(my_file_gen_js)',
166 'includes': [ '../build/grit_action.gypi' ],
169 'includes': [ '../build/grit_target.gypi' ],
175 The variables can also be defined in an existing .gyp file if appropriate. The
176 variables can then be used in to create a
178 my_project/my_project_resources.grd
183 <?xml version="1.0" encoding="utf-8"?>
185 <include name="IDR_MY_FILE_GEN_JS" file="${my_file_gen_js}" use_base_dir="false" type="BINDATA" />
189 In your C++, the resource can be retrieved like this:
191 base::string16 my_script =
193 ResourceBundle::GetSharedInstance()
194 .GetRawDataResource(IDR_MY_FILE_GEN_JS)
198 ## Debugging Compiled JavaScript
200 Along with the compiled JavaScript, a source map is created:
201 `src/out/<Debug|Release>/gen/closure/my_project/my_file.js.map`
203 Chrome DevTools has built in support for working with source maps:
204 https://developer.chrome.com/devtools/docs/javascript-debugging#source-maps
206 In order to use the source map, you must first manually edit the path to the
207 'sources' in the .js.map file that was generated. For example, if the source map
213 "file":"/tmp/gen/test_script.js",
215 "mappings":"A,aAAA,IAAIA,OAASA,QAAQ,EAAG,CACtBC,KAAA,CAAM,OAAN,CADsB;",
216 "sources":["/tmp/tmp70_QUi"],
217 "names":["fooBar","alert"]
221 sources should be changed to:
225 "sources":["/tmp/test_script.js"],
229 In your browser, the source map can be loaded through the Chrome DevTools
230 context menu that appears when you right click in the compiled JavaScript source
231 body. A dialog will pop up prompting you for the path to the source map file.
232 Once the source map is loaded, the uncompiled version of the JavaScript will
233 appear in the Sources panel on the left. You can set break points in the
234 uncompiled version to help debug; behind the scenes Chrome will still be running
235 the compiled version of the JavaScript.
237 ## Additional Arguments
239 `compile_js.gypi` accepts an optional `script_args` variable, which passes
240 additional arguments to `compile.py`, as well as an optional `closure_args`
241 variable, which passes additional arguments to the closure compiler. You may
242 also override the `disabled_closure_args` for more strict compilation.
244 For example, if you would like to specify multiple sources, strict compilation,
245 and an output wrapper, you would create a
248 my_project/compiled_resources.gyp
251 with contents similar to this:
253 # Copyright 2015 The Chromium Authors. All rights reserved.
254 # Use of this source code is governed by a BSD-style license that can be
255 # found in the LICENSE file.
259 'target_name': 'my_file',
265 'script_args': ['--no-single-file'], # required to process multiple files at once
267 'output_wrapper=\'(function(){%output%})();\'',
268 'jscomp_error=reportUnknownTypes', # the following three provide more strict compilation
269 'jscomp_error=duplicate',
270 'jscomp_error=misplacedTypeAnnotation',
272 'disabled_closure_args': [], # remove the disabled closure args for more strict compilation
274 'includes': ['../third_party/closure_compiler/compile_js.gypi'],