You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 4, 2017. It is now read-only.
Copy file name to clipboardExpand all lines: public/docs/ts/latest/cookbook/aot-compiler.jade
+129-1Lines changed: 129 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,7 @@ a#toc
15
15
* [Load the bundle](#load)
16
16
* [Serve the app](#serve)
17
17
* [Source Code](#source-code)
18
+
* [Tour of Heroes](#toh)
18
19
19
20
a#overview
20
21
.l-main-section
@@ -285,7 +286,7 @@ a#source-code
285
286
:marked
286
287
## Source Code
287
288
288
-
Here is the pertinent AoT source code for this cookbook:
289
+
Here is the pertinent AoT source code for this cookbook so far:
289
290
+makeTabs(
290
291
`cb-aot-compiler/ts/app/app.component.html,
291
292
cb-aot-compiler/ts/app/app.component.ts,
@@ -301,3 +302,130 @@ a#source-code
301
302
tsconfig-aot.json,
302
303
rollup.js`
303
304
)
305
+
306
+
a#toh
307
+
.l-main-section
308
+
:marked
309
+
## Tour of Heroes
310
+
311
+
Our sample application has so far been trivial. In this section we want to apply what we have learned to a more realistic application.
312
+
313
+
We will show how to apply AoT compilation and Tree Shaking to our <a href="/docs/ts/latest/tutorial/toh-pt6.html">Tour of Heroes</a> application.
314
+
315
+
### JiT vs AoT
316
+
317
+
We recommend using AoT in production, but the added compilation time in combination with Tree Shaking might make it impractical to do AoT during development.
318
+
319
+
Instead we have opted to do JiT compilation in our development environment and AoT compilation when deploying to production.
320
+
321
+
Our source code is for the most part agnostic of how we compile the application, but there are a few differences to keep in mind.
322
+
323
+
*Index.html*
324
+
325
+
The initial loading is different between JiT and AoT applications. In our case we have decided to use two different index.html files.
326
+
327
+
Our JiT version relies on `SystemJS` for loading of individual modules, but our AoT version loads the entire application as a single script tag without a dependency on `SystemJS`. This means we can remove the `SystemJS` script tag when doing AoT. It turns out we can also remove the dependency on `reflect-metadata`.
328
+
329
+
We have included the two index.html files below:
330
+
331
+
+makeTabs(
332
+
`toh-6/ts/index.html,
333
+
toh-6-aot/ts/index.html`,
334
+
null,
335
+
`index.html (JiT),
336
+
index.html (AoT)`
337
+
)
338
+
339
+
:marked
340
+
*External file paths*
341
+
342
+
The AoT compiler assumes component-relative paths when locating external templates and css files. This differs from JiT where the path is specified as either an absolute path from the application root, or a component-relative path in tandem with a `module.id`.
343
+
344
+
To make our components compatible with both AoT and JiT we have added `module.id` and component-relative paths to all our components.
345
+
346
+
The AoT compiler will ignore `module.id` during compilation, but there is one caveat. `module.id` is included in the final JavaScript, but in the case of AoT, `module` will not be defined at runtime. This means we will get a runtime error when trying to access `id` on the undefined `module` object.
347
+
348
+
The solution is simple. Since `module.id` has no meaning in an AoT application we can work around this by assigning `window.module` an arbitrary value at runtime.
349
+
350
+
The assigned value can be anything. The important part is to define `window.module` to avoid failure when `module.id` is referenced.
Manually setting `module` to an arbitrary value may seem a bit hacky, but we expect this to be a temporary workaround.
356
+
357
+
:marked
358
+
*TypeScript configuration*
359
+
360
+
To prepare our code for Tree Shaking our AoT compiled application is targeting `ES2015` modules. This is not ideal for our JiT configuration since most browsers can't execute `ES2015` directly.
361
+
362
+
In order to output regular `ES5` JavaScript for out JiT setup, we have created a second `tsconfig.json` file.
363
+
364
+
The two TypeScript configuration files can be seen below:
365
+
366
+
+makeTabs(
367
+
`toh-6/ts/tsconfig.json,
368
+
toh-6/ts/tsconfig-aot.json`,
369
+
null,
370
+
`tsconfig.json (JiT),
371
+
tsconfig-aot.json (AoT)`
372
+
)
373
+
374
+
:marked
375
+
### External AoT Libraries
376
+
377
+
Tour of Heroes has an external dependency on the `angular-in-memory-web-api` library.
378
+
379
+
We can't expect all libraries to be released with AoT in mind, but `angular-in-memory-web-api` has already been AoT compiled and released with the necessary dependencies to integrate it with another AoT compiled application.
380
+
381
+
The key to releasing an AoT compatible version of a library is including the `.metadata.json` files generated by the AoT compiler.
382
+
383
+
The metadata files, the JavaScript source and .d.ts typings files are enough to integrate the AoT build of `angular-in-memory-web-api` with our AoT compiled version of Tour of Heroes.
384
+
385
+
It may seem surprising to some that we're not integrating with the TypeScript source from `angular-in-memory-web-api` when doing AoT.
386
+
387
+
Releasing TypeScript with external libraries is unnecessary, but also discouraged since it forces the consumer to recreate the compilation environment with typings and everything needed to compile the third party library.
388
+
389
+
### Tree Shaking
390
+
391
+
Again we are using Rollup to do the Tree Shaking.
392
+
393
+
The Rollup configuration has not changed much from our previous example, but we have included `angular-in-memory-web-api` when calling the commonjs plugin. This is because `angular-in-memory-web-api` is released as `commonjs` and has to be converted to `ES2015` before Tree Shaking.
The JiT build can be run just like all our other JiT examples by running `npm start`.
401
+
402
+
To launch the AoT compiled version we execute the following command from the `toh-6-aot/ts` folder:
403
+
404
+
`npm run build:toh-aot && npm run lite`
405
+
406
+
:marked
407
+
### Inspect the Bundle
408
+
409
+
Now that we have a more substantial application it might be interesting to take a closer look at the generated JavaScript bundle. The code is minified, so we won't learn much from looking at the source code though.
410
+
411
+
Instead we use a handy tool called `source-map-explorer`. We install `source-map-explorer` by running `npm install source-map-explorer --save-dev`.
412
+
413
+
`source-map-explorer` uses this source map, generated with our bundle, to analyse and draw a map of all our dependencies.
From looking at the map we can know exactly which modules were included in our bundle. This includes everything from our application code to Angular framework code.
422
+
423
+
To generate the map we run the following command from the `toh-6-aot/ts` folder.
0 commit comments