14
14
* [Tree Shaking](#tree-shaking)
15
15
* [Load the bundle](#load)
16
16
* [Serve the app](#serve)
17
+ * [Workflow and convenience script](#workflow)
17
18
* [Source Code](#source-code)
18
19
* [Tour of Heroes](#toh)
19
20
@@ -104,7 +105,7 @@ a#compile
104
105
105
106
:marked
106
107
Install a few new npm dependencies with the following command:
107
- code-example( format = '.' ) .
108
+ code-example( language = "none" class = "code-shell" ) .
108
109
npm install @angular/compiler-cli @angular/platform-server --save
109
110
:marked
110
111
You will run the `ngc` compiler provided in the `@angular/compiler-cli` npm package
@@ -150,7 +151,7 @@ code-example(format='.').
150
151
### Compiling the application
151
152
152
153
Initiate AOT compilation from the command line using the previously installed `ngc` compiler by executing:
153
- code-example( format = '.' ) .
154
+ code-example( language = "none" class = "code-shell" ) .
154
155
node_modules/.bin/ngc -p tsconfig-aot.json
155
156
.l-sub-section
156
157
:marked
@@ -188,17 +189,21 @@ a#bootstrap
188
189
189
190
Instead of bootstrapping `AppModule`, you bootstrap the application with the generated module factory, `AppModuleNgFactory`.
190
191
191
- Switch from the `platformBrowserDynamic.bootstrap` used in JIT compilation to
192
- `platformBrowser().bootstrapModuleFactory` and pass in the `AppModuleNgFactory`.
192
+ Make a copy of `main.ts` and name it `main-jit.ts`.
193
+ This is the JIT version; set it aside as you may need it [later](#run-jit "Running with JIT").
194
+
195
+ Open `main.ts` and convert it to AOT compilation.
196
+ Switch from the `platformBrowserDynamic.bootstrap` used in JIT compilation to
197
+ `platformBrowser().bootstrapModuleFactory` and pass in the AOT-generated `AppModuleNgFactory`.
193
198
194
- Here is AOT bootstrap in `main.ts` next to the familiar JIT version:
199
+ Here is AOT bootstrap in `main.ts` next to the original JIT version:
195
200
196
201
+ makeTabs(
197
202
` cb-aot-compiler/ts/app/main.ts,
198
203
cb-aot-compiler/ts/app/main-jit.ts` ,
199
204
null ,
200
- ` app/main.ts (AOT) ,
201
- app/main.ts (JIT) `
205
+ ` app/main.ts,
206
+ app/main-jit .ts`
202
207
)
203
208
204
209
:marked
@@ -250,6 +255,8 @@ code-example(format='.').
250
255
:marked
251
256
It tells Rollup that the app entry point is `app/main.js` .
252
257
The `dest` attribute tells Rollup to create a bundle called `build.js` in the `dist` folder.
258
+ It overrides the default `onwarn` method in order to skip annoying messages about the AOT compiler's use of the `this` keyword.
259
+
253
260
Then there are plugins.
254
261
255
262
:marked
@@ -288,23 +295,20 @@ code-example(format='.').
288
295
Execute the Rollup process with this command:
289
296
code-example( format ='.' ) .
290
297
node_modules/.bin/rollup -c rollup-config.js
291
-
292
298
.l-sub-section
293
299
:marked
294
- Rollup may log many lines with the following warning message:
295
- code-example( format ='.' , language ='bash' ) .
296
- The `this` keyword is equivalent to `undefined` at the top level of an ES module, and has been rewritten
297
- :marked
298
- You can safely ignore these warnings.
299
-
300
+ Windows users should surround the `rollup` command in double quotes:
301
+ code-example( format ='.' ) .
302
+ "node_modules/.bin/rollup" -c rollup-config.js
303
+ :marked
300
304
a#load
301
305
.l-main-section
302
306
:marked
303
307
## Load the Bundle
304
308
305
309
Loading the generated application bundle does not require a module loader like SystemJS.
306
310
Remove the scripts that concern SystemJS.
307
- Instead, load the bundle file using a single `script` tag:
311
+ Instead, load the bundle file using a single `script` tag **_after_** the `</body>` tag :
308
312
309
313
+ makeExample('cb-aot-compiler/ts/index.html' ,'bundle' ,'index.html (load bundle)' )( format ='.' )
310
314
@@ -315,11 +319,11 @@ a#serve
315
319
316
320
You'll need a web server to host the application.
317
321
Use the same _Lite Server_ employed elsewhere in the documentation:
318
- code-example( format = '.' ) .
322
+ code-example( language = "none" class = "code-shell" ) .
319
323
npm run lite
320
324
:marked
321
325
The server starts, launches a browser, and the app should appear.
322
-
326
+
323
327
a#source-code
324
328
.l-main-section
325
329
:marked
@@ -342,6 +346,56 @@ a#source-code
342
346
rollup-config.js`
343
347
)
344
348
349
+ a#workflow
350
+ .l-main-section
351
+ :marked
352
+ ## Workflow and convenience script
353
+
354
+ You'll rebuild the AOT version of the application every time you make a change.
355
+ Those _npm_ commands are long and difficult to remember.
356
+
357
+ Add the following _npm_ convenience script to the `package.json` so you can compile and rollup in one command.
358
+ + makeJson('cb-aot-compiler/ts/package.json' , { paths: ' scripts.build:aot' }, "package.json (build:aot convenience script)" )
359
+ :marked
360
+ Open a terminal window and try it.
361
+ code-example( language ="none" class ="code-shell" ) .
362
+ npm run build:aot
363
+
364
+ a#run-jit
365
+ :marked
366
+ ### And JIT too!
367
+
368
+ AOT compilation and rollup together take several seconds.
369
+ You may be able to develop iteratively a little faster with SystemJS and JIT.
370
+ The same source code can be built both ways. Here's one way to do that.
371
+
372
+ * Make a copy of `index.html` and call it `index-jit.html`.
373
+ * Delete the script at the bottom of `index-jit.html` that loads `bundle.js`
374
+ * Restore the SystemJS scripts like this:
375
+ + makeExample('cb-aot-compiler/ts/index-jit.html' ,'jit' ,'index-jit.html (SystemJS scripts)' )( format ='.' )
376
+
377
+ :marked
378
+ Notice the slight change to the `system.import` which now specifies `app/main-jit`.
379
+ That's the JIT version of the bootstrap file that we preserved [above](#bootstrap)
380
+
381
+ :marked
382
+ Open a _different_ terminal window and enter.
383
+ code-example( language ="none" class ="code-shell" ) .
384
+ npm start
385
+ :marked
386
+ That compiles the app with JIT and launches the server.
387
+ The server loads `index.html` which is still the AOT version (confirm in the browser console).
388
+ Change the address bar to `index-jit.html` and it loads the JIT version (confirm in the browser console).
389
+
390
+ Develop as usual.
391
+ The server and TypeScript compiler are in "watch mode" so your changes are reflected immediately in the browser.
392
+
393
+ To see those changes in AOT, switch to the original terminal and re-run `npm run build:aot`.
394
+ When it finishes, go back to the browser and back-button to the AOT version in the (default) `index.html`.
395
+
396
+ Now you can develop JIT and AOT, side-by-side.
397
+
398
+
345
399
a#toh
346
400
.l-main-section
347
401
:marked
@@ -459,19 +513,18 @@ a#toh
459
513
tsconfig-aot.json` )
460
514
:marked
461
515
Extend the `scripts` section of the `package.json` with these npm scripts:
462
- code-example( format ='.' ) .
463
- "build:aot": "ngc -p tsconfig-aot.json && rollup -c rollup-config.js",
464
- "lite:aot": "lite-server -c aot/bs-config.json",
516
+ + makeJson('cb-aot-compiler/ts/package.json' , { paths: ' scripts.build:aot, scripts.lite:aot' }, "package.json (convenience scripts)" )
517
+
465
518
:marked
466
519
Copy the AOT distribution files into the `/aot` folder with the node script:
467
- code-example( format = '.' ) .
520
+ code-example( language = "none" class = "code-shell" ) .
468
521
node copy-dist-files
469
522
.l-sub-section
470
523
:marked
471
524
You won't do that again until there are updates to `zone.js` or the `core-js` shim for old browsers.
472
525
:marked
473
526
Now AOT-compile the app and launch it with the `lite` server:
474
- code-example( format = '.' ) .
527
+ code-example( language = "none" class = "code-shell" ) .
475
528
npm run build:aot && npm run lite:aot
476
529
477
530
:marked
@@ -483,12 +536,12 @@ code-example(format='.').
483
536
tool can be quite revealing.
484
537
485
538
Install it:
486
- code-example( format = '.' ) .
539
+ code-example( language = "none" class = "code-shell" ) .
487
540
npm install source-map-explorer --save-dev
488
541
:marked
489
542
Run the following command to generate the map.
490
543
491
- code-example( format = '.' ) .
544
+ code-example( language = "none" class = "code-shell" ) .
492
545
node_modules/.bin/source-map-explorer aot/dist/build.js
493
546
494
547
:marked
0 commit comments