Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 1 | <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 |
| 21 | WebUI builds are configured using BUILD.gn files, which live in the top level |
| 22 | directory for a WebUI. For example, the BUILD.gn file for the Downloads page |
| 23 | is located at chrome/browser/resources/downloads/BUILD.gn. |
| 24 | |
| 25 | These files specify the build steps necessary to convert the checked-in code to |
| 26 | the code that should be served at runtime. |
| 27 | |
Rebekah Potter | 2b9d269 | 2023-02-01 01:16:20 | [diff] [blame] | 28 | ## 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 Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 35 | [build_webui_tests](#build_webui_tests)). If you want to use any of the rules |
dpapad | f7a2c7a | 2023-03-16 20:26:28 | [diff] [blame] | 36 | below from within another tool or script, please get a review from one of the |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 37 | cross-platform, non-backend WebUI [OWNERS](/ui/webui/PLATFORM_OWNERS). |
Rebekah Potter | 2b9d269 | 2023-02-01 01:16:20 | [diff] [blame] | 38 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 39 | ## WebUI build rules |
| 40 | |
| 41 | ### **html_to_wrapper, css_to_wrapper** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 42 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 43 | These rules are used to inline HTML or CSS into a TypeScript file which can be |
| 44 | compiled by TS compiler and then imported with JS imports at runtime. This is |
| 45 | necessary when writing Web Components, which need to return their HTML in the |
| 46 | `template()` getter method. |
| 47 | |
| 48 | By default, these rules accept input files from within the current directory. |
| 49 | |
| 50 | By default, they output the wrapped `.html.ts` and `.css.ts` files to the target |
| 51 | generated directory (|target_gen_dir|). |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 52 | |
| 53 | #### **Arguments** |
| 54 | ``` |
| 55 | in_files: specifies the list of files to process with respect to the |
| 56 | |in_folder|. |
dpapad | 07cd33f | 2023-01-20 19:08:30 | [diff] [blame] | 57 | template: 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) |
dpapad | f237f35bc | 2022-07-01 23:29:07 | [diff] [blame] | 62 | in_folder: Specifies the input folder where files are located. If not specified, |
| 63 | the current directory (of the BUILD.gn file) is used. |
| 64 | out_folder: Specifies the location to write the wrapped files. If not specified, |
| 65 | |target_gen_dir| is used. |
dpapad | 0fb3780 | 2022-07-13 01:00:52 | [diff] [blame] | 66 | minify: Whether to minify HTML/CSS with |
dpapad | d55b8f7 | 2022-06-29 19:37:32 | [diff] [blame] | 67 | third_party/node/node_modules/html-minifier. Defaults to false. |
dpapad | 6de5a2e | 2022-07-26 16:22:19 | [diff] [blame] | 68 | use_js: Whether to output .js files instead of .ts files. Defaults to false. |
dpapad | 2efd445 | 2023-04-06 01:43:45 | [diff] [blame] | 69 | scheme: One of ['chrome', 'relative']. Defaults to 'relative'. Specifies whether |
dpapad | 24071ff | 2022-09-02 01:07:54 | [diff] [blame] | 70 | dependencies of the generated wrapper file should be imported with |
| 71 | "chrome://resources" or scheme-relative "//resources" URLs. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 72 | ``` |
| 73 | |
| 74 | #### **Example** |
| 75 | ``` |
| 76 | import("//tools/polymer/html_to_wrapper.gni") |
| 77 | import("//tools/polymer/css_to_wrapper.gni") |
| 78 | |
| 79 | # Generates "my_web_component.html.ts" in |target_gen_dir|. |
| 80 | html_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|. |
| 85 | css_to_wrapper("css_wrapper_files") { |
| 86 | in_files = [ "my_style_module.css" ] |
| 87 | } |
| 88 | ``` |
| 89 | |
| 90 | ### **preprocess_if_expr** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 91 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 92 | This rule is used to preprocess files containing `<if expr="*">`. These |
| 93 | expressions are most frequently used to enable code to only run on certain |
| 94 | platforms. |
dpapad | bb1e3aa8 | 2022-06-29 05:31:10 | [diff] [blame] | 95 | |
| 96 | By default, reads input files from within the current directory and saves output |
| 97 | in |target_gen_dir|. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 98 | |
| 99 | #### **Arguments** |
| 100 | ``` |
| 101 | in_folder: specifies the input folder, where all input files are located. |
dpapad | bb1e3aa8 | 2022-06-29 05:31:10 | [diff] [blame] | 102 | Defaults to the folder where the BUILD.gn file resides. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 103 | out_folder: specifies the folder that the preprocessed files should be placed |
dpapad | bb1e3aa8 | 2022-06-29 05:31:10 | [diff] [blame] | 104 | in. This must be a generated directory. Defaults to |
| 105 | |target_gen_dir|. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 106 | in_files: specifies the list of input files to preprocess with respect to the |
| 107 | |in_folder|. |
| 108 | out_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 Nguyen | fa9eb63b | 2024-02-20 23:19:07 | [diff] [blame] | 114 | defines: Optional parameter. Specifies additional variables that can be used in |
| 115 | conditional expressions. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 116 | ``` |
| 117 | #### **Example:** |
| 118 | ``` |
| 119 | import("//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". |
| 123 | preprocess_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 Nguyen | fa9eb63b | 2024-02-20 23:19:07 | [diff] [blame] | 135 | defines = [ "foo_enabled=false" ] |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | # Preprocess "my_web_component.ts" and "my_webui.ts" in the src dir into |
| 139 | # "$target_gen_dir/preprocessed". |
| 140 | preprocess_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 Nguyen | fa9eb63b | 2024-02-20 23:19:07 | [diff] [blame] | 147 | defines = [ "foo_enabled=true" ] |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 148 | } |
| 149 | ``` |
| 150 | |
| 151 | ### **ts_library** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 152 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 153 | This rule is used to compile TypeScript code to JavaScript. It outputs JS files |
| 154 | corresponding to each TS file passed as an input into the designated output |
| 155 | directory, and generates a manifest listing all the files it has output named |
Luciano Pacheco | c3c3504 | 2022-07-01 09:33:39 | [diff] [blame] | 156 | $target_name.manifest in the target generated directory. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 157 | |
| 158 | #### **Input File notes** |
| 159 | ``` |
| 160 | All input files must be valid TypeScript. This means, among other things, that |
| 161 | files shouldn't contain any `<if expr>`, i.e. they should already have been |
| 162 | preprocessed, if necessary. It also means that e.g. HTML or image files |
| 163 | shouldn't be passed to ts_library. |
| 164 | |
| 165 | All files that aren't imported with an absolute path (e.g. |
dpapad | 01dc2965 | 2023-10-12 22:11:22 | [diff] [blame] | 166 | `import {assert} from 'chrome://resources/js/assert.js'; `) need to exist |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 167 | inside the TypeScript root directory, at the expected location. For example, |
| 168 | if 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 |
| 170 | is invoked should look like this: |
| 171 | root_dir/ |
| 172 | foo.ts |
| 173 | bar/ |
| 174 | baz.ts |
| 175 | ``` |
| 176 | #### **Arguments** |
| 177 | ``` |
| 178 | root_dir: This is the root directory where all TypeScript files to compile |
| 179 | must reside (see note above). |
| 180 | out_dir: The directory where ts_library will write the compiled JavaScript |
| 181 | files. |
| 182 | composite: 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). |
| 185 | in_files: The input file paths to be compiled, with respect to the |in_folder|. |
| 186 | definitions: TypeScript definitions files used by the input TypeScript files, |
| 187 | for example chrome.send or extension API definitions. |
| 188 | tsconfig_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 |
| 191 | deps: Specifies all other ts_library targets generating libraries that this |
| 192 | target's library needs, for example the shared resources library at |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 193 | //ui/webui/resources/js:build_ts. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 194 | extra_deps: Used to specify build targets generating the input files for TS |
| 195 | compiler. |
| 196 | path_mappings: Additional non-default path mappings for absolute imports. The |
Rebekah Potter | 84a6b56d | 2023-02-01 01:17:26 | [diff] [blame] | 197 | 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'). |
rbpotter | 1df55739 | 2024-02-28 00:49:55 | [diff] [blame] | 202 | 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|. |
rbpotter | e31fb8b | 2024-03-12 15:42:00 | [diff] [blame] | 213 | path_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 Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 216 | manifest_excludes: List of input files to exclude from the output |
Luciano Pacheco | c3c3504 | 2022-07-01 09:33:39 | [diff] [blame] | 217 | the manifest file. |
Tibor Goldschwendt | 1b57bd5 | 2023-05-03 21:49:55 | [diff] [blame] | 218 | enable_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 Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 222 | ``` |
| 223 | |
| 224 | #### **Example** |
| 225 | |
| 226 | ``` |
| 227 | import("//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. |
| 231 | ts_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", |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 243 | "//ui/webui/resources/js:build_ts", |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 244 | ] |
| 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 | |
rbpotter | 890a38a | 2024-03-12 01:21:41 | [diff] [blame] | 256 | ### **webui_path_mappings** |
| 257 | |
| 258 | This rule generates a path mappings .json file named |
| 259 | 'path_mappings_<target_name>.json' in |target_gen_dir| from a list of target |
rbpotter | e31fb8b | 2024-03-12 15:42:00 | [diff] [blame] | 260 | dependencies. Its output can be passed to ts_library's |path_mappings_file| |
| 261 | parameter. |
rbpotter | 890a38a | 2024-03-12 01:21:41 | [diff] [blame] | 262 | |
| 263 | Note that the rule only generates mappings for dependencies that are mapped |
| 264 | in path_mappings.py (e.g. //ui/webui/resources/ deps). |
| 265 | |
| 266 | #### **Arguments** |
| 267 | ``` |
| 268 | ts_deps: List of ts_library() dependencies to generate path mappings for. |
rbpotter | 38f86ac | 2024-03-14 22:42:50 | [diff] [blame] | 269 | webui_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. |
rbpotter | 890a38a | 2024-03-12 01:21:41 | [diff] [blame] | 276 | ``` |
| 277 | |
rbpotter | e31fb8b | 2024-03-12 15:42:00 | [diff] [blame] | 278 | ### **webui_ts_library** |
| 279 | |
| 280 | This 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** |
| 287 | webui_ts_library uses all the same arguments as ts_library, in addition to |
| 288 | the following: |
| 289 | ``` |
rbpotter | 38f86ac | 2024-03-14 22:42:50 | [diff] [blame] | 290 | webui_context_type: See |webui_context_type| in webui_path_mappings(). |
rbpotter | e31fb8b | 2024-03-12 15:42:00 | [diff] [blame] | 291 | ``` |
| 292 | |
Cole Horvitz | 253c079a | 2023-04-28 00:34:42 | [diff] [blame] | 293 | ### **bundle_js** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 294 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 295 | This rule is used to bundle larger user-facing WebUIs for improved performance. |
| 296 | It is generally not needed for debug UIs or UIs that have very few files to |
| 297 | import. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 298 | |
| 299 | #### **Arguments** |
| 300 | ``` |
| 301 | host: 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. |
| 304 | input: The location of the input files to be bundled. |
| 305 | js_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 Potter | 018fa87 | 2023-01-28 00:54:28 | [diff] [blame] | 308 | 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 Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 314 | out_manifest: File location to write the manifest of all output files created |
Cole Horvitz | 253c079a | 2023-04-28 00:34:42 | [diff] [blame] | 315 | by bundle_js(). Useful for generating grds. |
dpapad | 4faf2e2 | 2023-11-28 23:03:46 | [diff] [blame] | 316 | rollup_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 Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 322 | deps: Targets generating any files being bundled. Note that this should include |
| 323 | targets generating shared resources that are expected to be bundled in |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 324 | the UI, e.g. //ui/webui/resources/js:build_ts. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 325 | excludes: Paths of files that are not bundled. Often used for large mojo files |
Rebekah Potter | 952290e | 2022-11-18 09:07:28 | [diff] [blame] | 326 | that would otherwise be in many bundles, and for cr.js which relies |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 327 | on global variables. |
Rebekah Potter | 06eee9c | 2023-02-22 03:29:39 | [diff] [blame] | 328 | external_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 Horvitz | 8ca0146 | 2023-05-22 18:34:11 | [diff] [blame] | 338 | out_folder: The location where bundled files will be placed in. Defaults to |
| 339 | |target_gen_dir|. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 340 | ``` |
| 341 | |
| 342 | #### **Example** |
| 343 | ``` |
Cole Horvitz | 253c079a | 2023-04-28 00:34:42 | [diff] [blame] | 344 | import("//ui/webui/resources/tools/bundle_js.gni") |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 345 | import ("//chrome/common/features.gni") |
| 346 | |
| 347 | # optimize_webui should generally only be called when the optimize_webui |
| 348 | # feature flag is enabled. |
| 349 | if (optimize_webui) { |
Cole Horvitz | 253c079a | 2023-04-28 00:34:42 | [diff] [blame] | 350 | bundle_js("build") { |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 351 | host = "mywebui" |
Rebekah Potter | 018fa87 | 2023-01-28 00:54:28 | [diff] [blame] | 352 | js_module_in_files = [ "my_webui.js" ] # will output my_webui.rollup.js |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 353 | input = rebase_path("$target_gen_dir/tsc", root_build_dir) |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 354 | 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", |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 359 | "//ui/webui/resources/js:build_ts", |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 360 | ] |
| 361 | excludes = [ |
Rebekah Potter | 952290e | 2022-11-18 09:07:28 | [diff] [blame] | 362 | "chrome://resources/js/cr.js", |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 363 | "chrome://resources/mojo/mojo/public/js/bindings.js", |
| 364 | ] |
| 365 | } |
| 366 | } |
| 367 | ``` |
| 368 | |
Cole Horvitz | 251fd88 | 2023-04-14 18:26:59 | [diff] [blame] | 369 | ### **minify_js** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 370 | |
Cole Horvitz | 251fd88 | 2023-04-14 18:26:59 | [diff] [blame] | 371 | This rule is used to minify Javascript files to reduce build size. |
Cole Horvitz | e368f0d | 2023-05-24 23:56:13 | [diff] [blame] | 372 | This can be used alongside bundle_js(), if bundling and minifying is |
| 373 | desired. |
Cole Horvitz | 251fd88 | 2023-04-14 18:26:59 | [diff] [blame] | 374 | |
| 375 | #### **Arguments** |
| 376 | ``` |
| 377 | in_folder: The location of the input files to be minified. |
| 378 | in_files: The list of JS files to minify with respect to the |in_folder|. |
| 379 | out_folder: The location where minified files will be outputted. |
Cole Horvitz | e368f0d | 2023-05-24 23:56:13 | [diff] [blame] | 380 | out_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 Horvitz | 251fd88 | 2023-04-14 18:26:59 | [diff] [blame] | 383 | deps: Targets generating any files being minified. |
| 384 | ``` |
| 385 | |
| 386 | #### **Example** |
| 387 | ``` |
| 388 | import("//ui/webui/resources/tools/minify_js.gni") |
| 389 | import ("//chrome/common/features.gni") |
| 390 | |
| 391 | # minify_js should generally only be called when the optimize_webui |
| 392 | # GN flag is enabled. |
| 393 | if (optimize_webui) { |
| 394 | minify_js("build") { |
| 395 | in_files = [ "my_webui.js" ] |
| 396 | in_folder = "$target_gen_dir/tsc" |
Cole Horvitz | 546af9f | 2023-04-18 23:24:54 | [diff] [blame] | 397 | out_folder = "$target_gen_dir/minified" |
Cole Horvitz | 251fd88 | 2023-04-14 18:26:59 | [diff] [blame] | 398 | # Assumes the JS files were generated by a ts_library target called |
| 399 | # build_ts. |
| 400 | deps = [ ":build_ts" ] |
| 401 | } |
| 402 | } |
| 403 | ``` |
| 404 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 405 | ### **generate_grd** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 406 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 407 | This rule is used to list the WebUI resources that need to be served at runtime |
| 408 | in a grd file. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 409 | |
| 410 | #### **Arguments** |
| 411 | ``` |
| 412 | input_files: Input file paths to list in the grd file. |
| 413 | input_files_base_dir: Directory in which |input_files| can be found. |
| 414 | deps: Lists any targets responsible for generating |input_files|, |
| 415 | |grdp files|, or |manifest_files|. |
| 416 | manifest_files: List of manifest files listing additional files that should be |
| 417 | added to the grd. |
| 418 | grdp_files: List of .grdp files that should be included in the grd. Generally |
dpapad | 973b3698 | 2023-03-15 23:20:55 | [diff] [blame] | 419 | 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 Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 422 | grd_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 |
dpapad | 973b3698 | 2023-03-15 23:20:55 | [diff] [blame] | 424 | out_grd: The output grd file to write. Must end with either '.grd' or '.grdp'. |
Mike Frysinger | 5c08d79 | 2023-06-16 19:20:58 | [diff] [blame] | 425 | resource_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 Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 429 | resource_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 | ``` |
| 441 | import("//ui/webui/resources/tools/generate_grd.gni") |
| 442 | |
| 443 | generate_grd("build_grd") { |
| 444 | input_files = [ "my_webui.html" ] |
| 445 | input_files_base_dir = rebase_path(".", "//") |
| 446 | deps = [ ":build_ts" ] |
Cole Horvitz | 8ecd16f | 2023-06-01 00:47:22 | [diff] [blame] | 447 | manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ]) |
Luciano Pacheco | c3c3504 | 2022-07-01 09:33:39 | [diff] [blame] | 448 | # Or, configure statically the manifest file name: |
| 449 | # manifest_files = [ "$target_gen_dir/build_ts.manifest" ] |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 450 | grd_prefix = "my_webui" |
| 451 | out_grd = "$target_gen_dir/${grd_prefix}_resources.grd" |
| 452 | } |
| 453 | ``` |
| 454 | |
| 455 | ### **grit** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 456 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 457 | This rule is used to create a pak file from the grd file that contains all the |
| 458 | listed resources, so that they can be included in the binary and served at |
| 459 | runtime. Without creating a grit rule (and hooking up the target to the build), |
| 460 | the WebUI will not load since the resources will not exist. This rule also |
| 461 | generates C++ header files that are used to add the files to a specific |
| 462 | WebUIController. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 463 | |
| 464 | #### **Arguments** |
| 465 | ``` |
| 466 | defines: The defines that grit should use when making the pak file. Generally |
| 467 | should include chrome_grit_defines. |
| 468 | enable_input_discovery_for_gn_analyze: If using generated grd files, this needs |
| 469 | to be set to false. |
| 470 | source: The source grd file. |
| 471 | outputs: 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. |
| 474 | output_dir: Location to put the pak file. Often "$root_gen_dir/chrome". |
| 475 | deps: 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 | ``` |
| 483 | import("//tools/grit/grit_rule.gni") |
| 484 | |
| 485 | grit("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 | |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 502 | ### **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 | |
| 507 | See the [go/build-webui-pipeline](http://go/build-webui-pipeline) design doc for |
| 508 | more info (internal only). |
| 509 | |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 510 | The flow diagram below shows a high level view of how a typical modern |
| 511 | user-facing WebUI is being built. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 512 | |
| 513 |  |
| 514 | |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 515 | This umbrella rule captures the most common WebUI build pipeline configuration |
| 516 | and aims to hide the complexity of having to define all previously described |
| 517 | rules directly. Using build_webui() is the recommended approach for most modern |
| 518 | user-facing WebUIs that use WebComponents+TypeScript+Mojo. |
| 519 | |
| 520 | The parameters passed to build_webui() are forwarded as needed to the other GN |
| 521 | rules described earlier. |
| 522 | |
| 523 | Under the cover, build_webui() defines the following targets |
| 524 | |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 525 | * 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") |
rbpotter | e31fb8b | 2024-03-12 15:42:00 | [diff] [blame] | 531 | * webui_path_mappings("build_path_map") |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 532 | * 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") |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 539 | |
| 540 | Some targets are only conditionally defined based on build_webui() input |
| 541 | parameters. |
| 542 | |
dpapad | f7a2c7a | 2023-03-16 20:26:28 | [diff] [blame] | 543 | Only ":build_ts", ":resources" and ":build_grdp" targets are public and can be |
| 544 | referred to from other parts of the build. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 545 | |
| 546 | #### **Arguments** |
| 547 | ``` |
| 548 | |
| 549 | List of files params: |
dpapad | 39cac96 | 2023-01-11 22:52:52 | [diff] [blame] | 550 | static_files: Optional parameter. List of |
dpapad | 57fa789 | 2022-08-03 03:45:15 | [diff] [blame] | 551 | 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. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 555 | |
| 556 | web_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 | |
| 560 | non_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 | |
| 564 | icons_html_files: List of HTML files that hold Polymer iron-iconset-svg |
| 565 | instances. Optional parameter. |
| 566 | |
| 567 | css_files: List of CSS files that hold Polymer style modules, or CSS variable |
| 568 | definitions. These are passed css_to_wrapper(). Optional parameter. |
| 569 | |
| 570 | mojo_files: List of Mojo JS generated files. These will be copied to a temporary |
rbpotter | e31fb8b | 2024-03-12 15:42:00 | [diff] [blame] | 571 | location so that they can be passed to ts_library() along with |
| 572 | other files. Optional parameter. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 573 | |
| 574 | mojo_files_deps: List of Mojo targets that generate |mojo_files|. Must be |
| 575 | defined if |mojo_files| is defined. |
| 576 | |
dpapad | bbee30ae | 2023-05-20 00:37:40 | [diff] [blame] | 577 | mojo_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 | |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 585 | TypeScript (ts_library()) related params: |
| 586 | ts_composite: See |composite| in ts_library(). Defaults to false, optional. |
dpapad | f7a2c7a | 2023-03-16 20:26:28 | [diff] [blame] | 587 | ts_out_dir: See |out_dir| in ts_library(). Optional parameter, defaults |
| 588 | '$target_gen_dir/tsc' |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 589 | ts_definitions: See |definitions| in ts_library(). Optional parameter. |
rbpotter | e31fb8b | 2024-03-12 15:42:00 | [diff] [blame] | 590 | ts_deps: See |deps| in ts_library(). Also used for webui_path_mappings(). |
| 591 | Optional parameter. |
Yuheng Huang | 2693a49e | 2022-07-26 15:20:03 | [diff] [blame] | 592 | ts_extra_deps: See |extra_deps| in ts_library(). Optional parameter. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 593 | ts_path_mappings: See |path_mappings| in ts_library(). Optional parameter. |
Rebekah Potter | 03240f5 | 2023-03-01 22:50:30 | [diff] [blame] | 594 | ts_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. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 600 | |
dpapad | 391a328 | 2022-07-19 17:38:32 | [diff] [blame] | 601 | HTML/CSS/JS optimization related params: |
Cole Horvitz | 546af9f | 2023-04-18 23:24:54 | [diff] [blame] | 602 | optimize: 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 Horvitz | 39b3c96d | 2023-05-02 23:32:22 | [diff] [blame] | 606 | 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). |
rbpotter | 49d0af3 | 2024-03-21 22:36:09 | [diff] [blame] | 609 | |optimize_webui_host| must be specified if |optimize_webui_in_files| |
dpapad | f7a2c7a | 2023-03-16 20:26:28 | [diff] [blame] | 610 | is provided. |
rbpotter | 49d0af3 | 2024-03-21 22:36:09 | [diff] [blame] | 611 | optimize_ webui_host: See |host| in bundle_js(). |
Cole Horvitz | 39b3c96d | 2023-05-02 23:32:22 | [diff] [blame] | 612 | optimize_webui_excludes: See |excludes| in bundle_js(). Optional. |
dpapad | f79e4c0e | 2023-04-13 22:35:16 | [diff] [blame] | 613 | optimize_webui_external_paths: See |external_paths| in optimize_webui(). |
| 614 | Optional. |
Cole Horvitz | 39b3c96d | 2023-05-02 23:32:22 | [diff] [blame] | 615 | optimize_webui_in_files: See |in_files| in bundle_js(). |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 616 | |
| 617 | Other params: |
rbpotter | 49d0af3 | 2024-03-21 22:36:09 | [diff] [blame] | 618 | webui_context_type: See |webui_context_type| in webui_path_mappings(). Optional, |
rbpotter | 075012c | 2024-03-27 00:09:00 | [diff] [blame] | 619 | defaults to "relative". |
dpapad | f7a2c7a | 2023-03-16 20:26:28 | [diff] [blame] | 620 | generate_grdp: Whether to generate grdp file instead of a grd file. Defaults to |
| 621 | false. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 622 | grd_prefix: See |grd_prefix| in generate_grd(). Required parameter. |
dpapad | 39cac96 | 2023-01-11 22:52:52 | [diff] [blame] | 623 | grd_resource_path_prefix: See |resource_path_prefix| in generate_grd(). Optional |
| 624 | parameter. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 625 | html_to_wrapper_template: See |template| in html_to_wrapper(). |
dpapad | 27f4ec8 | 2023-03-24 16:34:38 | [diff] [blame] | 626 | html_to_wrapper_scheme: See |scheme| in html_to_wrapper(). |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 627 | extra_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. |
| 630 | extra_grdp_files: Output .grdp files of external generate_grd() targets. Must be |
| 631 | defined if |extra_grdp_deps| is defined. |
Luc Nguyen | fa9eb63b | 2024-02-20 23:19:07 | [diff] [blame] | 632 | preprocessor_defines: Optional parameter. See |defines| in preprocess_if_expr(). |
dpapad | 838ef53 | 2022-09-09 08:41:53 | [diff] [blame] | 633 | grit_output_dir: See |output_dir| in grit(). Optional parameter, defaults to |
| 634 | "$root_gen_dir/chrome" |
dpapad | c854fcf9 | 2023-02-17 23:03:32 | [diff] [blame] | 635 | enable_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. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 639 | ``` |
| 640 | |
| 641 | #### **Example** |
| 642 | ``` |
Antonio Gomes | 282f5c4f9 | 2023-02-15 13:36:21 | [diff] [blame] | 643 | import("//ui/webui/resources/tools/build_webui.gni") |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 644 | |
| 645 | build_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", |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 682 | "//ui/webui/resources/cr_elements:build_ts", |
| 683 | "//ui/webui/resources/js:build_ts", |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 684 | ] |
Luc Nguyen | fa9eb63b | 2024-02-20 23:19:07 | [diff] [blame] | 685 | |
| 686 | preprocessor_defines = [ "foo_enabled=false" ] |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | ``` |
| 690 | |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 691 | ### **build_webui_tests** |
| 692 | |
| 693 | The flow diagram below shows a high level view of how a typical modern user-facing |
| 694 | WebUI's tests are built. |
| 695 | |
| 696 |  |
| 697 | |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 698 | This umbrella rule captures the most common WebUI test build pipeline |
| 699 | configuration and aims to hide the complexity of having to define multiple other |
| 700 | targets directly. Using build_webui_tests() is the recommended approach for most |
| 701 | modern user-facing WebUIs tests that reside under |
| 702 | chrome/test/data/webui/<webui_name>/ and are served from |
| 703 | chrome://webui-test/<webui_name>/ |
| 704 | |
| 705 | Under the cover, build_webui_tests() defines the following targets |
| 706 | |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 707 | * preprocess_if_expr("preprocess") |
rbpotter | e31fb8b | 2024-03-12 15:42:00 | [diff] [blame] | 708 | * webui_ts_library("build_ts") |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 709 | * generate_grd("build_grdp") |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 710 | |
| 711 | The parameters passed to build_webui_tests() are forwarded as needed to |
| 712 | the targets above. Only the ":build_grdp" target is public and can be referred |
| 713 | to from other parts of the build. |
| 714 | |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 715 | #### **Arguments** |
| 716 | ``` |
Rebekah Potter | 9ccc1d82 | 2023-11-06 23:58:41 | [diff] [blame] | 717 | is_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 |
rbpotter | 38f86ac | 2024-03-14 22:42:50 | [diff] [blame] | 720 | chrome://webui-test. Passing true will set |
| 721 | |webui_context_type| to 'untrusted' rather than 'trusted' |
| 722 | for webui_ts_library(). |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 723 | |
| 724 | List of files params: |
| 725 | files: Required parameter. List of all test related files. |
| 726 | |
| 727 | TypeScript (ts_library()) related params: |
dpapad | 1de7e97 | 2023-03-01 02:49:48 | [diff] [blame] | 728 | ts_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. |
rbpotter | 06eb025 | 2024-03-21 15:38:24 | [diff] [blame] | 731 | ts_composite: See |composite| in ts_library(). Defaults to false, optional. |
dpapad | 8ab521b | 2022-12-15 06:51:06 | [diff] [blame] | 732 | ts_definitions: See |definitions| in ts_library(). Optional parameter. |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 733 | ts_deps: See |deps| in ts_library(). Required parameter. |
dpapad | 8ab521b | 2022-12-15 06:51:06 | [diff] [blame] | 734 | ts_path_mappings: See |path_mappings| in ts_library(). Optional parameter. |
Lei Zhang | 820731d | 2023-11-04 01:15:11 | [diff] [blame] | 735 | |
| 736 | Grit (generate_grd()) related params: |
| 737 | resource_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. |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 740 | ``` |
| 741 | |
| 742 | #### **Example** |
| 743 | ``` |
| 744 | import("//chrome/test/data/webui/settings/tools/build_webui_tests.gni") |
| 745 | |
| 746 | build_webui_tests("build") { |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 747 | 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 Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 772 | ## Example build configurations |
| 773 | |
| 774 | ### **Simple UI with no web components** |
| 775 | |
| 776 | ``` |
| 777 | This UI has only a single checked in TypeScript file, my_debug_page.ts, and a |
| 778 | checked in HTML file my_debug_page_index.html that imports the compiled JS |
| 779 | file using a script tag with `src="my_debug_page.js"` Neither of these files |
| 780 | use any `<if expr>`. |
| 781 | ``` |
| 782 | |
| 783 | #### **BUILD.gn file** |
| 784 | ``` |
| 785 | import("//chrome/common/features.gni") |
| 786 | import("//tools/grit/grit_rule.gni") |
| 787 | import("//tools/typescript/ts_library.gni") |
| 788 | import("//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". |
| 792 | ts_library("build_ts") { |
| 793 | root_dir = "." |
| 794 | out_dir = "$target_gen_dir/tsc" |
| 795 | in_files = [ "my_debug_page.ts" ] |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 796 | deps = [ "//ui/webui/resources/js:build_ts" ] |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | # Both the HTML file and the JS file created by ts_library need to be listed in |
Luciano Pacheco | c3c3504 | 2022-07-01 09:33:39 | [diff] [blame] | 800 | # the grd. The JS file is already listed in the build_ts.manifest generated by |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 801 | # ts_library(). Pass the HTML file via |input_files|. |
| 802 | generate_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 Horvitz | 8ecd16f | 2023-06-01 00:47:22 | [diff] [blame] | 808 | manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ]) |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | # Create the pak, header, and resource map files. |
| 812 | grit("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 | ``` |
| 831 | This UI has a top level TypeScript file that imports a single Web Component, |
| 832 | my_debug_app.ts. It has a corresponding HTML file for the app and a HTML file |
| 833 | for the top level index. None of these files use any `<if expr>`. |
| 834 | ``` |
| 835 | |
| 836 | #### **BUILD.gn file** |
| 837 | ``` |
| 838 | import("//chrome/common/features.gni") |
| 839 | import("//tools/grit/grit_rule.gni") |
| 840 | import("//tools/typescript/ts_library.gni") |
| 841 | import("//ui/webui/resources/tools/generate_grd.gni") |
| 842 | import("//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. |
| 846 | html_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. |
| 853 | copy("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". |
| 867 | ts_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 | ] |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 875 | deps = [ "//ui/webui/resources/js:build_ts" ] |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 876 | 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 Pacheco | c3c3504 | 2022-07-01 09:33:39 | [diff] [blame] | 883 | # the grd. The JS files are already listed in the build_ts.manifest generated by |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 884 | # ts_library(). Pass the HTML file via |input_files|. |
| 885 | generate_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 Horvitz | 8ecd16f | 2023-06-01 00:47:22 | [diff] [blame] | 891 | manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ]) |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | # Create the pak, header, and resource map files. |
| 895 | grit("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 | ``` |