blob: 219aa3e5f66e1afdde8fa9ff4b358f80faf5f9d3 [file] [log] [blame] [view]
Rebekah Potterd38c2c42022-06-28 21:21:221<style>
2.doc h1 {
3 margin: 0;
4}
5
6.doc h3,
7.doc h4 {
8 font-weight: bold;
9}
10
11.doc h4 {
12 font-style: italic;
13}
14</style>
15
16# WebUI Build Configuration
17
18[TOC]
19
20## WebUI BUILD.gn files
21WebUI builds are configured using BUILD.gn files, which live in the top level
22directory for a WebUI. For example, the BUILD.gn file for the Downloads page
23is located at chrome/browser/resources/downloads/BUILD.gn.
24
25These files specify the build steps necessary to convert the checked-in code to
26the code that should be served at runtime.
27
Rebekah Potter2b9d2692023-02-01 01:16:2028## General guidance for using WebUI build rules
29* WebUI build rules should typically only be used in WebUI-related folders
30 (e.g. chrome/browser/resources, ui/webui/resources, and folders within
31 components/ and content/ that contain WebUI code).
32
33* WebUI build rules should only be used directly or via the wrapper rules
34 documented here ([build_webui](#build_webui),
Mike Frysingerfa3a0c92023-06-08 02:33:0135 [build_webui_tests](#build_webui_tests)). If you want to use any of the rules
dpapadf7a2c7a2023-03-16 20:26:2836 below from within another tool or script, please get a review from one of the
Mike Frysingerfa3a0c92023-06-08 02:33:0137 cross-platform, non-backend WebUI [OWNERS](/ui/webui/PLATFORM_OWNERS).
Rebekah Potter2b9d2692023-02-01 01:16:2038
Rebekah Potterd38c2c42022-06-28 21:21:2239## WebUI build rules
40
41### **html_to_wrapper, css_to_wrapper**
Mike Frysingerfa3a0c92023-06-08 02:33:0142
Rebekah Potterd38c2c42022-06-28 21:21:2243These rules are used to inline HTML or CSS into a TypeScript file which can be
44compiled by TS compiler and then imported with JS imports at runtime. This is
45necessary when writing Web Components, which need to return their HTML in the
46`template()` getter method.
47
48By default, these rules accept input files from within the current directory.
49
50By default, they output the wrapped `.html.ts` and `.css.ts` files to the target
51generated directory (|target_gen_dir|).
Rebekah Potterd38c2c42022-06-28 21:21:2252
53#### **Arguments**
54```
55in_files: specifies the list of files to process with respect to the
56 |in_folder|.
dpapad07cd33f2023-01-20 19:08:3057template: html_to_wrapper only. Valid values are:
58 - "polymer" (default)
59 - "native" (use when wrapping the HTML template of a non-Polymer web
60 component)
61 - "detect" (use when there are both Polymer and native web components)
dpapadf237f35bc2022-07-01 23:29:0762in_folder: Specifies the input folder where files are located. If not specified,
63 the current directory (of the BUILD.gn file) is used.
64out_folder: Specifies the location to write the wrapped files. If not specified,
65 |target_gen_dir| is used.
dpapad0fb37802022-07-13 01:00:5266minify: Whether to minify HTML/CSS with
dpapadd55b8f72022-06-29 19:37:3267 third_party/node/node_modules/html-minifier. Defaults to false.
dpapad6de5a2e2022-07-26 16:22:1968use_js: Whether to output .js files instead of .ts files. Defaults to false.
dpapad2efd4452023-04-06 01:43:4569scheme: One of ['chrome', 'relative']. Defaults to 'relative'. Specifies whether
dpapad24071ff2022-09-02 01:07:5470 dependencies of the generated wrapper file should be imported with
71 "chrome://resources" or scheme-relative "//resources" URLs.
Rebekah Potterd38c2c42022-06-28 21:21:2272```
73
74#### **Example**
75```
76import("//tools/polymer/html_to_wrapper.gni")
77import("//tools/polymer/css_to_wrapper.gni")
78
79# Generates "my_web_component.html.ts" in |target_gen_dir|.
80html_to_wrapper("html_wrapper_files") {
81 in_files = [ "my_web_component.html" ]
82}
83
84# Generates "my_style_module.css.ts" in |target_gen_dir|.
85css_to_wrapper("css_wrapper_files") {
86 in_files = [ "my_style_module.css" ]
87}
88```
89
90### **preprocess_if_expr**
Mike Frysingerfa3a0c92023-06-08 02:33:0191
Rebekah Potterd38c2c42022-06-28 21:21:2292This rule is used to preprocess files containing `<if expr="*">`. These
93expressions are most frequently used to enable code to only run on certain
94platforms.
dpapadbb1e3aa82022-06-29 05:31:1095
96By default, reads input files from within the current directory and saves output
97in |target_gen_dir|.
Rebekah Potterd38c2c42022-06-28 21:21:2298
99#### **Arguments**
100```
101in_folder: specifies the input folder, where all input files are located.
dpapadbb1e3aa82022-06-29 05:31:10102 Defaults to the folder where the BUILD.gn file resides.
Rebekah Potterd38c2c42022-06-28 21:21:22103out_folder: specifies the folder that the preprocessed files should be placed
dpapadbb1e3aa82022-06-29 05:31:10104 in. This must be a generated directory. Defaults to
105 |target_gen_dir|.
Rebekah Potterd38c2c42022-06-28 21:21:22106in_files: specifies the list of input files to preprocess with respect to the
107 |in_folder|.
108out_manifest: Specifies a file where the list of output files and their
109 directory should be written. This is most useful when the
110 preprocessed files need to be listed in a grd file. Since
111 preprocessed files are instead passed to ts_library in WebUIs
112 that have been migrated to TypeScript, this is only used by
113 WebUIs that have not been migrated to TypeScript yet.
114```
115#### **Example:**
116```
117import("//tools/grit/preprocess_if_expr.gni")
118
119# Preprocesses "my_web_component.html.ts" and my_style_module.css.ts in
120# |target_gen_dir|, into "$target_gen_dir/preprocessed".
121preprocess_if_expr("preprocess_generated") {
122 # Depend on the targets that generates these files.
123 deps = [
124 ":css_wrapper_files",
125 ":html_wrapper_files",
126 ]
127 in_folder = target_gen_dir
128 in_files = [
129 "my_style_module.css.ts",
130 "my_web_component.html.ts",
131 ]
132 out_folder = "$target_gen_dir/$preprocess_folder"
133}
134
135# Preprocess "my_web_component.ts" and "my_webui.ts" in the src dir into
136# "$target_gen_dir/preprocessed".
137preprocess_if_expr("preprocess_src") {
138 in_folder = "."
139 in_files = [
140 "my_web_component.ts",
141 "my_webui.ts",
142 ]
143 out_folder = "$target_gen_dir/$preprocess_folder"
144}
145```
146
147### **ts_library**
Mike Frysingerfa3a0c92023-06-08 02:33:01148
Rebekah Potterd38c2c42022-06-28 21:21:22149This rule is used to compile TypeScript code to JavaScript. It outputs JS files
150corresponding to each TS file passed as an input into the designated output
151directory, and generates a manifest listing all the files it has output named
Luciano Pachecoc3c35042022-07-01 09:33:39152$target_name.manifest in the target generated directory.
Rebekah Potterd38c2c42022-06-28 21:21:22153
154#### **Input File notes**
155```
156All input files must be valid TypeScript. This means, among other things, that
157files shouldn't contain any `<if expr>`, i.e. they should already have been
158preprocessed, if necessary. It also means that e.g. HTML or image files
159shouldn't be passed to ts_library.
160
161All files that aren't imported with an absolute path (e.g.
162`import {assert} from 'chrome://resources/js/assert_ts.js'; `) need to exist
163inside the TypeScript root directory, at the expected location. For example,
164if foo.ts in the top level directory contains the import statement
165`import {Baz} from './bar/baz.js';`, then the folder structure when ts_library
166is invoked should look like this:
167 root_dir/
168 foo.ts
169 bar/
170 baz.ts
171```
172#### **Arguments**
173```
174root_dir: This is the root directory where all TypeScript files to compile
175 must reside (see note above).
176out_dir: The directory where ts_library will write the compiled JavaScript
177 files.
178composite: Set this to "true" if the output needs to be used as a library by
179 a different ts_library target (this frequently occurs when tests are
180 compiled as a ts_library).
181in_files: The input file paths to be compiled, with respect to the |in_folder|.
182definitions: TypeScript definitions files used by the input TypeScript files,
183 for example chrome.send or extension API definitions.
184tsconfig_base: Specifies the tsconfig_base.json file to use. Necessary only
185 if specifying configuration options for TS compiler that differ
186 from the defaults in tools/typescript/tsconfig_base.json
187deps: Specifies all other ts_library targets generating libraries that this
188 target's library needs, for example the shared resources library at
dpapadf0e349692023-01-31 01:14:21189 //ui/webui/resources/js:build_ts.
Rebekah Potterd38c2c42022-06-28 21:21:22190extra_deps: Used to specify build targets generating the input files for TS
191 compiler.
192path_mappings: Additional non-default path mappings for absolute imports. The
Rebekah Potter84a6b56d2023-02-01 01:17:26193 absolute 'chrome://resources/' paths are already mapped for any
194 resources in libraries that are listed in |deps|; for example
195 adding "//ui/webui/resources/cr_elements:build_ts" in deps will
196 automatically add the mapping for imports from that library
197 (e.g. 'chrome://resources/cr_elements/cr_button/cr_button.js').
198 Important: Don't add path_mappings without also adding the
199 ts_library() target(s) responsible for the files being mapped to
200 deps! path_mappings without corresponding deps can result in
201 flaky build errors.
Rebekah Potterd38c2c42022-06-28 21:21:22202manifest_excludes: List of input files to exclude from the output
Luciano Pachecoc3c35042022-07-01 09:33:39203 the manifest file.
Tibor Goldschwendt1b57bd52023-05-03 21:49:55204enable_source_maps: Defaults to the value of the enable_webui_inline_sourcemaps
205 GN flag. Setting it to "true" turns on TS compiler's
206 'inlineSourceMap' and 'inlineSources' flags. Non-inlined
207 source maps are currently not supported.
Rebekah Potterd38c2c42022-06-28 21:21:22208```
209
210#### **Example**
211
212```
213import("//tools/typescript/ts_library.gni")
214
215# Compiles and outputs my_webui.js, my_web_component.js and
216# my_web_component.html.js, in the "$target_gen_dir/tsc" folder.
217ts_library("build_ts") {
218 root_dir = "$target_gen_dir/preprocessed"
219 out_dir = "$target_gen_dir/tsc"
220 in_files = [
221 "my_webui.ts",
222 "my_style_module.css.ts",
223 "my_web_component.html.ts",
224 "my_web_component.ts",
225 ]
226 # List other ts_library targets for libraries the UI needs here
227 deps = [
228 "//third_party/polymer/v3_0:library",
dpapadf0e349692023-01-31 01:14:21229 "//ui/webui/resources/js:build_ts",
Rebekah Potterd38c2c42022-06-28 21:21:22230 ]
231 definitions = [
232 "//tools/typescript/definitions/chrome_send.d.ts",
233 ]
234 extra_deps = [
235 ":copy_file",
236 ":preprocess_src",
237 ":preprocess_generated",
238 ]
239}
240```
241
Cole Horvitz253c079a2023-04-28 00:34:42242### **bundle_js**
Mike Frysingerfa3a0c92023-06-08 02:33:01243
Rebekah Potterd38c2c42022-06-28 21:21:22244This rule is used to bundle larger user-facing WebUIs for improved performance.
245It is generally not needed for debug UIs or UIs that have very few files to
246import.
Rebekah Potterd38c2c42022-06-28 21:21:22247
248#### **Arguments**
249```
250host: The WebUI host. If specified without a scheme, the assumed root location
251 will be "chrome://<host>". If specified with a scheme (e.g.
252 "chrome-extension://aaaaaaaaa") the full <host> argument will be the root.
253input: The location of the input files to be bundled.
254js_module_in_files: The names of the root files to bundle. These files should
255 import all other dependencies (directly or indirectly).
256 These should be specified with respect to |input|.
Rebekah Potter018fa872023-01-28 00:54:28257 1 or 2 input files are supported. Each will result in one
258 output file named like the input with a new "rollup" suffix,
259 e.g. "foo/main.js" --> "foo/main.rollup.js". If 2 inputs are
260 specified, a third shared bundle file will also be created.
261 The shared bundle will be named "shared.rollup.js" and
262 located at the same relative path as the inputs.
Rebekah Potterd38c2c42022-06-28 21:21:22263out_manifest: File location to write the manifest of all output files created
Cole Horvitz253c079a2023-04-28 00:34:42264 by bundle_js(). Useful for generating grds.
Rebekah Potterd38c2c42022-06-28 21:21:22265deps: Targets generating any files being bundled. Note that this should include
266 targets generating shared resources that are expected to be bundled in
dpapadf0e349692023-01-31 01:14:21267 the UI, e.g. //ui/webui/resources/js:build_ts.
Rebekah Potterd38c2c42022-06-28 21:21:22268excludes: Paths of files that are not bundled. Often used for large mojo files
Rebekah Potter952290e2022-11-18 09:07:28269 that would otherwise be in many bundles, and for cr.js which relies
Rebekah Potterd38c2c42022-06-28 21:21:22270 on global variables.
Rebekah Potter06eee9c2023-02-22 03:29:39271external_paths: Mappings between absolute URLs and paths where files imported
272 from these URLs can be found. Used for files that are not in
273 |input|. For example, the following external_path is
274 automatically added for all targets:
275 "chrome://resources/|$resources_path" where resources_path is
276 the path to where ts_library()s within ui/webui/resources place
277 their output (e.g. gen/ui/webui/resources/tsc).
278 Note: all absolute URLs must either be listed in |excludes| or
279 be mapped in |external_paths|, otherwise a build time error is
280 raised.
Cole Horvitz8ca01462023-05-22 18:34:11281out_folder: The location where bundled files will be placed in. Defaults to
282 |target_gen_dir|.
Rebekah Potterd38c2c42022-06-28 21:21:22283```
284
285#### **Example**
286```
Cole Horvitz253c079a2023-04-28 00:34:42287import("//ui/webui/resources/tools/bundle_js.gni")
Rebekah Potterd38c2c42022-06-28 21:21:22288import ("//chrome/common/features.gni")
289
290# optimize_webui should generally only be called when the optimize_webui
291# feature flag is enabled.
292if (optimize_webui) {
Cole Horvitz253c079a2023-04-28 00:34:42293 bundle_js("build") {
Rebekah Potterd38c2c42022-06-28 21:21:22294 host = "mywebui"
Rebekah Potter018fa872023-01-28 00:54:28295 js_module_in_files = [ "my_webui.js" ] # will output my_webui.rollup.js
Rebekah Potterd38c2c42022-06-28 21:21:22296 input = rebase_path("$target_gen_dir/tsc", root_build_dir)
Rebekah Potterd38c2c42022-06-28 21:21:22297 out_manifest = "$target_gen_dir/build_manifest.json"
298 # Assumes the JS files were generated by a ts_library target called
299 # build_ts.
300 deps = [
301 ":build_ts",
dpapadf0e349692023-01-31 01:14:21302 "//ui/webui/resources/js:build_ts",
Rebekah Potterd38c2c42022-06-28 21:21:22303 ]
304 excludes = [
Rebekah Potter952290e2022-11-18 09:07:28305 "chrome://resources/js/cr.js",
Rebekah Potterd38c2c42022-06-28 21:21:22306 "chrome://resources/mojo/mojo/public/js/bindings.js",
307 ]
308 }
309}
310```
311
Cole Horvitz251fd882023-04-14 18:26:59312### **minify_js**
Mike Frysingerfa3a0c92023-06-08 02:33:01313
Cole Horvitz251fd882023-04-14 18:26:59314This rule is used to minify Javascript files to reduce build size.
Cole Horvitze368f0d2023-05-24 23:56:13315This can be used alongside bundle_js(), if bundling and minifying is
316desired.
Cole Horvitz251fd882023-04-14 18:26:59317
318#### **Arguments**
319```
320in_folder: The location of the input files to be minified.
321in_files: The list of JS files to minify with respect to the |in_folder|.
322out_folder: The location where minified files will be outputted.
Cole Horvitze368f0d2023-05-24 23:56:13323out_manifest: The location to write the manifest file of all files
324 outputted by minify_js(). Defaults to
325 $target_gen_dir/${target_name}_manifest.json.
Cole Horvitz251fd882023-04-14 18:26:59326deps: Targets generating any files being minified.
327```
328
329#### **Example**
330```
331import("//ui/webui/resources/tools/minify_js.gni")
332import ("//chrome/common/features.gni")
333
334# minify_js should generally only be called when the optimize_webui
335# GN flag is enabled.
336if (optimize_webui) {
337 minify_js("build") {
338 in_files = [ "my_webui.js" ]
339 in_folder = "$target_gen_dir/tsc"
Cole Horvitz546af9f2023-04-18 23:24:54340 out_folder = "$target_gen_dir/minified"
Cole Horvitz251fd882023-04-14 18:26:59341 # Assumes the JS files were generated by a ts_library target called
342 # build_ts.
343 deps = [ ":build_ts" ]
344 }
345}
346```
347
Rebekah Potterd38c2c42022-06-28 21:21:22348### **generate_grd**
Mike Frysingerfa3a0c92023-06-08 02:33:01349
Rebekah Potterd38c2c42022-06-28 21:21:22350This rule is used to list the WebUI resources that need to be served at runtime
351in a grd file.
Rebekah Potterd38c2c42022-06-28 21:21:22352
353#### **Arguments**
354```
355input_files: Input file paths to list in the grd file.
356input_files_base_dir: Directory in which |input_files| can be found.
357deps: Lists any targets responsible for generating |input_files|,
358 |grdp files|, or |manifest_files|.
359manifest_files: List of manifest files listing additional files that should be
360 added to the grd.
361grdp_files: List of .grdp files that should be included in the grd. Generally
dpapad973b36982023-03-15 23:20:55362 such files are also created with generate_grd(). This option can
363 only be used if a .grd file is produced, since .grdp files can't be
364 nested.
Rebekah Potterd38c2c42022-06-28 21:21:22365grd_prefix: The prefix to use for the grd resource IDs. Resources will be named
366 with the following pattern: IDR_GRD_PREFIX_INPUT_FILE_PATH
dpapad973b36982023-03-15 23:20:55367out_grd: The output grd file to write. Must end with either '.grd' or '.grdp'.
Mike Frysinger5c08d792023-06-16 19:20:58368resource_path_prefix: Optional prefix to add to every path in input_files (after
369 the path has been processed by |resource_path_rewrites|).
370 Must not end in a `/` as it will automatically be added
371 when joining to each path.
Rebekah Potterd38c2c42022-06-28 21:21:22372resource_path_rewrites: Paths to rewrite. In general, the path in input_files,
373 or the path listed in a manifest, will be used as the
374 resource path, i.e. "foo/bar.js" will have that path
375 relative to the root "chrome://mywebui/" at runtime.
376 By specifying
377 resource_path_rewrites = [ "foo/bar.js|foo_bar.js" ],
378 the path will instead be "foo_bar.js". Usually only
379 needed in somewhat complicated cases.
380```
381
382#### **Example**
383```
384import("//ui/webui/resources/tools/generate_grd.gni")
385
386generate_grd("build_grd") {
387 input_files = [ "my_webui.html" ]
388 input_files_base_dir = rebase_path(".", "//")
389 deps = [ ":build_ts" ]
Cole Horvitz8ecd16f2023-06-01 00:47:22390 manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ])
Luciano Pachecoc3c35042022-07-01 09:33:39391 # Or, configure statically the manifest file name:
392 # manifest_files = [ "$target_gen_dir/build_ts.manifest" ]
Rebekah Potterd38c2c42022-06-28 21:21:22393 grd_prefix = "my_webui"
394 out_grd = "$target_gen_dir/${grd_prefix}_resources.grd"
395}
396```
397
398### **grit**
Mike Frysingerfa3a0c92023-06-08 02:33:01399
Rebekah Potterd38c2c42022-06-28 21:21:22400This rule is used to create a pak file from the grd file that contains all the
401listed resources, so that they can be included in the binary and served at
402runtime. Without creating a grit rule (and hooking up the target to the build),
403the WebUI will not load since the resources will not exist. This rule also
404generates C++ header files that are used to add the files to a specific
405WebUIController.
Rebekah Potterd38c2c42022-06-28 21:21:22406
407#### **Arguments**
408```
409defines: The defines that grit should use when making the pak file. Generally
410 should include chrome_grit_defines.
411enable_input_discovery_for_gn_analyze: If using generated grd files, this needs
412 to be set to false.
413source: The source grd file.
414outputs: List of files to output. Should always include the name of the pak
415 file, and should generally also list the C++ header file and C++ header
416 and .cc files for the resources map.
417output_dir: Location to put the pak file. Often "$root_gen_dir/chrome".
418deps: Names of any targets generating the grd file, and names of any targets
419 generating the files to be packed, if they are not already dependencies
420 of the target generating the grd file (as is often, but not always, the
421 case).
422```
423
424#### **Example**
425```
426import("//tools/grit/grit_rule.gni")
427
428grit("resources") {
429 defines = chrome_grit_defines
430
431 enable_input_discovery_for_gn_analyze = false
432 source = "$target_gen_dir/resources.grd"
433 deps = [ ":build_grd" ]
434
435 outputs = [
436 "grit/my_webui_resources.h",
437 "grit/my_webui_resources_map.cc",
438 "grit/my_webui_resources_map.h",
439 "my_webui_resources.pak",
440 ]
441 output_dir = "$root_gen_dir/chrome"
442}
443```
444
dpapad689e8272022-07-12 23:59:41445### **build_webui**
446
447<!-- TODO(crbug.com/1340376): Elevate build_webui() to the top of this document
448 after it has been deployed to a few places. -->
449
450See the [go/build-webui-pipeline](http://go/build-webui-pipeline) design doc for
451more info (internal only).
452
dpapadc2db8ac2022-12-14 01:36:40453The flow diagram below shows a high level view of how a typical modern
454user-facing WebUI is being built.
dpapad689e8272022-07-12 23:59:41455
456![WebUI build pipeline flow diagram](images/webui_build_pipeline.png)
457
dpapad689e8272022-07-12 23:59:41458This umbrella rule captures the most common WebUI build pipeline configuration
459and aims to hide the complexity of having to define all previously described
460rules directly. Using build_webui() is the recommended approach for most modern
461user-facing WebUIs that use WebComponents+TypeScript+Mojo.
462
463The parameters passed to build_webui() are forwarded as needed to the other GN
464rules described earlier.
465
466Under the cover, build_webui() defines the following targets
467
Mike Frysingerfa3a0c92023-06-08 02:33:01468* preprocess_if_expr("preprocess_ts_files")
469* preprocess_if_expr("preprocess_html_css_files")
470* create_js_source_maps("create_source_maps")
471* html_to_wrapper("html_wrapper_files")
472* css_to_wrapper("css_wrapper_files")
473* copy("copy_mojo")
474* ts_library("build_ts")
475* merge_js_source_maps("merge_source_maps")
476* bundle_js("build_bundle")
477* minify_js("build_min_js")
478* generate_grd("build_grd")
479* generate_grd("build_grdp")
480* grit("resources")
dpapad689e8272022-07-12 23:59:41481
482Some targets are only conditionally defined based on build_webui() input
483parameters.
484
dpapadf7a2c7a2023-03-16 20:26:28485Only ":build_ts", ":resources" and ":build_grdp" targets are public and can be
486referred to from other parts of the build.
dpapad689e8272022-07-12 23:59:41487
488#### **Arguments**
489```
490
491List of files params:
dpapad39cac962023-01-11 22:52:52492static_files: Optional parameter. List of
dpapad57fa7892022-08-03 03:45:15493 1) non Web Component HTML/CSS files (don't confuse with
494 |css_files| below). These are passed to preprocess_if_expr()
495 2) JPG/PNG/SVG files. These are included in the build verbatim
496 without any preprocessing.
dpapad689e8272022-07-12 23:59:41497
498web_component_files: List of TS files that hold Web Component definitions with
499 equivalent HTML template files. These can be either native
500 or Polymer Web Components. Optional parameter.
501
502non_web_component_files: List of TS files that are not Web Components, or
503 Web Component files that don't have a corresponding
504 HTML template (rare case). Optional parameter.
505
506icons_html_files: List of HTML files that hold Polymer iron-iconset-svg
507 instances. Optional parameter.
508
509css_files: List of CSS files that hold Polymer style modules, or CSS variable
510 definitions. These are passed css_to_wrapper(). Optional parameter.
511
512mojo_files: List of Mojo JS generated files. These will be copied to a temporary
513 location so that they can be passed to ts_library() along with other
514 files. Optional parameter.
515
516mojo_files_deps: List of Mojo targets that generate |mojo_files|. Must be
517 defined if |mojo_files| is defined.
518
dpapadbbee30ae2023-05-20 00:37:40519mojo_base_path: Specifies the directory under which Mojo files will be served at
520 runtime. Optional parameter. Defaults to the top level folder
521 '.', which results in Mojo files being served from
522 'chrome://<webui_name>/foo.mojom-webui.js'.
523 Example: Passing 'mojom-webui' would result in Mojo files being
524 served from
525 'chrome://<webui_name>/mojom-webui/foo.mojom-webui.js'.
526
dpapad689e8272022-07-12 23:59:41527TypeScript (ts_library()) related params:
528ts_composite: See |composite| in ts_library(). Defaults to false, optional.
dpapadf7a2c7a2023-03-16 20:26:28529ts_out_dir: See |out_dir| in ts_library(). Optional parameter, defaults
530 '$target_gen_dir/tsc'
dpapad689e8272022-07-12 23:59:41531ts_definitions: See |definitions| in ts_library(). Optional parameter.
532ts_deps: See |deps| in ts_library(). Optional parameter.
Yuheng Huang2693a49e2022-07-26 15:20:03533ts_extra_deps: See |extra_deps| in ts_library(). Optional parameter.
dpapad689e8272022-07-12 23:59:41534ts_path_mappings: See |path_mappings| in ts_library(). Optional parameter.
Rebekah Potter03240f52023-03-01 22:50:30535ts_tsconfig_base: The tsconfig file to use for ts_library(). Optional, defaults
536 to "//tools/typescript/tsconfig_base_polymer.json" for Polymer
537 UIs (i.e. UIs that specify |web_component_files| and/or
538 |icons_html_files| and do not set |html_to_wrapper_template|
539 to "native"). Defaults to
540 "//tools/typescript/tsconfig_base.json" for non-Polymer UIs.
dpapad689e8272022-07-12 23:59:41541
dpapad391a3282022-07-19 17:38:32542HTML/CSS/JS optimization related params:
Cole Horvitz546af9f2023-04-18 23:24:54543optimize: Specifies whether any optimization steps will be used. Defaults to the
544 value of the optimize_webui GN flag.
545 When true, html_to_wrapper() and css_to_wrapper() will be invoked with
546 the |minify| flag on, to minify HTML/CSS code.
Cole Horvitz39b3c96d2023-05-02 23:32:22547 When true, minify_js() will be invoked to minify JS code (using Terser).
548 If |optimize_webui_in_files| is provided then bundle_js() will also be
549 invoked to bundle JS code (using Rollup).
dpapadf7a2c7a2023-03-16 20:26:28550 |optimize_webui_host| must be specified if |optimize_webui_in_files|
551 is provided.
Cole Horvitz39b3c96d2023-05-02 23:32:22552optimize_webui_excludes: See |excludes| in bundle_js(). Optional.
dpapadf79e4c0e2023-04-13 22:35:16553optimize_webui_external_paths: See |external_paths| in optimize_webui().
554 Optional.
Cole Horvitz39b3c96d2023-05-02 23:32:22555optimize_webui_host: See |host| in bundle_js().
556optimize_webui_in_files: See |in_files| in bundle_js().
dpapad689e8272022-07-12 23:59:41557
558Other params:
dpapadf7a2c7a2023-03-16 20:26:28559generate_grdp: Whether to generate grdp file instead of a grd file. Defaults to
560 false.
dpapad689e8272022-07-12 23:59:41561grd_prefix: See |grd_prefix| in generate_grd(). Required parameter.
dpapad39cac962023-01-11 22:52:52562grd_resource_path_prefix: See |resource_path_prefix| in generate_grd(). Optional
563 parameter.
dpapad689e8272022-07-12 23:59:41564html_to_wrapper_template: See |template| in html_to_wrapper().
dpapad27f4ec82023-03-24 16:34:38565html_to_wrapper_scheme: See |scheme| in html_to_wrapper().
dpapad689e8272022-07-12 23:59:41566extra_grdp_deps: List of external generate_grd() targets that generate .grdp
567 files. These will be included in the final generated
568 resources.grd file. Optional parameter.
569extra_grdp_files: Output .grdp files of external generate_grd() targets. Must be
570 defined if |extra_grdp_deps| is defined.
dpapad838ef532022-09-09 08:41:53571grit_output_dir: See |output_dir| in grit(). Optional parameter, defaults to
572 "$root_gen_dir/chrome"
dpapadc854fcf92023-02-17 23:03:32573enable_source_maps: Defaults to "false". Incompatible with |optimize=true|.
574 Setting it to "true" turns on source map generation for a
575 few underlying targets. See ts_library()'s
576 |enable_source_maps| for more details.
dpapad689e8272022-07-12 23:59:41577```
578
579#### **Example**
580```
Antonio Gomes282f5c4f92023-02-15 13:36:21581import("//ui/webui/resources/tools/build_webui.gni")
dpapad689e8272022-07-12 23:59:41582
583build_webui("build") {
584 grd_prefix = "dummy-webui"
585
586 static_files = [
587 "index.html",
588 "index.css",
589 ]
590
591 # Files holding a CustomElement element definition AND have an equivalent
592 # .html template file.
593 web_component_files = [
594 "app.ts",
595 "bar_view.ts",
596 "foo_view.ts",
597 ]
598
599 # Files not holding a CustomElement element definition, or the CustomElement
600 # does not have a corresponding HTML template.
601 non_web_component_files = [
602 "app_proxy.ts",
603 "bar_proxy.ts",
604 "foo_proxy.ts",
605 ]
606
607 # Files that are passed as input to css_to_wrapper().
608 css_files = [
609 "shared_style.css",
610 "shared_vars.css",
611 ]
612
613 ts_definitions = [
614 "//tools/typescript/definitions/chrome_send.d.ts",
615 "//tools/typescript/definitions/metrics_private.d.ts",
616 ]
617
618 ts_deps = [
619 "//third_party/polymer/v3_0:library",
dpapadf0e349692023-01-31 01:14:21620 "//ui/webui/resources/cr_elements:build_ts",
621 "//ui/webui/resources/js:build_ts",
dpapad689e8272022-07-12 23:59:41622 ]
623}
624
625```
626
dpapadc2db8ac2022-12-14 01:36:40627### **build_webui_tests**
628
629The flow diagram below shows a high level view of how a typical modern user-facing
630WebUI's tests are built.
631
632![WebUI tests build pipeline flow diagram](images/webui_build_pipeline_tests.png)
633
dpapadc2db8ac2022-12-14 01:36:40634This umbrella rule captures the most common WebUI test build pipeline
635configuration and aims to hide the complexity of having to define multiple other
636targets directly. Using build_webui_tests() is the recommended approach for most
637modern user-facing WebUIs tests that reside under
638chrome/test/data/webui/<webui_name>/ and are served from
639chrome://webui-test/<webui_name>/
640
641Under the cover, build_webui_tests() defines the following targets
642
Mike Frysingerfa3a0c92023-06-08 02:33:01643* preprocess_if_expr("preprocess")
644* ts_library("build_ts")
645* generate_grd("build_grdp")
dpapadc2db8ac2022-12-14 01:36:40646
647The parameters passed to build_webui_tests() are forwarded as needed to
648the targets above. Only the ":build_grdp" target is public and can be referred
649to from other parts of the build.
650
dpapadc2db8ac2022-12-14 01:36:40651#### **Arguments**
652```
653
654List of files params:
655files: Required parameter. List of all test related files.
656
657TypeScript (ts_library()) related params:
dpapad1de7e972023-03-01 02:49:48658ts_tsconfig_base: See |tsconfig_base| in ts_library(). Optional parameter. If
659 not provided the default configuration at
660 '//chrome/test/data/webui/tsconfig_base.json' is used.
dpapad8ab521b2022-12-15 06:51:06661ts_definitions: See |definitions| in ts_library(). Optional parameter.
dpapadc2db8ac2022-12-14 01:36:40662ts_deps: See |deps| in ts_library(). Required parameter.
dpapad8ab521b2022-12-15 06:51:06663ts_path_mappings: See |path_mappings| in ts_library(). Optional parameter.
dpapadc2db8ac2022-12-14 01:36:40664
665Other params:
666resource_path_prefix: See |resource_path_prefix| in generate_grd(). Required
667 parameter.
668```
669
670#### **Example**
671```
672import("//chrome/test/data/webui/settings/tools/build_webui_tests.gni")
673
674build_webui_tests("build") {
675 resource_path_prefix = "settings"
676
677 files = [
678 "checkbox_tests.ts",
679 "collapse_radio_button_tests.ts",
680 "controlled_button_tests.ts",
681 "controlled_radio_button_tests.ts",
682 "dropdown_menu_tests.ts",
683 ]
684
685 ts_path_mappings =
686 [ "chrome://settings/*|" +
687 rebase_path("$root_gen_dir/chrome/browser/resources/settings/tsc/*",
688 target_gen_dir) ]
689
690 ts_definitions = [
691 "//tools/typescript/definitions/chrome_send.d.ts",
692 "//tools/typescript/definitions/settings_private.d.ts",
693 ]
694
695 ts_deps = [
696 "//chrome/browser/resources/settings:build_ts",
697 ]
698}
699
700```
701
Rebekah Potterd38c2c42022-06-28 21:21:22702## Example build configurations
703
704### **Simple UI with no web components**
705
706```
707This UI has only a single checked in TypeScript file, my_debug_page.ts, and a
708checked in HTML file my_debug_page_index.html that imports the compiled JS
709file using a script tag with `src="my_debug_page.js"` Neither of these files
710use any `<if expr>`.
711```
712
713#### **BUILD.gn file**
714```
715import("//chrome/common/features.gni")
716import("//tools/grit/grit_rule.gni")
717import("//tools/typescript/ts_library.gni")
718import("//ui/webui/resources/tools/generate_grd.gni")
719
720# This UI has only a single TypeScript input file that does not define a Web
721# Component. TS compiler will write out "$target_gen_dir/tsc/my_debug_page.js".
722ts_library("build_ts") {
723 root_dir = "."
724 out_dir = "$target_gen_dir/tsc"
725 in_files = [ "my_debug_page.ts" ]
dpapadf0e349692023-01-31 01:14:21726 deps = [ "//ui/webui/resources/js:build_ts" ]
Rebekah Potterd38c2c42022-06-28 21:21:22727}
728
729# Both the HTML file and the JS file created by ts_library need to be listed in
Luciano Pachecoc3c35042022-07-01 09:33:39730# the grd. The JS file is already listed in the build_ts.manifest generated by
Rebekah Potterd38c2c42022-06-28 21:21:22731# ts_library(). Pass the HTML file via |input_files|.
732generate_grd("build_grd") {
733 deps = [ ":build_ts" ]
734 grd_prefix= "my_debug_page"
735 out_grd = "$target_gen_dir/resources.grd"
736 input_files = [ "my_debug_page_index.html" ]
737 input_files_base_dir = rebase_path(".", "//")
Cole Horvitz8ecd16f2023-06-01 00:47:22738 manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ])
Rebekah Potterd38c2c42022-06-28 21:21:22739}
740
741# Create the pak, header, and resource map files.
742grit("resources") {
743 defines = chrome_grit_defines
744
745 # This arguments is needed since the grd is generated at build time.
746 enable_input_discovery_for_gn_analyze = false
747 source = "$target_gen_dir/resources.grd"
748 deps = [ ":build_grd" ]
749 outputs = [
750 "grit/my_debug_page_resources.h",
751 "grit/my_debug_page_resources_map.cc",
752 "grit/my_debug_page_resources_map.h",
753 "my_debug_page_resources.pak",
754 ]
755 output_dir = "$root_gen_dir/chrome"
756}
757```
758
759### **Non-Polymer UI with Web Components**
760```
761This UI has a top level TypeScript file that imports a single Web Component,
762my_debug_app.ts. It has a corresponding HTML file for the app and a HTML file
763for the top level index. None of these files use any `<if expr>`.
764```
765
766#### **BUILD.gn file**
767```
768import("//chrome/common/features.gni")
769import("//tools/grit/grit_rule.gni")
770import("//tools/typescript/ts_library.gni")
771import("//ui/webui/resources/tools/generate_grd.gni")
772import("//tools/polymer/html_to_wrapper.gni")
773
774# Generate the wrapper file. This UI isn't using Polymer, so it needs to set
775# the |template| argument.
776html_to_wrapper("html_wrapper_files") {
777 in_files = [ "my_debug_app.html" ]
778 template = "native"
779}
780
781# Copy the checked-in files to the same directory. This is needed so that the
782# TypeScript compiler can find all the files in the expected location.
783copy("copy_files") {
784 sources = [
785 "my_debug_app.ts",
786 "my_debug_page.ts",
787 ]
788 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
789}
790
791# This UI has 3 TypeScript input files. One is the generated
792# my_debug_app.html.ts, two are checked in: my_debug_page.ts and
793# my_debug_app.ts. TS compiler will write out:
794# "$target_gen_dir/tsc/my_debug_app.html.js",
795# "$target_gen_dir/tsc/my_debug_app.js", and
796# "$target_gen_dir/tsc/my_debug_page.js".
797ts_library("build_ts") {
798 root_dir = target_gen_dir
799 out_dir = "$target_gen_dir/tsc"
800 in_files = [
801 "my_debug_app.ts",
802 "my_debug_app.html.ts",
803 "my_debug_page.ts",
804 ]
dpapadf0e349692023-01-31 01:14:21805 deps = [ "//ui/webui/resources/js:build_ts" ]
Rebekah Potterd38c2c42022-06-28 21:21:22806 extra_deps = [
807 ":copy_files",
808 ":html_wrapper_files",
809 ]
810}
811
812# The HTML file and the JS files created by ts_library need to be listed in
Luciano Pachecoc3c35042022-07-01 09:33:39813# the grd. The JS files are already listed in the build_ts.manifest generated by
Rebekah Potterd38c2c42022-06-28 21:21:22814# ts_library(). Pass the HTML file via |input_files|.
815generate_grd("build_grd") {
816 deps = [ ":build_ts" ]
817 grd_prefix= "my_debug_page"
818 out_grd = "$target_gen_dir/resources.grd"
819 input_files = [ "my_debug_page_index.html" ]
820 input_files_base_dir = rebase_path(".", "//")
Cole Horvitz8ecd16f2023-06-01 00:47:22821 manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ])
Rebekah Potterd38c2c42022-06-28 21:21:22822}
823
824# Create the pak, header, and resource map files.
825grit("resources") {
826 defines = chrome_grit_defines
827
828 # This arguments is needed since the grd is generated at build time.
829 enable_input_discovery_for_gn_analyze = false
830 source = "$target_gen_dir/resources.grd"
831 deps = [ ":build_grd" ]
832 outputs = [
833 "grit/my_debug_page_resources.h",
834 "grit/my_debug_page_resources_map.cc",
835 "grit/my_debug_page_resources_map.h",
836 "my_debug_page_resources.pak",
837 ]
838 output_dir = "$root_gen_dir/chrome"
839}
840```