Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Optimizing Chrome Web UIs | |
| 2 | |
| 3 ## How do I do it? | |
| 4 | |
| 5 In order to build with a fast configuration, try setting these options in your | |
| 6 GN args: | |
| 7 | |
| 8 ``` | |
| 9 use_vulcanize = true | |
| 10 is_debug = false | |
| 11 ``` | |
| 12 | |
| 13 If you make local changes, you likely need to re-run: | |
| 14 | |
| 15 ``` | |
| 16 $ chrome/browser/resources/vulcanize.py | |
| 17 ``` | |
| 18 | |
| 19 And rebuild Chrome to see effects. vulcanize.py will result in local changes to | |
| 20 *crisper* and *vulcanized* files that you must currently check in. | |
| 21 | |
| 22 *NOTE: This is likely to change soon to be more tightly integrated with GN / | |
|
dpapad
2017/01/23 19:20:53
Perhaps add a link to https://bugs.chromium.org/p/
Dan Beam
2017/01/24 01:50:24
Done.
| |
| 23 Chrome's build.* | |
| 24 | |
| 25 ## How is the code optimized? | |
| 26 | |
| 27 ### Resource combination | |
| 28 | |
| 29 [HTML imports](https://www.html5rocks.com/en/tutorials/webcomponents/imports/) | |
| 30 are a swell technology, but can be used is slow ways. Each import may also | |
| 31 contain additional imports, which must be satisfied before certain things can | |
| 32 continue (i.e. script execution may be paused). | |
| 33 | |
| 34 ```html | |
| 35 <!-- If a.html contains more imports... --> | |
| 36 <link rel="import" href="a.html"> | |
| 37 <!-- This script is blocked until done. --> | |
| 38 <script> startThePageUp(); </script> | |
| 39 ``` | |
| 40 | |
| 41 To reduce this latency, Chrome uses a tool created by the Polymer project named | |
| 42 [vulcanize](https://github.com/Polymer/vulcanize). It processes a page starting | |
|
dpapad
2017/01/23 19:20:53
This URL just redirects to polymer-bundler repo. P
Dan Beam
2017/01/24 01:50:24
Done.
| |
| 43 from a URL entry point and inlines resources the first time they're encountered. | |
| 44 This greatly decreases latency due to HTML imports. | |
| 45 | |
| 46 ```html | |
| 47 <!-- Contents of a.html and all its dependencies. --> | |
| 48 <script> startThePageUp(); </script> | |
| 49 ``` | |
| 50 | |
| 51 ### CSS @apply to --var transformation | |
| 52 | |
| 53 We also use | |
| 54 [polymer-css-build](https://github.com/PolymerLabs/polymer-css-build) to | |
| 55 transform CSS @apply mixins (which are not yet natively supported) into faster | |
| 56 --css-variables. This turns something like this: | |
| 57 | |
| 58 ```css | |
| 59 :host { | |
| 60 --mixin-name: { | |
| 61 color: red; | |
| 62 display: block; | |
| 63 }; | |
| 64 } | |
| 65 /* In a different place */ | |
| 66 .red-thing { | |
| 67 @apply(--mixin-name); | |
| 68 } | |
| 69 ``` | |
| 70 | |
| 71 into the more performant: | |
| 72 | |
| 73 ```css | |
| 74 :host { | |
| 75 --mixin-name_-_color: red; | |
| 76 --mixin-name_-_display: block; | |
| 77 } | |
| 78 /* In a different place */ | |
| 79 .red-thing { | |
| 80 color: var(--mixin-name_-_color); | |
| 81 display: var(--mixin-name_-_display); | |
| 82 } | |
| 83 ``` | |
| 84 | |
| 85 ### JavaScript Minification | |
| 86 | |
| 87 In order to minimize disk size, we run | |
| 88 [uglifyjs](https://github.com/mishoo/UglifyJS2) on all combined JavaScript. This | |
| 89 reduces installer and the size of resources required to load to show a UI. | |
| 90 | |
| 91 Code like this: | |
| 92 | |
| 93 ```js | |
| 94 function fizzBuzz() { | |
| 95 for (var i = 1; i <= 100; i++) { | |
| 96 var fizz = i % 3 == 0 ? 'fizz' : ''; | |
| 97 var buzz = i % 5 == 0 ? 'buzz' : ''; | |
| 98 console.log(fizz + buzz || i); | |
| 99 } | |
| 100 } | |
| 101 fizzBuzz(); | |
| 102 ``` | |
| 103 | |
| 104 would be minified to: | |
| 105 | |
| 106 ```js | |
| 107 function fizzBuzz(){for(var z=1;100>=z;z++){var f=z%3==0?"fizz":"",o=z%5==0?"buz z":"";console.log(f+o||z)}}fizzBuzz(); | |
| 108 ``` | |
| 109 | |
| 110 If you'd like to more easily debug minified code, click the "{}" prettify button | |
| 111 in Chrome's developer tools, which will beautify the code and allow setting | |
| 112 breakpoints on the un-minified version. | |
| 113 | |
| 114 ### Gzip compression of web resources | |
| 115 | |
| 116 In certain cases, it might be preferable to leave web resources compressed on | |
| 117 disk and inflate them when needed (i.e. when a user wants to see a page). | |
| 118 | |
| 119 In this case, you can run `gzip --rsyncable` on a resource before it's put into | |
| 120 a .pak file via GRIT with this syntax: | |
| 121 | |
| 122 ```xml | |
| 123 <include name="IDR_MY_PAGE" file="my/page.html" type="BINDATA" compress="gzip" / > | |
| 124 ``` | |
| 125 | |
| 126 Gzip is currently set up to apply to a whole WebUI's data source, though it's | |
| 127 possible to exclude specific paths for things like dynamically generated content | |
| 128 (i.e. many pages load translations dynamically from a path named "strings.js"). | |
| 129 | |
| 130 To mark a WebUI's resources compressed, you'll need to do something like: | |
| 131 | |
| 132 ```c++ | |
| 133 WebUIDataSource* data_source = WebUIDataSource::Create(...); | |
| 134 data_source->SetDefaultResource(IDR_MY_PAGE); | |
| 135 std::unordered_set<std::string> exclusions; | |
| 136 exclusions.insert("strings.js"); // if required | |
| 137 data_source->UseGzip(exclusions); | |
| 138 ``` | |
| OLD | NEW |