@@ -341,9 +341,11 @@ a#toh
341
341
342
342
Fortunately, the source code can be compiled either way without change _if_ you account for a few key differences.
343
343
344
- ***Index .html***
344
+ ***index .html***
345
345
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
+
347
349
Here they are for comparison:
348
350
349
351
+ makeTabs(
@@ -355,35 +357,47 @@ a#toh
355
357
)
356
358
357
359
:marked
358
- They can't be in the same folder.
359
- ***Put the AoT version in the `/aot` folder***.
360
-
361
360
The JiT version relies on `SystemJS` to load individual modules and requires the `reflect-metadata` shim.
362
361
Both scripts appear in its `index.html`.
363
362
364
363
The AoT version loads the entire application in a single script, `aot/dist/build.js`.
365
364
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
+ )
366
379
367
- *Component-relative Template URLS*
380
+ :marked
381
+ ***Component-relative Template URLS***
368
382
369
383
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 .
372
386
373
387
While JiT app URLs are more flexible, stick with _component-relative_ URLs for compatibility with AoT compilation.
374
388
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`.
376
390
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:
378
392
+ makeExample('toh-6/ts/aot/index.html' ,'moduleId' )( format ='.' )
379
393
.l-sub-section
380
394
:marked
381
395
Setting a global `module` is a temporary expedient.
382
396
:marked
383
- *TypeScript configuration*
397
+ *** TypeScript configuration** *
384
398
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.
387
401
AoT requires its own TypeScript configuration settings as well.
388
402
389
403
You'll need separate TypeScript configuration files such as these:
@@ -396,6 +410,17 @@ a#toh
396
410
tsconfig.json (JiT)`
397
411
)
398
412
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
+
399
424
:marked
400
425
### Tree Shaking
401
426
0 commit comments