Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit f627706

Browse files
authored
docs(cookbook-aot-compiler): improve Ahead-of-Time compilation cookbook (#2798)
closes #2790
1 parent 75464d5 commit f627706

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// #docregion
2-
// main entry point
32
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
3+
44
import { AppModule } from './app.module';
55

66
platformBrowserDynamic().bootstrapModule(AppModule);

public/docs/ts/latest/cookbook/aot-compiler.jade

+38-13
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,11 @@ a#toh
341341

342342
Fortunately, the source code can be compiled either way without change _if_ you account for a few key differences.
343343

344-
***Index.html***
344+
***index.html***
345345

346-
The JiT and AoT apps are setup and launched so differently that they require their own `index.html` files.
346+
The JiT and AoT apps require their own `index.html` files because they setup and launch so differently.
347+
**Put the AoT version in the `/aot` folder** because two `index.html` files can't be in the same folder.
348+
347349
Here they are for comparison:
348350

349351
+makeTabs(
@@ -355,35 +357,47 @@ a#toh
355357
)
356358

357359
:marked
358-
They can't be in the same folder.
359-
***Put the AoT version in the `/aot` folder***.
360-
361360
The JiT version relies on `SystemJS` to load individual modules and requires the `reflect-metadata` shim.
362361
Both scripts appear in its `index.html`.
363362

364363
The AoT version loads the entire application in a single script, `aot/dist/build.js`.
365364
It does not need `SystemJS` or the `reflect-metadata` shim; those scripts are absent from its `index.html`
365+
366+
***main.ts***
367+
368+
JiT and AoT applications boot in much the same way but require different Angular libraries to do so.
369+
The key differences, covered in the [Bootstrap](#bootstrap) section above,
370+
are evident in these `main` files which can and should reside in the same folder:
371+
372+
+makeTabs(
373+
`toh-6/ts/app/main-aot.ts,
374+
toh-6/ts/app/main.ts`,
375+
null,
376+
`app/main-aot.ts (AoT),
377+
app/main.ts (JiT)`
378+
)
366379

367-
*Component-relative Template URLS*
380+
:marked
381+
***Component-relative Template URLS***
368382

369383
The AoT compiler requires that `@Component` URLS for external templates and css files be _component-relative_.
370-
That means that the value of `@Component.templateUrl` is a URL value relative to the component class file:
371-
`foo.component.html` no matter where `foo.component.ts` sits in the project folder structure.
384+
That means that the value of `@Component.templateUrl` is a URL value _relative_ to the component class file.
385+
For example, a `'hero.component.html'` URL means that the template file is a sibling of its companion `hero.component.ts` file.
372386

373387
While JiT app URLs are more flexible, stick with _component-relative_ URLs for compatibility with AoT compilation.
374388

375-
JiT-compiled apps, using the SystemJS loader and _component-relative_ URLs *must set the* `@Component.moduleId` *property to* `module.id`.
389+
JiT-compiled applications that use the SystemJS loader and _component-relative_ URLs *must set the* `@Component.moduleId` *property to* `module.id`.
376390
The `module` object is undefined when an AoT-compiled app runs.
377-
The app fails unless you assign a global `module` value in the `index.html` like this:
391+
The app fails with a null reference error unless you assign a global `module` value in the `index.html` like this:
378392
+makeExample('toh-6/ts/aot/index.html','moduleId')(format='.')
379393
.l-sub-section
380394
:marked
381395
Setting a global `module` is a temporary expedient.
382396
:marked
383-
*TypeScript configuration*
397+
***TypeScript configuration***
384398

385-
JiT-compiled apps transpile to `commonjs` modules.
386-
AoT-compiled apps transpile to _ES2015_/_ES6_ modules to facilitate Tree Shaking.
399+
JiT-compiled applications transpile to `commonjs` modules.
400+
AoT-compiled applications transpile to _ES2015_/_ES6_ modules to facilitate Tree Shaking.
387401
AoT requires its own TypeScript configuration settings as well.
388402

389403
You'll need separate TypeScript configuration files such as these:
@@ -396,6 +410,17 @@ a#toh
396410
tsconfig.json (JiT)`
397411
)
398412

413+
.callout.is-helpful
414+
header @Types and node modules
415+
:marked
416+
In the file structure of _this particular sample project_,
417+
the `node_modules` folder happens to be two levels up from the project root.
418+
Therefore, `"typeRoots"` must be set to `"../../node_modules/@types/"`.
419+
420+
In a more typical project, `node_modules` would be a sibling of `tsconfig-aot.json`
421+
and `"typeRoots"` would be set to `"node_modules/@types/"`.
422+
Edit your `tsconfig-aot.json` to fit your project's file structure.
423+
399424
:marked
400425
### Tree Shaking
401426

0 commit comments

Comments
 (0)