blob: f86571e4e427d55904588482d199deb7a84622a0 [file] [log] [blame] [view]
dbeamf02a25aa2017-01-24 01:58:381# Optimizing Chrome Web UIs
2
3## How do I do it?
4
5In order to build with a fast configuration, try setting these options in your
6GN args:
7
8```
dpapadd0ef1a92017-09-18 19:32:129optimize_webui = true
dbeamf02a25aa2017-01-24 01:58:3810is_debug = false
11```
12
dbeamf02a25aa2017-01-24 01:58:3813## How is the code optimized?
14
15### Resource combination
16
17[HTML imports](https://www.html5rocks.com/en/tutorials/webcomponents/imports/)
18are a swell technology, but can be used is slow ways. Each import may also
19contain additional imports, which must be satisfied before certain things can
20continue (i.e. script execution may be paused).
21
22```html
23<!-- If a.html contains more imports... -->
24<link rel="import" href="a.html">
25<!-- This script is blocked until done. -->
26<script> startThePageUp(); </script>
27```
28
29To reduce this latency, Chrome uses a tool created by the Polymer project named
dpapad1e6a87f52017-07-22 00:22:2530[polymer-bundler](https://github.com/Polymer/polymer-bundler). It processes
dbeamf02a25aa2017-01-24 01:58:3831a page starting from a URL entry point and inlines resources the first time
32they're encountered. This greatly decreases latency due to HTML imports.
33
34```html
35<!-- Contents of a.html and all its dependencies. -->
36<script> startThePageUp(); </script>
37```
38
39### CSS @apply to --var transformation
40
41We also use
42[polymer-css-build](https://github.com/PolymerLabs/polymer-css-build) to
43transform CSS @apply mixins (which are not yet natively supported) into faster
44--css-variables. This turns something like this:
45
46```css
47:host {
48 --mixin-name: {
49 color: red;
50 display: block;
51 };
52}
53/* In a different place */
54.red-thing {
55 @apply(--mixin-name);
56}
57```
58
59into the more performant:
60
61```css
62:host {
63 --mixin-name_-_color: red;
64 --mixin-name_-_display: block;
65}
66/* In a different place */
67.red-thing {
68 color: var(--mixin-name_-_color);
69 display: var(--mixin-name_-_display);
70}
71```
72
73### JavaScript Minification
74
75In order to minimize disk size, we run
76[uglifyjs](https://github.com/mishoo/UglifyJS2) on all combined JavaScript. This
77reduces installer and the size of resources required to load to show a UI.
78
79Code like this:
80
81```js
82function fizzBuzz() {
83 for (var i = 1; i <= 100; i++) {
84 var fizz = i % 3 == 0 ? 'fizz' : '';
85 var buzz = i % 5 == 0 ? 'buzz' : '';
86 console.log(fizz + buzz || i);
87 }
88}
89fizzBuzz();
90```
91
92would be minified to:
93
94```js
95function fizzBuzz(){for(var z=1;100>=z;z++){var f=z%3==0?"fizz":"",o=z%5==0?"buzz":"";console.log(f+o||z)}}fizzBuzz();
96```
97
98If you'd like to more easily debug minified code, click the "{}" prettify button
99in Chrome's developer tools, which will beautify the code and allow setting
100breakpoints on the un-minified version.
101
102### Gzip compression of web resources
103
dpapadb6ce7d02020-07-13 08:30:22104As of [r761031](https://chromium.googlesource.com/chromium/src/+/6b83ee683f6c545be29ee807c6d0b6ac1508a549)
105all HTML, JS, CSS and SVG resources are compressed by default with gzip
106Previously this was only happening if the `compress="gzip"` attribute was
107specified as follows in the corresponding .grd file:
dbeamf02a25aa2017-01-24 01:58:38108
109```xml
110<include name="IDR_MY_PAGE" file="my/page.html" type="BINDATA" compress="gzip" />
111```
112
dpapadb6ce7d02020-07-13 08:30:22113This is no longer necessary, and should be omitted. Only specify the `compress`
114attribute if the value is `false` or `brotli`.
dbeamf02a25aa2017-01-24 01:58:38115
dpapadb6ce7d02020-07-13 08:30:22116Compressed resources are uncompressed at a fairly low layer within
117ResourceBundle, and WebUI authors typically do not need to do anything special
118to serve those files to the UI.