Skip to content

Commit 8f42fe4

Browse files
clydinmgechev
authored andcommitted
docs: add build system differential loading theory of operation overview
1 parent 397ef05 commit 8f42fe4

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

docs/design/build-system.md

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,46 @@ Aside from tree-shaking, scripts and styles (as defined in the sources above) al
156156

157157
## Post-processing steps
158158

159-
There are some steps that are meant to operate over existing whole applications and thus happen after the webpack compilation finishes and outputs files.
160-
161-
The fist step is differential loading, where we take code that is targeting modern browsers and produces from it a compatible version for older browsers.
162-
`index.html` is then modified in such a way that both modern and older browsers only load their corresponding scripts.
163-
This is the first step because it's also the most resource intensive one, and performing it later in the pipeline would multiply the work done.
159+
There are some steps that are meant to operate over whole applications and thus happen after the compilation finishes and outputs files.
160+
The steps are described in the order in which they are executed during a build.
161+
The execution order was determined based on the complexity of the step with a primary goal of minimizing the repetition of computationally expensive operations.
162+
163+
### Differential Loading
164+
Differential loading is a strategy that allows your web application to support multiple browsers, but only load the necessary code that the browser needs.
165+
When differential loading is enabled, the CLI generates two distinct variants of application bundles.
166+
167+
* The first contains ES2015 syntax, takes advantage of built-in support in modern browsers, ships fewer polyfills, and results in a smaller total size.
168+
* The second contains code in the older ES5 syntax, along with all necessary polyfills for Angular to function. This results in a larger total size, but supports older browsers.
169+
170+
This process as designed has the advantage that only one full compilation of the application in ES2015 syntax is required.
171+
This removes a large amount of otherwise unnecessary and duplicate processing such as module resolution and dead code elimination.
172+
It also guarantees that the application is ES5 compliant including third-party code.
173+
The two variants of application bundles are created by the following steps:
174+
1) A full build of the application is performed using an ES2015 output target.
175+
The application's global stylesheets, scripts, and assets are also processed during this step via the full build. These elements are reused for both of application variants.
176+
2) A copy of the JavaScript output application files are transformed (commonly referred to as down-leveled) to ES5 compatible syntax.
177+
ES2015+ syntax elements such as classes are converted into functionally equivalent ES5 code structures.
178+
3) An additional ES5-only polyfills file is generated that contains the required Angular polyfills for ES5-only browsers.
179+
4) A single index HTML file is created that references both application variants and is designed to only load the appropriate files for each browser.
180+
181+
To support loading the file sets in the appropriate browsers, the HTML `script` element's `type` and `nomodule` attributes are leveraged.
182+
Browsers will only load a script with a known type.
183+
The ES2015 files are referenced using a type of `module` which is only supported on browsers that support ES2015+ code.
184+
Since browsers that do not support ES2015+ code also do not support the `module` script type, these scripts are ignored for browsers that cannot parse and execute the ES2015 code.
185+
Browsers that support the `module` script type also support the `nomodule` attribute.
186+
This attribute instructs a browser that supports module scripts to ignore the script with the attribute. There is one browser exception in this case: Safari 10.1.
187+
This browser supports module scripts but does not support the `nomodule` attribute.
188+
To support this case, a special polyfill script is included to provide a workaround for the browser.
189+
This arrangement of script elements ensures that ES5-only browsers will only execute the ES5 script files and browsers that support ES2015+ will only execute the ES2015 script files.
190+
191+
### Localization (i18n)
164192

165193
The second of these post-processing steps is build-time localization.
166194
The final js bundles are processed using `@angular/localize`, replacing any locale-specific translations.
167195
This sort of localization produces one application for each locale, each in their own folders.
168196

197+
### Service Worker
198+
169199
The third and last post-processing step is the creation of a [service worker](https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API).
170200
A listing of final application files is taken, fingerprinted according to their content, and added to the service worker manifest.
171201
This must be the last step because it needs each application file to not be modified further.

0 commit comments

Comments
 (0)