blob: 4067095e3c36f2c091360181e27747aa0637d6c9 [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.
Luc Nguyenfa9eb63b2024-02-20 23:19:07114defines: Optional parameter. Specifies additional variables that can be used in
115 conditional expressions.
Rebekah Potterd38c2c42022-06-28 21:21:22116```
117#### **Example:**
118```
119import("//tools/grit/preprocess_if_expr.gni")
120
121# Preprocesses "my_web_component.html.ts" and my_style_module.css.ts in
122# |target_gen_dir|, into "$target_gen_dir/preprocessed".
123preprocess_if_expr("preprocess_generated") {
124 # Depend on the targets that generates these files.
125 deps = [
126 ":css_wrapper_files",
127 ":html_wrapper_files",
128 ]
129 in_folder = target_gen_dir
130 in_files = [
131 "my_style_module.css.ts",
132 "my_web_component.html.ts",
133 ]
134 out_folder = "$target_gen_dir/$preprocess_folder"
Luc Nguyenfa9eb63b2024-02-20 23:19:07135 defines = [ "foo_enabled=false" ]
Rebekah Potterd38c2c42022-06-28 21:21:22136}
137
138# Preprocess "my_web_component.ts" and "my_webui.ts" in the src dir into
139# "$target_gen_dir/preprocessed".
140preprocess_if_expr("preprocess_src") {
141 in_folder = "."
142 in_files = [
143 "my_web_component.ts",
144 "my_webui.ts",
145 ]
146 out_folder = "$target_gen_dir/$preprocess_folder"
Luc Nguyenfa9eb63b2024-02-20 23:19:07147 defines = [ "foo_enabled=true" ]
Rebekah Potterd38c2c42022-06-28 21:21:22148}
149```
150
151### **ts_library**
Mike Frysingerfa3a0c92023-06-08 02:33:01152
Rebekah Potterd38c2c42022-06-28 21:21:22153This rule is used to compile TypeScript code to JavaScript. It outputs JS files
154corresponding to each TS file passed as an input into the designated output
155directory, and generates a manifest listing all the files it has output named
Luciano Pachecoc3c35042022-07-01 09:33:39156$target_name.manifest in the target generated directory.
Rebekah Potterd38c2c42022-06-28 21:21:22157
158#### **Input File notes**
159```
160All input files must be valid TypeScript. This means, among other things, that
161files shouldn't contain any `<if expr>`, i.e. they should already have been
162preprocessed, if necessary. It also means that e.g. HTML or image files
163shouldn't be passed to ts_library.
164
165All files that aren't imported with an absolute path (e.g.
dpapad01dc29652023-10-12 22:11:22166`import {assert} from 'chrome://resources/js/assert.js'; `) need to exist
Rebekah Potterd38c2c42022-06-28 21:21:22167inside the TypeScript root directory, at the expected location. For example,
168if foo.ts in the top level directory contains the import statement
169`import {Baz} from './bar/baz.js';`, then the folder structure when ts_library
170is invoked should look like this:
171 root_dir/
172 foo.ts
173 bar/
174 baz.ts
175```
176#### **Arguments**
177```
178root_dir: This is the root directory where all TypeScript files to compile
179 must reside (see note above).
180out_dir: The directory where ts_library will write the compiled JavaScript
181 files.
182composite: Set this to "true" if the output needs to be used as a library by
183 a different ts_library target (this frequently occurs when tests are
184 compiled as a ts_library).
185in_files: The input file paths to be compiled, with respect to the |in_folder|.
186definitions: TypeScript definitions files used by the input TypeScript files,
187 for example chrome.send or extension API definitions.
188tsconfig_base: Specifies the tsconfig_base.json file to use. Necessary only
189 if specifying configuration options for TS compiler that differ
190 from the defaults in tools/typescript/tsconfig_base.json
191deps: Specifies all other ts_library targets generating libraries that this
192 target's library needs, for example the shared resources library at
dpapadf0e349692023-01-31 01:14:21193 //ui/webui/resources/js:build_ts.
Rebekah Potterd38c2c42022-06-28 21:21:22194extra_deps: Used to specify build targets generating the input files for TS
195 compiler.
196path_mappings: Additional non-default path mappings for absolute imports. The
Rebekah Potter84a6b56d2023-02-01 01:17:26197 absolute 'chrome://resources/' paths are already mapped for any
198 resources in libraries that are listed in |deps|; for example
199 adding "//ui/webui/resources/cr_elements:build_ts" in deps will
200 automatically add the mapping for imports from that library
201 (e.g. 'chrome://resources/cr_elements/cr_button/cr_button.js').
rbpotter1df557392024-02-28 00:49:55202 Important: Adding path_mappings *does not* add the files mapped
203 in |inputs| or the targets generating files to |deps|. To prevent
204 flaky build errors, *always* do one of the following when adding
205 a path_mapping:
206 - Add the ts_library() target responsible for compiling .ts files
207 into the mapped generated directory to |deps|.
208 - Add the ts_definitions(), copy(), preprocess_if_expr(), or
209 other target responsible for generating definitions files in
210 the mapped generated directory to |extra_deps|
211 - Add all source .d.ts files your target uses from the mapped
212 source directory to |definitions|.
rbpottere31fb8b2024-03-12 15:42:00213path_mappings_file: A .json file containing path mappings in the form
214 {url: [ dir1, dir2, ... ] } where dir* are relative to the
215 |target_gen_dir|.
Rebekah Potterd38c2c42022-06-28 21:21:22216manifest_excludes: List of input files to exclude from the output
Luciano Pachecoc3c35042022-07-01 09:33:39217 the manifest file.
Tibor Goldschwendt1b57bd52023-05-03 21:49:55218enable_source_maps: Defaults to the value of the enable_webui_inline_sourcemaps
219 GN flag. Setting it to "true" turns on TS compiler's
220 'inlineSourceMap' and 'inlineSources' flags. Non-inlined
221 source maps are currently not supported.
Rebekah Potterd38c2c42022-06-28 21:21:22222```
223
224#### **Example**
225
226```
227import("//tools/typescript/ts_library.gni")
228
229# Compiles and outputs my_webui.js, my_web_component.js and
230# my_web_component.html.js, in the "$target_gen_dir/tsc" folder.
231ts_library("build_ts") {
232 root_dir = "$target_gen_dir/preprocessed"
233 out_dir = "$target_gen_dir/tsc"
234 in_files = [
235 "my_webui.ts",
236 "my_style_module.css.ts",
237 "my_web_component.html.ts",
238 "my_web_component.ts",
239 ]
240 # List other ts_library targets for libraries the UI needs here
241 deps = [
242 "//third_party/polymer/v3_0:library",
dpapadf0e349692023-01-31 01:14:21243 "//ui/webui/resources/js:build_ts",
Rebekah Potterd38c2c42022-06-28 21:21:22244 ]
245 definitions = [
246 "//tools/typescript/definitions/chrome_send.d.ts",
247 ]
248 extra_deps = [
249 ":copy_file",
250 ":preprocess_src",
251 ":preprocess_generated",
252 ]
253}
254```
255
rbpotter890a38a2024-03-12 01:21:41256### **webui_path_mappings**
257
258This rule generates a path mappings .json file named
259'path_mappings_<target_name>.json' in |target_gen_dir| from a list of target
rbpottere31fb8b2024-03-12 15:42:00260dependencies. Its output can be passed to ts_library's |path_mappings_file|
261parameter.
rbpotter890a38a2024-03-12 01:21:41262
263Note that the rule only generates mappings for dependencies that are mapped
264in path_mappings.py (e.g. //ui/webui/resources/ deps).
265
266#### **Arguments**
267```
268ts_deps: List of ts_library() dependencies to generate path mappings for.
rbpotter38f86ac2024-03-14 22:42:50269webui_context_type: What import scheme(s) should be mapped for the UI. Options:
270 'trusted': Maps chrome:// and scheme-relative imports.
271 Default value.
272 'untrusted': Maps chrome-untrusted:// and scheme-relative
273 imports.
274 'relative': Maps scheme-relative imports only.
275 'trusted_only': Maps chrome:// imports only.
rbpotter890a38a2024-03-12 01:21:41276```
277
rbpottere31fb8b2024-03-12 15:42:00278### **webui_ts_library**
279
280This rule is a thin wrapper around ts_library() that defines 2 targets:
281(1) a webui_path_mappings() ("path_mappings") target to generate a path mappings
282 file from |deps|.
283(2) a ts_library() target ("build_ts") that consumes the generated path
284 mappings file along with all remaining arguments.
285
286#### **Arguments**
287webui_ts_library uses all the same arguments as ts_library, in addition to
288the following:
289```
rbpotter38f86ac2024-03-14 22:42:50290webui_context_type: See |webui_context_type| in webui_path_mappings().
rbpottere31fb8b2024-03-12 15:42:00291```
292
Cole Horvitz253c079a2023-04-28 00:34:42293### **bundle_js**
Mike Frysingerfa3a0c92023-06-08 02:33:01294
Rebekah Potterd38c2c42022-06-28 21:21:22295This rule is used to bundle larger user-facing WebUIs for improved performance.
296It is generally not needed for debug UIs or UIs that have very few files to
297import.
Rebekah Potterd38c2c42022-06-28 21:21:22298
299#### **Arguments**
300```
301host: The WebUI host. If specified without a scheme, the assumed root location
302 will be "chrome://<host>". If specified with a scheme (e.g.
303 "chrome-extension://aaaaaaaaa") the full <host> argument will be the root.
304input: The location of the input files to be bundled.
305js_module_in_files: The names of the root files to bundle. These files should
306 import all other dependencies (directly or indirectly).
307 These should be specified with respect to |input|.
Rebekah Potter018fa872023-01-28 00:54:28308 1 or 2 input files are supported. Each will result in one
309 output file named like the input with a new "rollup" suffix,
310 e.g. "foo/main.js" --> "foo/main.rollup.js". If 2 inputs are
311 specified, a third shared bundle file will also be created.
312 The shared bundle will be named "shared.rollup.js" and
313 located at the same relative path as the inputs.
Rebekah Potterd38c2c42022-06-28 21:21:22314out_manifest: File location to write the manifest of all output files created
Cole Horvitz253c079a2023-04-28 00:34:42315 by bundle_js(). Useful for generating grds.
dpapad4faf2e22023-11-28 23:03:46316rollup_config: Optional parameter. The path to a custom Rollup configuration
317 file. When specified it overrides the default auto-generated
318 configuration file.
319 Note: The custom configuration should not refer to other files
320 (like plugin files), because such files are not automatically
321 declared as `inputs`.
Rebekah Potterd38c2c42022-06-28 21:21:22322deps: Targets generating any files being bundled. Note that this should include
323 targets generating shared resources that are expected to be bundled in
dpapadf0e349692023-01-31 01:14:21324 the UI, e.g. //ui/webui/resources/js:build_ts.
Rebekah Potterd38c2c42022-06-28 21:21:22325excludes: Paths of files that are not bundled. Often used for large mojo files
Rebekah Potter952290e2022-11-18 09:07:28326 that would otherwise be in many bundles, and for cr.js which relies
Rebekah Potterd38c2c42022-06-28 21:21:22327 on global variables.
Rebekah Potter06eee9c2023-02-22 03:29:39328external_paths: Mappings between absolute URLs and paths where files imported
329 from these URLs can be found. Used for files that are not in
330 |input|. For example, the following external_path is
331 automatically added for all targets:
332 "chrome://resources/|$resources_path" where resources_path is
333 the path to where ts_library()s within ui/webui/resources place
334 their output (e.g. gen/ui/webui/resources/tsc).
335 Note: all absolute URLs must either be listed in |excludes| or
336 be mapped in |external_paths|, otherwise a build time error is
337 raised.
Cole Horvitz8ca01462023-05-22 18:34:11338out_folder: The location where bundled files will be placed in. Defaults to
339 |target_gen_dir|.
Rebekah Potterd38c2c42022-06-28 21:21:22340```
341
342#### **Example**
343```
Cole Horvitz253c079a2023-04-28 00:34:42344import("//ui/webui/resources/tools/bundle_js.gni")
Rebekah Potterd38c2c42022-06-28 21:21:22345import ("//chrome/common/features.gni")
346
347# optimize_webui should generally only be called when the optimize_webui
348# feature flag is enabled.
349if (optimize_webui) {
Cole Horvitz253c079a2023-04-28 00:34:42350 bundle_js("build") {
Rebekah Potterd38c2c42022-06-28 21:21:22351 host = "mywebui"
Rebekah Potter018fa872023-01-28 00:54:28352 js_module_in_files = [ "my_webui.js" ] # will output my_webui.rollup.js
Rebekah Potterd38c2c42022-06-28 21:21:22353 input = rebase_path("$target_gen_dir/tsc", root_build_dir)
Rebekah Potterd38c2c42022-06-28 21:21:22354 out_manifest = "$target_gen_dir/build_manifest.json"
355 # Assumes the JS files were generated by a ts_library target called
356 # build_ts.
357 deps = [
358 ":build_ts",
dpapadf0e349692023-01-31 01:14:21359 "//ui/webui/resources/js:build_ts",
Rebekah Potterd38c2c42022-06-28 21:21:22360 ]
361 excludes = [
Rebekah Potter952290e2022-11-18 09:07:28362 "chrome://resources/js/cr.js",
Rebekah Potterd38c2c42022-06-28 21:21:22363 "chrome://resources/mojo/mojo/public/js/bindings.js",
364 ]
365 }
366}
367```
368
Cole Horvitz251fd882023-04-14 18:26:59369### **minify_js**
Mike Frysingerfa3a0c92023-06-08 02:33:01370
Cole Horvitz251fd882023-04-14 18:26:59371This rule is used to minify Javascript files to reduce build size.
Cole Horvitze368f0d2023-05-24 23:56:13372This can be used alongside bundle_js(), if bundling and minifying is
373desired.
Cole Horvitz251fd882023-04-14 18:26:59374
375#### **Arguments**
376```
377in_folder: The location of the input files to be minified.
378in_files: The list of JS files to minify with respect to the |in_folder|.
379out_folder: The location where minified files will be outputted.
Cole Horvitze368f0d2023-05-24 23:56:13380out_manifest: The location to write the manifest file of all files
381 outputted by minify_js(). Defaults to
382 $target_gen_dir/${target_name}_manifest.json.
Cole Horvitz251fd882023-04-14 18:26:59383deps: Targets generating any files being minified.
384```
385
386#### **Example**
387```
388import("//ui/webui/resources/tools/minify_js.gni")
389import ("//chrome/common/features.gni")
390
391# minify_js should generally only be called when the optimize_webui
392# GN flag is enabled.
393if (optimize_webui) {
394 minify_js("build") {
395 in_files = [ "my_webui.js" ]
396 in_folder = "$target_gen_dir/tsc"
Cole Horvitz546af9f2023-04-18 23:24:54397 out_folder = "$target_gen_dir/minified"
Cole Horvitz251fd882023-04-14 18:26:59398 # Assumes the JS files were generated by a ts_library target called
399 # build_ts.
400 deps = [ ":build_ts" ]
401 }
402}
403```
404
Rebekah Potterd38c2c42022-06-28 21:21:22405### **generate_grd**
Mike Frysingerfa3a0c92023-06-08 02:33:01406
Rebekah Potterd38c2c42022-06-28 21:21:22407This rule is used to list the WebUI resources that need to be served at runtime
408in a grd file.
Rebekah Potterd38c2c42022-06-28 21:21:22409
410#### **Arguments**
411```
412input_files: Input file paths to list in the grd file.
413input_files_base_dir: Directory in which |input_files| can be found.
414deps: Lists any targets responsible for generating |input_files|,
415 |grdp files|, or |manifest_files|.
416manifest_files: List of manifest files listing additional files that should be
417 added to the grd.
418grdp_files: List of .grdp files that should be included in the grd. Generally
dpapad973b36982023-03-15 23:20:55419 such files are also created with generate_grd(). This option can
420 only be used if a .grd file is produced, since .grdp files can't be
421 nested.
Rebekah Potterd38c2c42022-06-28 21:21:22422grd_prefix: The prefix to use for the grd resource IDs. Resources will be named
423 with the following pattern: IDR_GRD_PREFIX_INPUT_FILE_PATH
dpapad973b36982023-03-15 23:20:55424out_grd: The output grd file to write. Must end with either '.grd' or '.grdp'.
Mike Frysinger5c08d792023-06-16 19:20:58425resource_path_prefix: Optional prefix to add to every path in input_files (after
426 the path has been processed by |resource_path_rewrites|).
427 Must not end in a `/` as it will automatically be added
428 when joining to each path.
Rebekah Potterd38c2c42022-06-28 21:21:22429resource_path_rewrites: Paths to rewrite. In general, the path in input_files,
430 or the path listed in a manifest, will be used as the
431 resource path, i.e. "foo/bar.js" will have that path
432 relative to the root "chrome://mywebui/" at runtime.
433 By specifying
434 resource_path_rewrites = [ "foo/bar.js|foo_bar.js" ],
435 the path will instead be "foo_bar.js". Usually only
436 needed in somewhat complicated cases.
437```
438
439#### **Example**
440```
441import("//ui/webui/resources/tools/generate_grd.gni")
442
443generate_grd("build_grd") {
444 input_files = [ "my_webui.html" ]
445 input_files_base_dir = rebase_path(".", "//")
446 deps = [ ":build_ts" ]
Cole Horvitz8ecd16f2023-06-01 00:47:22447 manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ])
Luciano Pachecoc3c35042022-07-01 09:33:39448 # Or, configure statically the manifest file name:
449 # manifest_files = [ "$target_gen_dir/build_ts.manifest" ]
Rebekah Potterd38c2c42022-06-28 21:21:22450 grd_prefix = "my_webui"
451 out_grd = "$target_gen_dir/${grd_prefix}_resources.grd"
452}
453```
454
455### **grit**
Mike Frysingerfa3a0c92023-06-08 02:33:01456
Rebekah Potterd38c2c42022-06-28 21:21:22457This rule is used to create a pak file from the grd file that contains all the
458listed resources, so that they can be included in the binary and served at
459runtime. Without creating a grit rule (and hooking up the target to the build),
460the WebUI will not load since the resources will not exist. This rule also
461generates C++ header files that are used to add the files to a specific
462WebUIController.
Rebekah Potterd38c2c42022-06-28 21:21:22463
464#### **Arguments**
465```
466defines: The defines that grit should use when making the pak file. Generally
467 should include chrome_grit_defines.
468enable_input_discovery_for_gn_analyze: If using generated grd files, this needs
469 to be set to false.
470source: The source grd file.
471outputs: List of files to output. Should always include the name of the pak
472 file, and should generally also list the C++ header file and C++ header
473 and .cc files for the resources map.
474output_dir: Location to put the pak file. Often "$root_gen_dir/chrome".
475deps: Names of any targets generating the grd file, and names of any targets
476 generating the files to be packed, if they are not already dependencies
477 of the target generating the grd file (as is often, but not always, the
478 case).
479```
480
481#### **Example**
482```
483import("//tools/grit/grit_rule.gni")
484
485grit("resources") {
486 defines = chrome_grit_defines
487
488 enable_input_discovery_for_gn_analyze = false
489 source = "$target_gen_dir/resources.grd"
490 deps = [ ":build_grd" ]
491
492 outputs = [
493 "grit/my_webui_resources.h",
494 "grit/my_webui_resources_map.cc",
495 "grit/my_webui_resources_map.h",
496 "my_webui_resources.pak",
497 ]
498 output_dir = "$root_gen_dir/chrome"
499}
500```
501
dpapad689e8272022-07-12 23:59:41502### **build_webui**
503
504<!-- TODO(crbug.com/1340376): Elevate build_webui() to the top of this document
505 after it has been deployed to a few places. -->
506
507See the [go/build-webui-pipeline](http://go/build-webui-pipeline) design doc for
508more info (internal only).
509
dpapadc2db8ac2022-12-14 01:36:40510The flow diagram below shows a high level view of how a typical modern
511user-facing WebUI is being built.
dpapad689e8272022-07-12 23:59:41512
513![WebUI build pipeline flow diagram](images/webui_build_pipeline.png)
514
dpapad689e8272022-07-12 23:59:41515This umbrella rule captures the most common WebUI build pipeline configuration
516and aims to hide the complexity of having to define all previously described
517rules directly. Using build_webui() is the recommended approach for most modern
518user-facing WebUIs that use WebComponents+TypeScript+Mojo.
519
520The parameters passed to build_webui() are forwarded as needed to the other GN
521rules described earlier.
522
523Under the cover, build_webui() defines the following targets
524
Mike Frysingerfa3a0c92023-06-08 02:33:01525* preprocess_if_expr("preprocess_ts_files")
526* preprocess_if_expr("preprocess_html_css_files")
527* create_js_source_maps("create_source_maps")
528* html_to_wrapper("html_wrapper_files")
529* css_to_wrapper("css_wrapper_files")
530* copy("copy_mojo")
rbpottere31fb8b2024-03-12 15:42:00531* webui_path_mappings("build_path_map")
Mike Frysingerfa3a0c92023-06-08 02:33:01532* ts_library("build_ts")
533* merge_js_source_maps("merge_source_maps")
534* bundle_js("build_bundle")
535* minify_js("build_min_js")
536* generate_grd("build_grd")
537* generate_grd("build_grdp")
538* grit("resources")
dpapad689e8272022-07-12 23:59:41539
540Some targets are only conditionally defined based on build_webui() input
541parameters.
542
dpapadf7a2c7a2023-03-16 20:26:28543Only ":build_ts", ":resources" and ":build_grdp" targets are public and can be
544referred to from other parts of the build.
dpapad689e8272022-07-12 23:59:41545
546#### **Arguments**
547```
548
549List of files params:
dpapad39cac962023-01-11 22:52:52550static_files: Optional parameter. List of
dpapad57fa7892022-08-03 03:45:15551 1) non Web Component HTML/CSS files (don't confuse with
552 |css_files| below). These are passed to preprocess_if_expr()
553 2) JPG/PNG/SVG files. These are included in the build verbatim
554 without any preprocessing.
dpapad689e8272022-07-12 23:59:41555
556web_component_files: List of TS files that hold Web Component definitions with
557 equivalent HTML template files. These can be either native
558 or Polymer Web Components. Optional parameter.
559
560non_web_component_files: List of TS files that are not Web Components, or
561 Web Component files that don't have a corresponding
562 HTML template (rare case). Optional parameter.
563
564icons_html_files: List of HTML files that hold Polymer iron-iconset-svg
565 instances. Optional parameter.
566
567css_files: List of CSS files that hold Polymer style modules, or CSS variable
568 definitions. These are passed css_to_wrapper(). Optional parameter.
569
570mojo_files: List of Mojo JS generated files. These will be copied to a temporary
rbpottere31fb8b2024-03-12 15:42:00571 location so that they can be passed to ts_library() along with
572 other files. Optional parameter.
dpapad689e8272022-07-12 23:59:41573
574mojo_files_deps: List of Mojo targets that generate |mojo_files|. Must be
575 defined if |mojo_files| is defined.
576
dpapadbbee30ae2023-05-20 00:37:40577mojo_base_path: Specifies the directory under which Mojo files will be served at
578 runtime. Optional parameter. Defaults to the top level folder
579 '.', which results in Mojo files being served from
580 'chrome://<webui_name>/foo.mojom-webui.js'.
581 Example: Passing 'mojom-webui' would result in Mojo files being
582 served from
583 'chrome://<webui_name>/mojom-webui/foo.mojom-webui.js'.
584
dpapad689e8272022-07-12 23:59:41585TypeScript (ts_library()) related params:
586ts_composite: See |composite| in ts_library(). Defaults to false, optional.
dpapadf7a2c7a2023-03-16 20:26:28587ts_out_dir: See |out_dir| in ts_library(). Optional parameter, defaults
588 '$target_gen_dir/tsc'
dpapad689e8272022-07-12 23:59:41589ts_definitions: See |definitions| in ts_library(). Optional parameter.
rbpottere31fb8b2024-03-12 15:42:00590ts_deps: See |deps| in ts_library(). Also used for webui_path_mappings().
591 Optional parameter.
Yuheng Huang2693a49e2022-07-26 15:20:03592ts_extra_deps: See |extra_deps| in ts_library(). Optional parameter.
dpapad689e8272022-07-12 23:59:41593ts_path_mappings: See |path_mappings| in ts_library(). Optional parameter.
Rebekah Potter03240f52023-03-01 22:50:30594ts_tsconfig_base: The tsconfig file to use for ts_library(). Optional, defaults
595 to "//tools/typescript/tsconfig_base_polymer.json" for Polymer
596 UIs (i.e. UIs that specify |web_component_files| and/or
597 |icons_html_files| and do not set |html_to_wrapper_template|
598 to "native"). Defaults to
599 "//tools/typescript/tsconfig_base.json" for non-Polymer UIs.
dpapad689e8272022-07-12 23:59:41600
dpapad391a3282022-07-19 17:38:32601HTML/CSS/JS optimization related params:
Cole Horvitz546af9f2023-04-18 23:24:54602optimize: Specifies whether any optimization steps will be used. Defaults to the
603 value of the optimize_webui GN flag.
604 When true, html_to_wrapper() and css_to_wrapper() will be invoked with
605 the |minify| flag on, to minify HTML/CSS code.
Cole Horvitz39b3c96d2023-05-02 23:32:22606 When true, minify_js() will be invoked to minify JS code (using Terser).
607 If |optimize_webui_in_files| is provided then bundle_js() will also be
608 invoked to bundle JS code (using Rollup).
rbpotter49d0af32024-03-21 22:36:09609 |optimize_webui_host| must be specified if |optimize_webui_in_files|
dpapadf7a2c7a2023-03-16 20:26:28610 is provided.
rbpotter49d0af32024-03-21 22:36:09611optimize_ webui_host: See |host| in bundle_js().
Cole Horvitz39b3c96d2023-05-02 23:32:22612optimize_webui_excludes: See |excludes| in bundle_js(). Optional.
dpapadf79e4c0e2023-04-13 22:35:16613optimize_webui_external_paths: See |external_paths| in optimize_webui().
614 Optional.
Cole Horvitz39b3c96d2023-05-02 23:32:22615optimize_webui_in_files: See |in_files| in bundle_js().
dpapad689e8272022-07-12 23:59:41616
617Other params:
rbpotter49d0af32024-03-21 22:36:09618webui_context_type: See |webui_context_type| in webui_path_mappings(). Optional,
rbpotter075012c2024-03-27 00:09:00619 defaults to "relative".
dpapadf7a2c7a2023-03-16 20:26:28620generate_grdp: Whether to generate grdp file instead of a grd file. Defaults to
621 false.
dpapad689e8272022-07-12 23:59:41622grd_prefix: See |grd_prefix| in generate_grd(). Required parameter.
dpapad39cac962023-01-11 22:52:52623grd_resource_path_prefix: See |resource_path_prefix| in generate_grd(). Optional
624 parameter.
dpapad689e8272022-07-12 23:59:41625html_to_wrapper_template: See |template| in html_to_wrapper().
dpapad27f4ec82023-03-24 16:34:38626html_to_wrapper_scheme: See |scheme| in html_to_wrapper().
dpapad689e8272022-07-12 23:59:41627extra_grdp_deps: List of external generate_grd() targets that generate .grdp
628 files. These will be included in the final generated
629 resources.grd file. Optional parameter.
630extra_grdp_files: Output .grdp files of external generate_grd() targets. Must be
631 defined if |extra_grdp_deps| is defined.
Luc Nguyenfa9eb63b2024-02-20 23:19:07632preprocessor_defines: Optional parameter. See |defines| in preprocess_if_expr().
dpapad838ef532022-09-09 08:41:53633grit_output_dir: See |output_dir| in grit(). Optional parameter, defaults to
634 "$root_gen_dir/chrome"
dpapadc854fcf92023-02-17 23:03:32635enable_source_maps: Defaults to "false". Incompatible with |optimize=true|.
636 Setting it to "true" turns on source map generation for a
637 few underlying targets. See ts_library()'s
638 |enable_source_maps| for more details.
dpapad689e8272022-07-12 23:59:41639```
640
641#### **Example**
642```
Antonio Gomes282f5c4f92023-02-15 13:36:21643import("//ui/webui/resources/tools/build_webui.gni")
dpapad689e8272022-07-12 23:59:41644
645build_webui("build") {
646 grd_prefix = "dummy-webui"
647
648 static_files = [
649 "index.html",
650 "index.css",
651 ]
652
653 # Files holding a CustomElement element definition AND have an equivalent
654 # .html template file.
655 web_component_files = [
656 "app.ts",
657 "bar_view.ts",
658 "foo_view.ts",
659 ]
660
661 # Files not holding a CustomElement element definition, or the CustomElement
662 # does not have a corresponding HTML template.
663 non_web_component_files = [
664 "app_proxy.ts",
665 "bar_proxy.ts",
666 "foo_proxy.ts",
667 ]
668
669 # Files that are passed as input to css_to_wrapper().
670 css_files = [
671 "shared_style.css",
672 "shared_vars.css",
673 ]
674
675 ts_definitions = [
676 "//tools/typescript/definitions/chrome_send.d.ts",
677 "//tools/typescript/definitions/metrics_private.d.ts",
678 ]
679
680 ts_deps = [
681 "//third_party/polymer/v3_0:library",
dpapadf0e349692023-01-31 01:14:21682 "//ui/webui/resources/cr_elements:build_ts",
683 "//ui/webui/resources/js:build_ts",
dpapad689e8272022-07-12 23:59:41684 ]
Luc Nguyenfa9eb63b2024-02-20 23:19:07685
686 preprocessor_defines = [ "foo_enabled=false" ]
dpapad689e8272022-07-12 23:59:41687}
688
689```
690
dpapadc2db8ac2022-12-14 01:36:40691### **build_webui_tests**
692
693The flow diagram below shows a high level view of how a typical modern user-facing
694WebUI's tests are built.
695
696![WebUI tests build pipeline flow diagram](images/webui_build_pipeline_tests.png)
697
dpapadc2db8ac2022-12-14 01:36:40698This umbrella rule captures the most common WebUI test build pipeline
699configuration and aims to hide the complexity of having to define multiple other
700targets directly. Using build_webui_tests() is the recommended approach for most
701modern user-facing WebUIs tests that reside under
702chrome/test/data/webui/<webui_name>/ and are served from
703chrome://webui-test/<webui_name>/
704
705Under the cover, build_webui_tests() defines the following targets
706
Mike Frysingerfa3a0c92023-06-08 02:33:01707* preprocess_if_expr("preprocess")
rbpottere31fb8b2024-03-12 15:42:00708* webui_ts_library("build_ts")
Mike Frysingerfa3a0c92023-06-08 02:33:01709* generate_grd("build_grdp")
dpapadc2db8ac2022-12-14 01:36:40710
711The parameters passed to build_webui_tests() are forwarded as needed to
712the targets above. Only the ":build_grdp" target is public and can be referred
713to from other parts of the build.
714
dpapadc2db8ac2022-12-14 01:36:40715#### **Arguments**
716```
Rebekah Potter9ccc1d822023-11-06 23:58:41717is_chrome_untrusted: Set to true if testing a chrome-untrusted:// UI. Optional
718 parameter. Allows importing shared test files from
719 chrome-untrusted://webui-test/ instead of
rbpotter38f86ac2024-03-14 22:42:50720 chrome://webui-test. Passing true will set
721 |webui_context_type| to 'untrusted' rather than 'trusted'
722 for webui_ts_library().
dpapadc2db8ac2022-12-14 01:36:40723
724List of files params:
725files: Required parameter. List of all test related files.
726
727TypeScript (ts_library()) related params:
dpapad1de7e972023-03-01 02:49:48728ts_tsconfig_base: See |tsconfig_base| in ts_library(). Optional parameter. If
729 not provided the default configuration at
730 '//chrome/test/data/webui/tsconfig_base.json' is used.
rbpotter06eb0252024-03-21 15:38:24731ts_composite: See |composite| in ts_library(). Defaults to false, optional.
dpapad8ab521b2022-12-15 06:51:06732ts_definitions: See |definitions| in ts_library(). Optional parameter.
dpapadc2db8ac2022-12-14 01:36:40733ts_deps: See |deps| in ts_library(). Required parameter.
dpapad8ab521b2022-12-15 06:51:06734ts_path_mappings: See |path_mappings| in ts_library(). Optional parameter.
Lei Zhang820731d2023-11-04 01:15:11735
736Grit (generate_grd()) related params:
737resource_path_prefix: See |resource_path_prefix| in generate_grd(). Optional
738 parameter. Only specify it for targets residing outside of
739 //chrome/test/data/webui.
dpapadc2db8ac2022-12-14 01:36:40740```
741
742#### **Example**
743```
744import("//chrome/test/data/webui/settings/tools/build_webui_tests.gni")
745
746build_webui_tests("build") {
dpapadc2db8ac2022-12-14 01:36:40747 files = [
748 "checkbox_tests.ts",
749 "collapse_radio_button_tests.ts",
750 "controlled_button_tests.ts",
751 "controlled_radio_button_tests.ts",
752 "dropdown_menu_tests.ts",
753 ]
754
755 ts_path_mappings =
756 [ "chrome://settings/*|" +
757 rebase_path("$root_gen_dir/chrome/browser/resources/settings/tsc/*",
758 target_gen_dir) ]
759
760 ts_definitions = [
761 "//tools/typescript/definitions/chrome_send.d.ts",
762 "//tools/typescript/definitions/settings_private.d.ts",
763 ]
764
765 ts_deps = [
766 "//chrome/browser/resources/settings:build_ts",
767 ]
768}
769
770```
771
Rebekah Potterd38c2c42022-06-28 21:21:22772## Example build configurations
773
774### **Simple UI with no web components**
775
776```
777This UI has only a single checked in TypeScript file, my_debug_page.ts, and a
778checked in HTML file my_debug_page_index.html that imports the compiled JS
779file using a script tag with `src="my_debug_page.js"` Neither of these files
780use any `<if expr>`.
781```
782
783#### **BUILD.gn file**
784```
785import("//chrome/common/features.gni")
786import("//tools/grit/grit_rule.gni")
787import("//tools/typescript/ts_library.gni")
788import("//ui/webui/resources/tools/generate_grd.gni")
789
790# This UI has only a single TypeScript input file that does not define a Web
791# Component. TS compiler will write out "$target_gen_dir/tsc/my_debug_page.js".
792ts_library("build_ts") {
793 root_dir = "."
794 out_dir = "$target_gen_dir/tsc"
795 in_files = [ "my_debug_page.ts" ]
dpapadf0e349692023-01-31 01:14:21796 deps = [ "//ui/webui/resources/js:build_ts" ]
Rebekah Potterd38c2c42022-06-28 21:21:22797}
798
799# Both the HTML file and the JS file created by ts_library need to be listed in
Luciano Pachecoc3c35042022-07-01 09:33:39800# the grd. The JS file is already listed in the build_ts.manifest generated by
Rebekah Potterd38c2c42022-06-28 21:21:22801# ts_library(). Pass the HTML file via |input_files|.
802generate_grd("build_grd") {
803 deps = [ ":build_ts" ]
804 grd_prefix= "my_debug_page"
805 out_grd = "$target_gen_dir/resources.grd"
806 input_files = [ "my_debug_page_index.html" ]
807 input_files_base_dir = rebase_path(".", "//")
Cole Horvitz8ecd16f2023-06-01 00:47:22808 manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ])
Rebekah Potterd38c2c42022-06-28 21:21:22809}
810
811# Create the pak, header, and resource map files.
812grit("resources") {
813 defines = chrome_grit_defines
814
815 # This arguments is needed since the grd is generated at build time.
816 enable_input_discovery_for_gn_analyze = false
817 source = "$target_gen_dir/resources.grd"
818 deps = [ ":build_grd" ]
819 outputs = [
820 "grit/my_debug_page_resources.h",
821 "grit/my_debug_page_resources_map.cc",
822 "grit/my_debug_page_resources_map.h",
823 "my_debug_page_resources.pak",
824 ]
825 output_dir = "$root_gen_dir/chrome"
826}
827```
828
829### **Non-Polymer UI with Web Components**
830```
831This UI has a top level TypeScript file that imports a single Web Component,
832my_debug_app.ts. It has a corresponding HTML file for the app and a HTML file
833for the top level index. None of these files use any `<if expr>`.
834```
835
836#### **BUILD.gn file**
837```
838import("//chrome/common/features.gni")
839import("//tools/grit/grit_rule.gni")
840import("//tools/typescript/ts_library.gni")
841import("//ui/webui/resources/tools/generate_grd.gni")
842import("//tools/polymer/html_to_wrapper.gni")
843
844# Generate the wrapper file. This UI isn't using Polymer, so it needs to set
845# the |template| argument.
846html_to_wrapper("html_wrapper_files") {
847 in_files = [ "my_debug_app.html" ]
848 template = "native"
849}
850
851# Copy the checked-in files to the same directory. This is needed so that the
852# TypeScript compiler can find all the files in the expected location.
853copy("copy_files") {
854 sources = [
855 "my_debug_app.ts",
856 "my_debug_page.ts",
857 ]
858 outputs = [ "$target_gen_dir/{{source_file_part}}" ]
859}
860
861# This UI has 3 TypeScript input files. One is the generated
862# my_debug_app.html.ts, two are checked in: my_debug_page.ts and
863# my_debug_app.ts. TS compiler will write out:
864# "$target_gen_dir/tsc/my_debug_app.html.js",
865# "$target_gen_dir/tsc/my_debug_app.js", and
866# "$target_gen_dir/tsc/my_debug_page.js".
867ts_library("build_ts") {
868 root_dir = target_gen_dir
869 out_dir = "$target_gen_dir/tsc"
870 in_files = [
871 "my_debug_app.ts",
872 "my_debug_app.html.ts",
873 "my_debug_page.ts",
874 ]
dpapadf0e349692023-01-31 01:14:21875 deps = [ "//ui/webui/resources/js:build_ts" ]
Rebekah Potterd38c2c42022-06-28 21:21:22876 extra_deps = [
877 ":copy_files",
878 ":html_wrapper_files",
879 ]
880}
881
882# The HTML file and the JS files created by ts_library need to be listed in
Luciano Pachecoc3c35042022-07-01 09:33:39883# the grd. The JS files are already listed in the build_ts.manifest generated by
Rebekah Potterd38c2c42022-06-28 21:21:22884# ts_library(). Pass the HTML file via |input_files|.
885generate_grd("build_grd") {
886 deps = [ ":build_ts" ]
887 grd_prefix= "my_debug_page"
888 out_grd = "$target_gen_dir/resources.grd"
889 input_files = [ "my_debug_page_index.html" ]
890 input_files_base_dir = rebase_path(".", "//")
Cole Horvitz8ecd16f2023-06-01 00:47:22891 manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ])
Rebekah Potterd38c2c42022-06-28 21:21:22892}
893
894# Create the pak, header, and resource map files.
895grit("resources") {
896 defines = chrome_grit_defines
897
898 # This arguments is needed since the grd is generated at build time.
899 enable_input_discovery_for_gn_analyze = false
900 source = "$target_gen_dir/resources.grd"
901 deps = [ ":build_grd" ]
902 outputs = [
903 "grit/my_debug_page_resources.h",
904 "grit/my_debug_page_resources_map.cc",
905 "grit/my_debug_page_resources_map.h",
906 "my_debug_page_resources.pak",
907 ]
908 output_dir = "$root_gen_dir/chrome"
909}
910```