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. |
| 114 | ``` |
| 115 | #### **Example:** |
| 116 | ``` |
| 117 | import("//tools/grit/preprocess_if_expr.gni") |
| 118 | |
| 119 | # Preprocesses "my_web_component.html.ts" and my_style_module.css.ts in |
| 120 | # |target_gen_dir|, into "$target_gen_dir/preprocessed". |
| 121 | preprocess_if_expr("preprocess_generated") { |
| 122 | # Depend on the targets that generates these files. |
| 123 | deps = [ |
| 124 | ":css_wrapper_files", |
| 125 | ":html_wrapper_files", |
| 126 | ] |
| 127 | in_folder = target_gen_dir |
| 128 | in_files = [ |
| 129 | "my_style_module.css.ts", |
| 130 | "my_web_component.html.ts", |
| 131 | ] |
| 132 | out_folder = "$target_gen_dir/$preprocess_folder" |
| 133 | } |
| 134 | |
| 135 | # Preprocess "my_web_component.ts" and "my_webui.ts" in the src dir into |
| 136 | # "$target_gen_dir/preprocessed". |
| 137 | preprocess_if_expr("preprocess_src") { |
| 138 | in_folder = "." |
| 139 | in_files = [ |
| 140 | "my_web_component.ts", |
| 141 | "my_webui.ts", |
| 142 | ] |
| 143 | out_folder = "$target_gen_dir/$preprocess_folder" |
| 144 | } |
| 145 | ``` |
| 146 | |
| 147 | ### **ts_library** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 148 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 149 | This rule is used to compile TypeScript code to JavaScript. It outputs JS files |
| 150 | corresponding to each TS file passed as an input into the designated output |
| 151 | directory, and generates a manifest listing all the files it has output named |
Luciano Pacheco | c3c3504 | 2022-07-01 09:33:39 | [diff] [blame] | 152 | $target_name.manifest in the target generated directory. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 153 | |
| 154 | #### **Input File notes** |
| 155 | ``` |
| 156 | All input files must be valid TypeScript. This means, among other things, that |
| 157 | files shouldn't contain any `<if expr>`, i.e. they should already have been |
| 158 | preprocessed, if necessary. It also means that e.g. HTML or image files |
| 159 | shouldn't be passed to ts_library. |
| 160 | |
| 161 | All files that aren't imported with an absolute path (e.g. |
| 162 | `import {assert} from 'chrome://resources/js/assert_ts.js'; `) need to exist |
| 163 | inside the TypeScript root directory, at the expected location. For example, |
| 164 | if foo.ts in the top level directory contains the import statement |
| 165 | `import {Baz} from './bar/baz.js';`, then the folder structure when ts_library |
| 166 | is invoked should look like this: |
| 167 | root_dir/ |
| 168 | foo.ts |
| 169 | bar/ |
| 170 | baz.ts |
| 171 | ``` |
| 172 | #### **Arguments** |
| 173 | ``` |
| 174 | root_dir: This is the root directory where all TypeScript files to compile |
| 175 | must reside (see note above). |
| 176 | out_dir: The directory where ts_library will write the compiled JavaScript |
| 177 | files. |
| 178 | composite: Set this to "true" if the output needs to be used as a library by |
| 179 | a different ts_library target (this frequently occurs when tests are |
| 180 | compiled as a ts_library). |
| 181 | in_files: The input file paths to be compiled, with respect to the |in_folder|. |
| 182 | definitions: TypeScript definitions files used by the input TypeScript files, |
| 183 | for example chrome.send or extension API definitions. |
| 184 | tsconfig_base: Specifies the tsconfig_base.json file to use. Necessary only |
| 185 | if specifying configuration options for TS compiler that differ |
| 186 | from the defaults in tools/typescript/tsconfig_base.json |
| 187 | deps: Specifies all other ts_library targets generating libraries that this |
| 188 | target's library needs, for example the shared resources library at |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 189 | //ui/webui/resources/js:build_ts. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 190 | extra_deps: Used to specify build targets generating the input files for TS |
| 191 | compiler. |
| 192 | path_mappings: Additional non-default path mappings for absolute imports. The |
Rebekah Potter | 84a6b56d | 2023-02-01 01:17:26 | [diff] [blame] | 193 | absolute 'chrome://resources/' paths are already mapped for any |
| 194 | resources in libraries that are listed in |deps|; for example |
| 195 | adding "//ui/webui/resources/cr_elements:build_ts" in deps will |
| 196 | automatically add the mapping for imports from that library |
| 197 | (e.g. 'chrome://resources/cr_elements/cr_button/cr_button.js'). |
| 198 | Important: Don't add path_mappings without also adding the |
| 199 | ts_library() target(s) responsible for the files being mapped to |
| 200 | deps! path_mappings without corresponding deps can result in |
| 201 | flaky build errors. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 202 | manifest_excludes: List of input files to exclude from the output |
Luciano Pacheco | c3c3504 | 2022-07-01 09:33:39 | [diff] [blame] | 203 | the manifest file. |
Tibor Goldschwendt | 1b57bd5 | 2023-05-03 21:49:55 | [diff] [blame] | 204 | enable_source_maps: Defaults to the value of the enable_webui_inline_sourcemaps |
| 205 | GN flag. Setting it to "true" turns on TS compiler's |
| 206 | 'inlineSourceMap' and 'inlineSources' flags. Non-inlined |
| 207 | source maps are currently not supported. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 208 | ``` |
| 209 | |
| 210 | #### **Example** |
| 211 | |
| 212 | ``` |
| 213 | import("//tools/typescript/ts_library.gni") |
| 214 | |
| 215 | # Compiles and outputs my_webui.js, my_web_component.js and |
| 216 | # my_web_component.html.js, in the "$target_gen_dir/tsc" folder. |
| 217 | ts_library("build_ts") { |
| 218 | root_dir = "$target_gen_dir/preprocessed" |
| 219 | out_dir = "$target_gen_dir/tsc" |
| 220 | in_files = [ |
| 221 | "my_webui.ts", |
| 222 | "my_style_module.css.ts", |
| 223 | "my_web_component.html.ts", |
| 224 | "my_web_component.ts", |
| 225 | ] |
| 226 | # List other ts_library targets for libraries the UI needs here |
| 227 | deps = [ |
| 228 | "//third_party/polymer/v3_0:library", |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 229 | "//ui/webui/resources/js:build_ts", |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 230 | ] |
| 231 | definitions = [ |
| 232 | "//tools/typescript/definitions/chrome_send.d.ts", |
| 233 | ] |
| 234 | extra_deps = [ |
| 235 | ":copy_file", |
| 236 | ":preprocess_src", |
| 237 | ":preprocess_generated", |
| 238 | ] |
| 239 | } |
| 240 | ``` |
| 241 | |
Cole Horvitz | 253c079a | 2023-04-28 00:34:42 | [diff] [blame] | 242 | ### **bundle_js** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 243 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 244 | This rule is used to bundle larger user-facing WebUIs for improved performance. |
| 245 | It is generally not needed for debug UIs or UIs that have very few files to |
| 246 | import. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 247 | |
| 248 | #### **Arguments** |
| 249 | ``` |
| 250 | host: The WebUI host. If specified without a scheme, the assumed root location |
| 251 | will be "chrome://<host>". If specified with a scheme (e.g. |
| 252 | "chrome-extension://aaaaaaaaa") the full <host> argument will be the root. |
| 253 | input: The location of the input files to be bundled. |
| 254 | js_module_in_files: The names of the root files to bundle. These files should |
| 255 | import all other dependencies (directly or indirectly). |
| 256 | These should be specified with respect to |input|. |
Rebekah Potter | 018fa87 | 2023-01-28 00:54:28 | [diff] [blame] | 257 | 1 or 2 input files are supported. Each will result in one |
| 258 | output file named like the input with a new "rollup" suffix, |
| 259 | e.g. "foo/main.js" --> "foo/main.rollup.js". If 2 inputs are |
| 260 | specified, a third shared bundle file will also be created. |
| 261 | The shared bundle will be named "shared.rollup.js" and |
| 262 | located at the same relative path as the inputs. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 263 | out_manifest: File location to write the manifest of all output files created |
Cole Horvitz | 253c079a | 2023-04-28 00:34:42 | [diff] [blame] | 264 | by bundle_js(). Useful for generating grds. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 265 | deps: Targets generating any files being bundled. Note that this should include |
| 266 | targets generating shared resources that are expected to be bundled in |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 267 | the UI, e.g. //ui/webui/resources/js:build_ts. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 268 | 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] | 269 | that would otherwise be in many bundles, and for cr.js which relies |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 270 | on global variables. |
Rebekah Potter | 06eee9c | 2023-02-22 03:29:39 | [diff] [blame] | 271 | external_paths: Mappings between absolute URLs and paths where files imported |
| 272 | from these URLs can be found. Used for files that are not in |
| 273 | |input|. For example, the following external_path is |
| 274 | automatically added for all targets: |
| 275 | "chrome://resources/|$resources_path" where resources_path is |
| 276 | the path to where ts_library()s within ui/webui/resources place |
| 277 | their output (e.g. gen/ui/webui/resources/tsc). |
| 278 | Note: all absolute URLs must either be listed in |excludes| or |
| 279 | be mapped in |external_paths|, otherwise a build time error is |
| 280 | raised. |
Cole Horvitz | 8ca0146 | 2023-05-22 18:34:11 | [diff] [blame] | 281 | out_folder: The location where bundled files will be placed in. Defaults to |
| 282 | |target_gen_dir|. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 283 | ``` |
| 284 | |
| 285 | #### **Example** |
| 286 | ``` |
Cole Horvitz | 253c079a | 2023-04-28 00:34:42 | [diff] [blame] | 287 | import("//ui/webui/resources/tools/bundle_js.gni") |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 288 | import ("//chrome/common/features.gni") |
| 289 | |
| 290 | # optimize_webui should generally only be called when the optimize_webui |
| 291 | # feature flag is enabled. |
| 292 | if (optimize_webui) { |
Cole Horvitz | 253c079a | 2023-04-28 00:34:42 | [diff] [blame] | 293 | bundle_js("build") { |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 294 | host = "mywebui" |
Rebekah Potter | 018fa87 | 2023-01-28 00:54:28 | [diff] [blame] | 295 | js_module_in_files = [ "my_webui.js" ] # will output my_webui.rollup.js |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 296 | input = rebase_path("$target_gen_dir/tsc", root_build_dir) |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 297 | out_manifest = "$target_gen_dir/build_manifest.json" |
| 298 | # Assumes the JS files were generated by a ts_library target called |
| 299 | # build_ts. |
| 300 | deps = [ |
| 301 | ":build_ts", |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 302 | "//ui/webui/resources/js:build_ts", |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 303 | ] |
| 304 | excludes = [ |
Rebekah Potter | 952290e | 2022-11-18 09:07:28 | [diff] [blame] | 305 | "chrome://resources/js/cr.js", |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 306 | "chrome://resources/mojo/mojo/public/js/bindings.js", |
| 307 | ] |
| 308 | } |
| 309 | } |
| 310 | ``` |
| 311 | |
Cole Horvitz | 251fd88 | 2023-04-14 18:26:59 | [diff] [blame] | 312 | ### **minify_js** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 313 | |
Cole Horvitz | 251fd88 | 2023-04-14 18:26:59 | [diff] [blame] | 314 | This rule is used to minify Javascript files to reduce build size. |
Cole Horvitz | e368f0d | 2023-05-24 23:56:13 | [diff] [blame] | 315 | This can be used alongside bundle_js(), if bundling and minifying is |
| 316 | desired. |
Cole Horvitz | 251fd88 | 2023-04-14 18:26:59 | [diff] [blame] | 317 | |
| 318 | #### **Arguments** |
| 319 | ``` |
| 320 | in_folder: The location of the input files to be minified. |
| 321 | in_files: The list of JS files to minify with respect to the |in_folder|. |
| 322 | out_folder: The location where minified files will be outputted. |
Cole Horvitz | e368f0d | 2023-05-24 23:56:13 | [diff] [blame] | 323 | out_manifest: The location to write the manifest file of all files |
| 324 | outputted by minify_js(). Defaults to |
| 325 | $target_gen_dir/${target_name}_manifest.json. |
Cole Horvitz | 251fd88 | 2023-04-14 18:26:59 | [diff] [blame] | 326 | deps: Targets generating any files being minified. |
| 327 | ``` |
| 328 | |
| 329 | #### **Example** |
| 330 | ``` |
| 331 | import("//ui/webui/resources/tools/minify_js.gni") |
| 332 | import ("//chrome/common/features.gni") |
| 333 | |
| 334 | # minify_js should generally only be called when the optimize_webui |
| 335 | # GN flag is enabled. |
| 336 | if (optimize_webui) { |
| 337 | minify_js("build") { |
| 338 | in_files = [ "my_webui.js" ] |
| 339 | in_folder = "$target_gen_dir/tsc" |
Cole Horvitz | 546af9f | 2023-04-18 23:24:54 | [diff] [blame] | 340 | out_folder = "$target_gen_dir/minified" |
Cole Horvitz | 251fd88 | 2023-04-14 18:26:59 | [diff] [blame] | 341 | # Assumes the JS files were generated by a ts_library target called |
| 342 | # build_ts. |
| 343 | deps = [ ":build_ts" ] |
| 344 | } |
| 345 | } |
| 346 | ``` |
| 347 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 348 | ### **generate_grd** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 349 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 350 | This rule is used to list the WebUI resources that need to be served at runtime |
| 351 | in a grd file. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 352 | |
| 353 | #### **Arguments** |
| 354 | ``` |
| 355 | input_files: Input file paths to list in the grd file. |
| 356 | input_files_base_dir: Directory in which |input_files| can be found. |
| 357 | deps: Lists any targets responsible for generating |input_files|, |
| 358 | |grdp files|, or |manifest_files|. |
| 359 | manifest_files: List of manifest files listing additional files that should be |
| 360 | added to the grd. |
| 361 | grdp_files: List of .grdp files that should be included in the grd. Generally |
dpapad | 973b3698 | 2023-03-15 23:20:55 | [diff] [blame] | 362 | such files are also created with generate_grd(). This option can |
| 363 | only be used if a .grd file is produced, since .grdp files can't be |
| 364 | nested. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 365 | grd_prefix: The prefix to use for the grd resource IDs. Resources will be named |
| 366 | with the following pattern: IDR_GRD_PREFIX_INPUT_FILE_PATH |
dpapad | 973b3698 | 2023-03-15 23:20:55 | [diff] [blame] | 367 | 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] | 368 | resource_path_prefix: Optional prefix to add to every path in input_files (after |
| 369 | the path has been processed by |resource_path_rewrites|). |
| 370 | Must not end in a `/` as it will automatically be added |
| 371 | when joining to each path. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 372 | resource_path_rewrites: Paths to rewrite. In general, the path in input_files, |
| 373 | or the path listed in a manifest, will be used as the |
| 374 | resource path, i.e. "foo/bar.js" will have that path |
| 375 | relative to the root "chrome://mywebui/" at runtime. |
| 376 | By specifying |
| 377 | resource_path_rewrites = [ "foo/bar.js|foo_bar.js" ], |
| 378 | the path will instead be "foo_bar.js". Usually only |
| 379 | needed in somewhat complicated cases. |
| 380 | ``` |
| 381 | |
| 382 | #### **Example** |
| 383 | ``` |
| 384 | import("//ui/webui/resources/tools/generate_grd.gni") |
| 385 | |
| 386 | generate_grd("build_grd") { |
| 387 | input_files = [ "my_webui.html" ] |
| 388 | input_files_base_dir = rebase_path(".", "//") |
| 389 | deps = [ ":build_ts" ] |
Cole Horvitz | 8ecd16f | 2023-06-01 00:47:22 | [diff] [blame] | 390 | manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ]) |
Luciano Pacheco | c3c3504 | 2022-07-01 09:33:39 | [diff] [blame] | 391 | # Or, configure statically the manifest file name: |
| 392 | # manifest_files = [ "$target_gen_dir/build_ts.manifest" ] |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 393 | grd_prefix = "my_webui" |
| 394 | out_grd = "$target_gen_dir/${grd_prefix}_resources.grd" |
| 395 | } |
| 396 | ``` |
| 397 | |
| 398 | ### **grit** |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 399 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 400 | This rule is used to create a pak file from the grd file that contains all the |
| 401 | listed resources, so that they can be included in the binary and served at |
| 402 | runtime. Without creating a grit rule (and hooking up the target to the build), |
| 403 | the WebUI will not load since the resources will not exist. This rule also |
| 404 | generates C++ header files that are used to add the files to a specific |
| 405 | WebUIController. |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 406 | |
| 407 | #### **Arguments** |
| 408 | ``` |
| 409 | defines: The defines that grit should use when making the pak file. Generally |
| 410 | should include chrome_grit_defines. |
| 411 | enable_input_discovery_for_gn_analyze: If using generated grd files, this needs |
| 412 | to be set to false. |
| 413 | source: The source grd file. |
| 414 | outputs: List of files to output. Should always include the name of the pak |
| 415 | file, and should generally also list the C++ header file and C++ header |
| 416 | and .cc files for the resources map. |
| 417 | output_dir: Location to put the pak file. Often "$root_gen_dir/chrome". |
| 418 | deps: Names of any targets generating the grd file, and names of any targets |
| 419 | generating the files to be packed, if they are not already dependencies |
| 420 | of the target generating the grd file (as is often, but not always, the |
| 421 | case). |
| 422 | ``` |
| 423 | |
| 424 | #### **Example** |
| 425 | ``` |
| 426 | import("//tools/grit/grit_rule.gni") |
| 427 | |
| 428 | grit("resources") { |
| 429 | defines = chrome_grit_defines |
| 430 | |
| 431 | enable_input_discovery_for_gn_analyze = false |
| 432 | source = "$target_gen_dir/resources.grd" |
| 433 | deps = [ ":build_grd" ] |
| 434 | |
| 435 | outputs = [ |
| 436 | "grit/my_webui_resources.h", |
| 437 | "grit/my_webui_resources_map.cc", |
| 438 | "grit/my_webui_resources_map.h", |
| 439 | "my_webui_resources.pak", |
| 440 | ] |
| 441 | output_dir = "$root_gen_dir/chrome" |
| 442 | } |
| 443 | ``` |
| 444 | |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 445 | ### **build_webui** |
| 446 | |
| 447 | <!-- TODO(crbug.com/1340376): Elevate build_webui() to the top of this document |
| 448 | after it has been deployed to a few places. --> |
| 449 | |
| 450 | See the [go/build-webui-pipeline](http://go/build-webui-pipeline) design doc for |
| 451 | more info (internal only). |
| 452 | |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 453 | The flow diagram below shows a high level view of how a typical modern |
| 454 | user-facing WebUI is being built. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 455 | |
| 456 |  |
| 457 | |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 458 | This umbrella rule captures the most common WebUI build pipeline configuration |
| 459 | and aims to hide the complexity of having to define all previously described |
| 460 | rules directly. Using build_webui() is the recommended approach for most modern |
| 461 | user-facing WebUIs that use WebComponents+TypeScript+Mojo. |
| 462 | |
| 463 | The parameters passed to build_webui() are forwarded as needed to the other GN |
| 464 | rules described earlier. |
| 465 | |
| 466 | Under the cover, build_webui() defines the following targets |
| 467 | |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 468 | * preprocess_if_expr("preprocess_ts_files") |
| 469 | * preprocess_if_expr("preprocess_html_css_files") |
| 470 | * create_js_source_maps("create_source_maps") |
| 471 | * html_to_wrapper("html_wrapper_files") |
| 472 | * css_to_wrapper("css_wrapper_files") |
| 473 | * copy("copy_mojo") |
| 474 | * ts_library("build_ts") |
| 475 | * merge_js_source_maps("merge_source_maps") |
| 476 | * bundle_js("build_bundle") |
| 477 | * minify_js("build_min_js") |
| 478 | * generate_grd("build_grd") |
| 479 | * generate_grd("build_grdp") |
| 480 | * grit("resources") |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 481 | |
| 482 | Some targets are only conditionally defined based on build_webui() input |
| 483 | parameters. |
| 484 | |
dpapad | f7a2c7a | 2023-03-16 20:26:28 | [diff] [blame] | 485 | Only ":build_ts", ":resources" and ":build_grdp" targets are public and can be |
| 486 | referred to from other parts of the build. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 487 | |
| 488 | #### **Arguments** |
| 489 | ``` |
| 490 | |
| 491 | List of files params: |
dpapad | 39cac96 | 2023-01-11 22:52:52 | [diff] [blame] | 492 | static_files: Optional parameter. List of |
dpapad | 57fa789 | 2022-08-03 03:45:15 | [diff] [blame] | 493 | 1) non Web Component HTML/CSS files (don't confuse with |
| 494 | |css_files| below). These are passed to preprocess_if_expr() |
| 495 | 2) JPG/PNG/SVG files. These are included in the build verbatim |
| 496 | without any preprocessing. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 497 | |
| 498 | web_component_files: List of TS files that hold Web Component definitions with |
| 499 | equivalent HTML template files. These can be either native |
| 500 | or Polymer Web Components. Optional parameter. |
| 501 | |
| 502 | non_web_component_files: List of TS files that are not Web Components, or |
| 503 | Web Component files that don't have a corresponding |
| 504 | HTML template (rare case). Optional parameter. |
| 505 | |
| 506 | icons_html_files: List of HTML files that hold Polymer iron-iconset-svg |
| 507 | instances. Optional parameter. |
| 508 | |
| 509 | css_files: List of CSS files that hold Polymer style modules, or CSS variable |
| 510 | definitions. These are passed css_to_wrapper(). Optional parameter. |
| 511 | |
| 512 | mojo_files: List of Mojo JS generated files. These will be copied to a temporary |
| 513 | location so that they can be passed to ts_library() along with other |
| 514 | files. Optional parameter. |
| 515 | |
| 516 | mojo_files_deps: List of Mojo targets that generate |mojo_files|. Must be |
| 517 | defined if |mojo_files| is defined. |
| 518 | |
dpapad | bbee30ae | 2023-05-20 00:37:40 | [diff] [blame] | 519 | mojo_base_path: Specifies the directory under which Mojo files will be served at |
| 520 | runtime. Optional parameter. Defaults to the top level folder |
| 521 | '.', which results in Mojo files being served from |
| 522 | 'chrome://<webui_name>/foo.mojom-webui.js'. |
| 523 | Example: Passing 'mojom-webui' would result in Mojo files being |
| 524 | served from |
| 525 | 'chrome://<webui_name>/mojom-webui/foo.mojom-webui.js'. |
| 526 | |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 527 | TypeScript (ts_library()) related params: |
| 528 | ts_composite: See |composite| in ts_library(). Defaults to false, optional. |
dpapad | f7a2c7a | 2023-03-16 20:26:28 | [diff] [blame] | 529 | ts_out_dir: See |out_dir| in ts_library(). Optional parameter, defaults |
| 530 | '$target_gen_dir/tsc' |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 531 | ts_definitions: See |definitions| in ts_library(). Optional parameter. |
| 532 | ts_deps: See |deps| in ts_library(). Optional parameter. |
Yuheng Huang | 2693a49e | 2022-07-26 15:20:03 | [diff] [blame] | 533 | ts_extra_deps: See |extra_deps| in ts_library(). Optional parameter. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 534 | ts_path_mappings: See |path_mappings| in ts_library(). Optional parameter. |
Rebekah Potter | 03240f5 | 2023-03-01 22:50:30 | [diff] [blame] | 535 | ts_tsconfig_base: The tsconfig file to use for ts_library(). Optional, defaults |
| 536 | to "//tools/typescript/tsconfig_base_polymer.json" for Polymer |
| 537 | UIs (i.e. UIs that specify |web_component_files| and/or |
| 538 | |icons_html_files| and do not set |html_to_wrapper_template| |
| 539 | to "native"). Defaults to |
| 540 | "//tools/typescript/tsconfig_base.json" for non-Polymer UIs. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 541 | |
dpapad | 391a328 | 2022-07-19 17:38:32 | [diff] [blame] | 542 | HTML/CSS/JS optimization related params: |
Cole Horvitz | 546af9f | 2023-04-18 23:24:54 | [diff] [blame] | 543 | optimize: Specifies whether any optimization steps will be used. Defaults to the |
| 544 | value of the optimize_webui GN flag. |
| 545 | When true, html_to_wrapper() and css_to_wrapper() will be invoked with |
| 546 | the |minify| flag on, to minify HTML/CSS code. |
Cole Horvitz | 39b3c96d | 2023-05-02 23:32:22 | [diff] [blame] | 547 | When true, minify_js() will be invoked to minify JS code (using Terser). |
| 548 | If |optimize_webui_in_files| is provided then bundle_js() will also be |
| 549 | invoked to bundle JS code (using Rollup). |
dpapad | f7a2c7a | 2023-03-16 20:26:28 | [diff] [blame] | 550 | |optimize_webui_host| must be specified if |optimize_webui_in_files| |
| 551 | is provided. |
Cole Horvitz | 39b3c96d | 2023-05-02 23:32:22 | [diff] [blame] | 552 | optimize_webui_excludes: See |excludes| in bundle_js(). Optional. |
dpapad | f79e4c0e | 2023-04-13 22:35:16 | [diff] [blame] | 553 | optimize_webui_external_paths: See |external_paths| in optimize_webui(). |
| 554 | Optional. |
Cole Horvitz | 39b3c96d | 2023-05-02 23:32:22 | [diff] [blame] | 555 | optimize_webui_host: See |host| in bundle_js(). |
| 556 | optimize_webui_in_files: See |in_files| in bundle_js(). |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 557 | |
| 558 | Other params: |
dpapad | f7a2c7a | 2023-03-16 20:26:28 | [diff] [blame] | 559 | generate_grdp: Whether to generate grdp file instead of a grd file. Defaults to |
| 560 | false. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 561 | grd_prefix: See |grd_prefix| in generate_grd(). Required parameter. |
dpapad | 39cac96 | 2023-01-11 22:52:52 | [diff] [blame] | 562 | grd_resource_path_prefix: See |resource_path_prefix| in generate_grd(). Optional |
| 563 | parameter. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 564 | html_to_wrapper_template: See |template| in html_to_wrapper(). |
dpapad | 27f4ec8 | 2023-03-24 16:34:38 | [diff] [blame] | 565 | html_to_wrapper_scheme: See |scheme| in html_to_wrapper(). |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 566 | extra_grdp_deps: List of external generate_grd() targets that generate .grdp |
| 567 | files. These will be included in the final generated |
| 568 | resources.grd file. Optional parameter. |
| 569 | extra_grdp_files: Output .grdp files of external generate_grd() targets. Must be |
| 570 | defined if |extra_grdp_deps| is defined. |
dpapad | 838ef53 | 2022-09-09 08:41:53 | [diff] [blame] | 571 | grit_output_dir: See |output_dir| in grit(). Optional parameter, defaults to |
| 572 | "$root_gen_dir/chrome" |
dpapad | c854fcf9 | 2023-02-17 23:03:32 | [diff] [blame] | 573 | enable_source_maps: Defaults to "false". Incompatible with |optimize=true|. |
| 574 | Setting it to "true" turns on source map generation for a |
| 575 | few underlying targets. See ts_library()'s |
| 576 | |enable_source_maps| for more details. |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 577 | ``` |
| 578 | |
| 579 | #### **Example** |
| 580 | ``` |
Antonio Gomes | 282f5c4f9 | 2023-02-15 13:36:21 | [diff] [blame] | 581 | import("//ui/webui/resources/tools/build_webui.gni") |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 582 | |
| 583 | build_webui("build") { |
| 584 | grd_prefix = "dummy-webui" |
| 585 | |
| 586 | static_files = [ |
| 587 | "index.html", |
| 588 | "index.css", |
| 589 | ] |
| 590 | |
| 591 | # Files holding a CustomElement element definition AND have an equivalent |
| 592 | # .html template file. |
| 593 | web_component_files = [ |
| 594 | "app.ts", |
| 595 | "bar_view.ts", |
| 596 | "foo_view.ts", |
| 597 | ] |
| 598 | |
| 599 | # Files not holding a CustomElement element definition, or the CustomElement |
| 600 | # does not have a corresponding HTML template. |
| 601 | non_web_component_files = [ |
| 602 | "app_proxy.ts", |
| 603 | "bar_proxy.ts", |
| 604 | "foo_proxy.ts", |
| 605 | ] |
| 606 | |
| 607 | # Files that are passed as input to css_to_wrapper(). |
| 608 | css_files = [ |
| 609 | "shared_style.css", |
| 610 | "shared_vars.css", |
| 611 | ] |
| 612 | |
| 613 | ts_definitions = [ |
| 614 | "//tools/typescript/definitions/chrome_send.d.ts", |
| 615 | "//tools/typescript/definitions/metrics_private.d.ts", |
| 616 | ] |
| 617 | |
| 618 | ts_deps = [ |
| 619 | "//third_party/polymer/v3_0:library", |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 620 | "//ui/webui/resources/cr_elements:build_ts", |
| 621 | "//ui/webui/resources/js:build_ts", |
dpapad | 689e827 | 2022-07-12 23:59:41 | [diff] [blame] | 622 | ] |
| 623 | } |
| 624 | |
| 625 | ``` |
| 626 | |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 627 | ### **build_webui_tests** |
| 628 | |
| 629 | The flow diagram below shows a high level view of how a typical modern user-facing |
| 630 | WebUI's tests are built. |
| 631 | |
| 632 |  |
| 633 | |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 634 | This umbrella rule captures the most common WebUI test build pipeline |
| 635 | configuration and aims to hide the complexity of having to define multiple other |
| 636 | targets directly. Using build_webui_tests() is the recommended approach for most |
| 637 | modern user-facing WebUIs tests that reside under |
| 638 | chrome/test/data/webui/<webui_name>/ and are served from |
| 639 | chrome://webui-test/<webui_name>/ |
| 640 | |
| 641 | Under the cover, build_webui_tests() defines the following targets |
| 642 | |
Mike Frysinger | fa3a0c9 | 2023-06-08 02:33:01 | [diff] [blame] | 643 | * preprocess_if_expr("preprocess") |
| 644 | * ts_library("build_ts") |
| 645 | * generate_grd("build_grdp") |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 646 | |
| 647 | The parameters passed to build_webui_tests() are forwarded as needed to |
| 648 | the targets above. Only the ":build_grdp" target is public and can be referred |
| 649 | to from other parts of the build. |
| 650 | |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 651 | #### **Arguments** |
| 652 | ``` |
| 653 | |
| 654 | List of files params: |
| 655 | files: Required parameter. List of all test related files. |
| 656 | |
| 657 | TypeScript (ts_library()) related params: |
dpapad | 1de7e97 | 2023-03-01 02:49:48 | [diff] [blame] | 658 | ts_tsconfig_base: See |tsconfig_base| in ts_library(). Optional parameter. If |
| 659 | not provided the default configuration at |
| 660 | '//chrome/test/data/webui/tsconfig_base.json' is used. |
dpapad | 8ab521b | 2022-12-15 06:51:06 | [diff] [blame] | 661 | ts_definitions: See |definitions| in ts_library(). Optional parameter. |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 662 | ts_deps: See |deps| in ts_library(). Required parameter. |
dpapad | 8ab521b | 2022-12-15 06:51:06 | [diff] [blame] | 663 | ts_path_mappings: See |path_mappings| in ts_library(). Optional parameter. |
dpapad | c2db8ac | 2022-12-14 01:36:40 | [diff] [blame] | 664 | |
| 665 | Other params: |
| 666 | resource_path_prefix: See |resource_path_prefix| in generate_grd(). Required |
| 667 | parameter. |
| 668 | ``` |
| 669 | |
| 670 | #### **Example** |
| 671 | ``` |
| 672 | import("//chrome/test/data/webui/settings/tools/build_webui_tests.gni") |
| 673 | |
| 674 | build_webui_tests("build") { |
| 675 | resource_path_prefix = "settings" |
| 676 | |
| 677 | files = [ |
| 678 | "checkbox_tests.ts", |
| 679 | "collapse_radio_button_tests.ts", |
| 680 | "controlled_button_tests.ts", |
| 681 | "controlled_radio_button_tests.ts", |
| 682 | "dropdown_menu_tests.ts", |
| 683 | ] |
| 684 | |
| 685 | ts_path_mappings = |
| 686 | [ "chrome://settings/*|" + |
| 687 | rebase_path("$root_gen_dir/chrome/browser/resources/settings/tsc/*", |
| 688 | target_gen_dir) ] |
| 689 | |
| 690 | ts_definitions = [ |
| 691 | "//tools/typescript/definitions/chrome_send.d.ts", |
| 692 | "//tools/typescript/definitions/settings_private.d.ts", |
| 693 | ] |
| 694 | |
| 695 | ts_deps = [ |
| 696 | "//chrome/browser/resources/settings:build_ts", |
| 697 | ] |
| 698 | } |
| 699 | |
| 700 | ``` |
| 701 | |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 702 | ## Example build configurations |
| 703 | |
| 704 | ### **Simple UI with no web components** |
| 705 | |
| 706 | ``` |
| 707 | This UI has only a single checked in TypeScript file, my_debug_page.ts, and a |
| 708 | checked in HTML file my_debug_page_index.html that imports the compiled JS |
| 709 | file using a script tag with `src="my_debug_page.js"` Neither of these files |
| 710 | use any `<if expr>`. |
| 711 | ``` |
| 712 | |
| 713 | #### **BUILD.gn file** |
| 714 | ``` |
| 715 | import("//chrome/common/features.gni") |
| 716 | import("//tools/grit/grit_rule.gni") |
| 717 | import("//tools/typescript/ts_library.gni") |
| 718 | import("//ui/webui/resources/tools/generate_grd.gni") |
| 719 | |
| 720 | # This UI has only a single TypeScript input file that does not define a Web |
| 721 | # Component. TS compiler will write out "$target_gen_dir/tsc/my_debug_page.js". |
| 722 | ts_library("build_ts") { |
| 723 | root_dir = "." |
| 724 | out_dir = "$target_gen_dir/tsc" |
| 725 | in_files = [ "my_debug_page.ts" ] |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 726 | deps = [ "//ui/webui/resources/js:build_ts" ] |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | # 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] | 730 | # 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] | 731 | # ts_library(). Pass the HTML file via |input_files|. |
| 732 | generate_grd("build_grd") { |
| 733 | deps = [ ":build_ts" ] |
| 734 | grd_prefix= "my_debug_page" |
| 735 | out_grd = "$target_gen_dir/resources.grd" |
| 736 | input_files = [ "my_debug_page_index.html" ] |
| 737 | input_files_base_dir = rebase_path(".", "//") |
Cole Horvitz | 8ecd16f | 2023-06-01 00:47:22 | [diff] [blame] | 738 | manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ]) |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | # Create the pak, header, and resource map files. |
| 742 | grit("resources") { |
| 743 | defines = chrome_grit_defines |
| 744 | |
| 745 | # This arguments is needed since the grd is generated at build time. |
| 746 | enable_input_discovery_for_gn_analyze = false |
| 747 | source = "$target_gen_dir/resources.grd" |
| 748 | deps = [ ":build_grd" ] |
| 749 | outputs = [ |
| 750 | "grit/my_debug_page_resources.h", |
| 751 | "grit/my_debug_page_resources_map.cc", |
| 752 | "grit/my_debug_page_resources_map.h", |
| 753 | "my_debug_page_resources.pak", |
| 754 | ] |
| 755 | output_dir = "$root_gen_dir/chrome" |
| 756 | } |
| 757 | ``` |
| 758 | |
| 759 | ### **Non-Polymer UI with Web Components** |
| 760 | ``` |
| 761 | This UI has a top level TypeScript file that imports a single Web Component, |
| 762 | my_debug_app.ts. It has a corresponding HTML file for the app and a HTML file |
| 763 | for the top level index. None of these files use any `<if expr>`. |
| 764 | ``` |
| 765 | |
| 766 | #### **BUILD.gn file** |
| 767 | ``` |
| 768 | import("//chrome/common/features.gni") |
| 769 | import("//tools/grit/grit_rule.gni") |
| 770 | import("//tools/typescript/ts_library.gni") |
| 771 | import("//ui/webui/resources/tools/generate_grd.gni") |
| 772 | import("//tools/polymer/html_to_wrapper.gni") |
| 773 | |
| 774 | # Generate the wrapper file. This UI isn't using Polymer, so it needs to set |
| 775 | # the |template| argument. |
| 776 | html_to_wrapper("html_wrapper_files") { |
| 777 | in_files = [ "my_debug_app.html" ] |
| 778 | template = "native" |
| 779 | } |
| 780 | |
| 781 | # Copy the checked-in files to the same directory. This is needed so that the |
| 782 | # TypeScript compiler can find all the files in the expected location. |
| 783 | copy("copy_files") { |
| 784 | sources = [ |
| 785 | "my_debug_app.ts", |
| 786 | "my_debug_page.ts", |
| 787 | ] |
| 788 | outputs = [ "$target_gen_dir/{{source_file_part}}" ] |
| 789 | } |
| 790 | |
| 791 | # This UI has 3 TypeScript input files. One is the generated |
| 792 | # my_debug_app.html.ts, two are checked in: my_debug_page.ts and |
| 793 | # my_debug_app.ts. TS compiler will write out: |
| 794 | # "$target_gen_dir/tsc/my_debug_app.html.js", |
| 795 | # "$target_gen_dir/tsc/my_debug_app.js", and |
| 796 | # "$target_gen_dir/tsc/my_debug_page.js". |
| 797 | ts_library("build_ts") { |
| 798 | root_dir = target_gen_dir |
| 799 | out_dir = "$target_gen_dir/tsc" |
| 800 | in_files = [ |
| 801 | "my_debug_app.ts", |
| 802 | "my_debug_app.html.ts", |
| 803 | "my_debug_page.ts", |
| 804 | ] |
dpapad | f0e34969 | 2023-01-31 01:14:21 | [diff] [blame] | 805 | deps = [ "//ui/webui/resources/js:build_ts" ] |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 806 | extra_deps = [ |
| 807 | ":copy_files", |
| 808 | ":html_wrapper_files", |
| 809 | ] |
| 810 | } |
| 811 | |
| 812 | # The HTML file and the JS files created by ts_library need to be listed in |
Luciano Pacheco | c3c3504 | 2022-07-01 09:33:39 | [diff] [blame] | 813 | # 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] | 814 | # ts_library(). Pass the HTML file via |input_files|. |
| 815 | generate_grd("build_grd") { |
| 816 | deps = [ ":build_ts" ] |
| 817 | grd_prefix= "my_debug_page" |
| 818 | out_grd = "$target_gen_dir/resources.grd" |
| 819 | input_files = [ "my_debug_page_index.html" ] |
| 820 | input_files_base_dir = rebase_path(".", "//") |
Cole Horvitz | 8ecd16f | 2023-06-01 00:47:22 | [diff] [blame] | 821 | manifest_files = filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ]) |
Rebekah Potter | d38c2c4 | 2022-06-28 21:21:22 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | # Create the pak, header, and resource map files. |
| 825 | grit("resources") { |
| 826 | defines = chrome_grit_defines |
| 827 | |
| 828 | # This arguments is needed since the grd is generated at build time. |
| 829 | enable_input_discovery_for_gn_analyze = false |
| 830 | source = "$target_gen_dir/resources.grd" |
| 831 | deps = [ ":build_grd" ] |
| 832 | outputs = [ |
| 833 | "grit/my_debug_page_resources.h", |
| 834 | "grit/my_debug_page_resources_map.cc", |
| 835 | "grit/my_debug_page_resources_map.h", |
| 836 | "my_debug_page_resources.pak", |
| 837 | ] |
| 838 | output_dir = "$root_gen_dir/chrome" |
| 839 | } |
| 840 | ``` |