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/).
25 The type information is
26 [annotated in comment tags](https://developers.google.com/closure/compiler/docs/js-for-compiler)
27 that are briefly described below.
30 [the design doc](https://docs.google.com/a/chromium.org/document/d/1Ee9ggmp6U-lM-w9WmxN5cSLkK9B5YAq14939Woo-JY0/edit).
34 A working Chrome checkout. See here:
35 http://www.chromium.org/developers/how-tos/get-the-code
37 ## Typechecking Your Javascript
39 So you'd like to compile your JavaScript!
41 Maybe you're working on a page that looks like this:
44 <script src="other_file.js"></script>
45 <script src="my_product/my_file.js"></script>
48 Where `other_file.js` contains:
53 // ... later on, sneakily ...
55 wit += ' IQ'; // '100 IQ'
58 and `src/my_product/my_file.js` contains:
61 /** @type {number} */ var mensa = wit + 50;
62 alert(mensa); // '100 IQ50' instead of 150
65 In order to check that our code acts as we'd expect, we can create a
67 my_project/compiled_resources.gyp
72 # Copyright 2015 The Chromium Authors. All rights reserved.
73 # Use of this source code is governed by a BSD-style license that can be
74 # found in the LICENSE file.
78 'target_name': 'my_file', # file name without ".js"
80 'variables': { # Only use if necessary (no need to specify empty lists).
82 'other_file.js', # or 'other_project/compiled_resources.gyp:target',
85 '<(CLOSURE_DIR)/externs/any_needed_externs.js' # e.g. chrome.send(), chrome.app.window, etc.
89 'includes': ['../third_party/closure_compiler/compile_js.gypi'],
95 You should get results like:
98 (ERROR) Error in: my_project/my_file.js
99 ## /my/home/chromium/src/my_project/my_file.js:1: ERROR - initializing variable
102 ## /** @type {number} */ var mensa = wit + 50;
106 Yay! We can easily find our unexpected type errors and write less error-prone
109 ## Continuous Checking
111 To compile your code on every commit, add a line to
112 /third_party/closure_compiler/compiled_resources.gyp
119 'target_name': 'compile_all_resources',
121 # ... other projects ...
122 ++ '../my_project/compiled_resources.gyp:*',
130 [Closure compiler bot](http://build.chromium.org/p/chromium.fyi/builders/Closure%20Compilation%20Linux)
131 will [re-]compile your code whenever relevant .js files change.
133 ## Using Compiled JavaScript
135 Compiled JavaScript is output in
136 `src/out/<Debug|Release>/gen/closure/my_project/my_file.js` along with a source
137 map for use in debugging. In order to use the compiled JavaScript, we can create
140 my_project/my_project_resources.gpy
145 # Copyright 2015 The Chromium Authors. All rights reserved.
146 # Use of this source code is governed by a BSD-style license that can be
147 # found in the LICENSE file.
152 # GN version: //my_project/resources
153 'target_name': 'my_project_resources',
156 'grit_out_dir': '<(SHARED_INTERMEDIATE_DIR)/my_project',
157 'my_file_gen_js': '<(SHARED_INTERMEDIATE_DIR)/closure/my_project/my_file.js',
161 # GN version: //my_project/resources:my_project_resources
162 'action_name': 'generate_my_project_resources',
164 'grit_grd_file': 'resources/my_project_resources.grd',
165 'grit_additional_defines': [
166 '-E', 'my_file_gen_js=<(my_file_gen_js)',
169 'includes': [ '../build/grit_action.gypi' ],
172 'includes': [ '../build/grit_target.gypi' ],
178 The variables can also be defined in an existing .gyp file if appropriate. The
179 variables can then be used in to create a
181 my_project/my_project_resources.grd
186 <?xml version="1.0" encoding="utf-8"?>
188 <include name="IDR_MY_FILE_GEN_JS" file="${my_file_gen_js}" use_base_dir="false" type="BINDATA" />
192 In your C++, the resource can be retrieved like this:
194 base::string16 my_script =
196 ResourceBundle::GetSharedInstance()
197 .GetRawDataResource(IDR_MY_FILE_GEN_JS)
201 ## Debugging Compiled JavaScript
203 Along with the compiled JavaScript, a source map is created:
204 `src/out/<Debug|Release>/gen/closure/my_project/my_file.js.map`
206 Chrome DevTools has built in support for working with source maps:
207 https://developer.chrome.com/devtools/docs/javascript-debugging#source-maps
209 In order to use the source map, you must first manually edit the path to the
210 'sources' in the .js.map file that was generated. For example, if the source map
216 "file":"/tmp/gen/test_script.js",
218 "mappings":"A,aAAA,IAAIA,OAASA,QAAQ,EAAG,CACtBC,KAAA,CAAM,OAAN,CADsB;",
219 "sources":["/tmp/tmp70_QUi"],
220 "names":["fooBar","alert"]
224 sources should be changed to:
228 "sources":["/tmp/test_script.js"],
232 In your browser, the source map can be loaded through the Chrome DevTools
233 context menu that appears when you right click in the compiled JavaScript source
234 body. A dialog will pop up prompting you for the path to the source map file.
235 Once the source map is loaded, the uncompiled version of the JavaScript will
236 appear in the Sources panel on the left. You can set break points in the
237 uncompiled version to help debug; behind the scenes Chrome will still be running
238 the compiled version of the JavaScript.
240 ## Additional Arguments
242 `compile_js.gypi` accepts an optional `script_args` variable, which passes
243 additional arguments to `compile.py`, as well as an optional `closure_args`
244 variable, which passes additional arguments to the closure compiler. You may
245 also override the `disabled_closure_args` for more strict compilation.
247 For example, if you would like to specify multiple sources, strict compilation,
248 and an output wrapper, you would create a
251 my_project/compiled_resources.gyp
254 with contents similar to this:
256 # Copyright 2015 The Chromium Authors. All rights reserved.
257 # Use of this source code is governed by a BSD-style license that can be
258 # found in the LICENSE file.
262 'target_name': 'my_file',
268 'script_args': ['--no-single-file'], # required to process multiple files at once
270 'output_wrapper=\'(function(){%output%})();\'',
271 'jscomp_error=reportUnknownTypes', # the following three provide more strict compilation
272 'jscomp_error=duplicate',
273 'jscomp_error=misplacedTypeAnnotation',
275 'disabled_closure_args': [], # remove the disabled closure args for more strict compilation
277 'includes': ['../third_party/closure_compiler/compile_js.gypi'],