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

Commit a4015ab

Browse files
committed
docs(aot-cookbook): workflow suggestion; show how to run with JIT
1 parent 60b4287 commit a4015ab

File tree

6 files changed

+107
-33
lines changed

6 files changed

+107
-33
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// #docregion
22
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
3-
43
import { AppModule } from './app.module';
54

5+
console.log('Running JIT compiled');
66
platformBrowserDynamic().bootstrapModule(AppModule);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// #docregion
22
import { platformBrowser } from '@angular/platform-browser';
3-
43
import { AppModuleNgFactory } from '../aot/app/app.module.ngfactory';
54

5+
console.log('Running AOT compiled');
66
platformBrowser().bootstrapModuleFactory(AppModuleNgFactory);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!-- #docregion -->
2+
<!DOCTYPE html>
3+
<html>
4+
<head>
5+
<title>Ahead of time compilation (JIT)</title>
6+
<meta charset="UTF-8">
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
<link rel="stylesheet" href="styles.css">
9+
10+
<script src="node_modules/core-js/client/shim.min.js"></script>
11+
<script src="node_modules/zone.js/dist/zone.js"></script>
12+
<!-- #docregion jit -->
13+
<script src="node_modules/systemjs/dist/system.src.js"></script>
14+
<script src="systemjs.config.js"></script>
15+
<script>
16+
System.import('app/main-jit').catch(function(err){ console.error(err); });
17+
</script>
18+
<!-- #enddocregion jit -->
19+
</head>
20+
<body>
21+
<my-app>Loading...</my-app>
22+
</body>
23+
</html>

public/docs/_examples/cb-aot-compiler/ts/index.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@
1313
<script>window.module = 'aot';</script>
1414
<!-- #enddocregion moduleId -->
1515
</head>
16-
17-
<!-- #docregion bundle -->
1816
<body>
1917
<my-app>Loading...</my-app>
2018
</body>
21-
19+
<!-- #docregion bundle -->
2220
<script src="dist/build.js"></script>
2321
<!-- #enddocregion bundle -->
2422
</html>

public/docs/_examples/cb-aot-compiler/ts/rollup-config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ export default {
1111
sourceMap: false,
1212
format: 'iife',
1313
onwarn: function(warning) {
14-
// skip certain warnings
14+
// Skip certain warnings
1515

16-
// Should intercept ... but doesn't in some rollup versions
16+
// should intercept ... but doesn't in some rollup versions
1717
if ( warning.code === 'THIS_IS_UNDEFINED' ) { return; }
18-
// Intercepts in some rollup versions
18+
// intercepts in some rollup versions
1919
if ( warning.indexOf("The 'this' keyword is equivalent to 'undefined'") > -1 ) { return; }
2020

2121
// console.warn everything else
22-
console.warn ? console.warn( warning.message ) : console.log( warning.message ) ;
22+
console.warn( warning.message );
2323
},
2424
plugins: [
2525
nodeResolve({jsnext: true, module: true}),

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

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ a#toc
1414
* [Tree Shaking](#tree-shaking)
1515
* [Load the bundle](#load)
1616
* [Serve the app](#serve)
17+
* [Workflow and convenience script](#workflow)
1718
* [Source Code](#source-code)
1819
* [Tour of Heroes](#toh)
1920

@@ -104,7 +105,7 @@ a#compile
104105

105106
:marked
106107
Install a few new npm dependencies with the following command:
107-
code-example(format='.').
108+
code-example(language="none" class="code-shell").
108109
npm install @angular/compiler-cli @angular/platform-server --save
109110
:marked
110111
You will run the `ngc` compiler provided in the `@angular/compiler-cli` npm package
@@ -150,7 +151,7 @@ code-example(format='.').
150151
### Compiling the application
151152

152153
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").
154155
node_modules/.bin/ngc -p tsconfig-aot.json
155156
.l-sub-section
156157
:marked
@@ -188,17 +189,21 @@ a#bootstrap
188189

189190
Instead of bootstrapping `AppModule`, you bootstrap the application with the generated module factory, `AppModuleNgFactory`.
190191

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`.
193198

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:
195200

196201
+makeTabs(
197202
`cb-aot-compiler/ts/app/main.ts,
198203
cb-aot-compiler/ts/app/main-jit.ts`,
199204
null,
200-
`app/main.ts (AOT),
201-
app/main.ts (JIT)`
205+
`app/main.ts,
206+
app/main-jit.ts`
202207
)
203208

204209
:marked
@@ -250,6 +255,8 @@ code-example(format='.').
250255
:marked
251256
It tells Rollup that the app entry point is `app/main.js` .
252257
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+
253260
Then there are plugins.
254261

255262
:marked
@@ -288,23 +295,20 @@ code-example(format='.').
288295
Execute the Rollup process with this command:
289296
code-example(format='.').
290297
node_modules/.bin/rollup -c rollup-config.js
291-
292298
.l-sub-section
293299
: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
300304
a#load
301305
.l-main-section
302306
:marked
303307
## Load the Bundle
304308

305309
Loading the generated application bundle does not require a module loader like SystemJS.
306310
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:
308312

309313
+makeExample('cb-aot-compiler/ts/index.html','bundle','index.html (load bundle)')(format='.')
310314

@@ -315,11 +319,11 @@ a#serve
315319

316320
You'll need a web server to host the application.
317321
Use the same _Lite Server_ employed elsewhere in the documentation:
318-
code-example(format='.').
322+
code-example(language="none" class="code-shell").
319323
npm run lite
320324
:marked
321325
The server starts, launches a browser, and the app should appear.
322-
326+
323327
a#source-code
324328
.l-main-section
325329
:marked
@@ -342,6 +346,56 @@ a#source-code
342346
rollup-config.js`
343347
)
344348

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+
345399
a#toh
346400
.l-main-section
347401
:marked
@@ -459,19 +513,18 @@ a#toh
459513
tsconfig-aot.json`)
460514
:marked
461515
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+
465518
:marked
466519
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").
468521
node copy-dist-files
469522
.l-sub-section
470523
:marked
471524
You won't do that again until there are updates to `zone.js` or the `core-js` shim for old browsers.
472525
:marked
473526
Now AOT-compile the app and launch it with the `lite` server:
474-
code-example(format='.').
527+
code-example(language="none" class="code-shell").
475528
npm run build:aot && npm run lite:aot
476529

477530
:marked
@@ -483,12 +536,12 @@ code-example(format='.').
483536
tool can be quite revealing.
484537

485538
Install it:
486-
code-example(format='.').
539+
code-example(language="none" class="code-shell").
487540
npm install source-map-explorer --save-dev
488541
:marked
489542
Run the following command to generate the map.
490543

491-
code-example(format='.').
544+
code-example(language="none" class="code-shell").
492545
node_modules/.bin/source-map-explorer aot/dist/build.js
493546

494547
:marked

0 commit comments

Comments
 (0)