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

Commit b1a705d

Browse files
committed
add AOT and JIT appendix
1 parent e6d18e5 commit b1a705d

File tree

1 file changed

+24
-190
lines changed

1 file changed

+24
-190
lines changed

public/docs/ts/latest/cookbook/third-party-lib.jade

Lines changed: 24 additions & 190 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ include ../_util-fns
1111
Modern web development has changed this process.
1212
Instead of publishing a "one size fits all" bundle, developers want to only include the parts of the library they actually need and in the format they need it in.
1313

14-
This cookbook shows how to publish a third party library in a way that makes it possible to take advantage of techniques like Ahead of Time Compilation (AoT) and Tree Shaking.
14+
This cookbook shows how to publish a third party library in a way that makes it possible to take advantage of techniques like Ahead of Time Compilation (AOT) and Tree Shaking.
1515

1616
<a id="toc"></a>
1717
:marked
1818
## Table of contents
1919

2020
[Creating a Third Party Library](#third-party-lib)
2121

22-
[Supporting AoT](#aot)
22+
[Supporting AOT](#AOT)
2323

2424
[Preparing the library for Tree Shaking](#tree-shaking)
2525

26-
[Supporting JiT](#jit)
26+
[Supporting JIT](#JIT)
2727

2828
[Publish](#publish)
2929

@@ -200,11 +200,12 @@ table(width="100%")
200200
In order to understand how to build and publish a library, you have to consider _how_ the library is going to be consumed.
201201

202202
Some users need to add it as a `<script>` tag.
203-
Others might be using SystemJS instead.
204-
Bundlers, like Webpack, are very popular as well.
205-
Typescript users need type definitions.
206-
Rollup users make use of ECMAScript modules for tree-shaking.
207-
AOT applications need library metadata to compile.
203+
Others might be using [SystemJS](https://github.com/systemjs/systemjs) instead.
204+
Bundlers, like [Webpack](https://webpack.js.org/), are very popular as well.
205+
[Typescript](typescriptlang.org) users need type definitions.
206+
[Rollup](https://github.com/rollup/rollup) users make use of ECMAScript modules for tree-shaking.
207+
Both [Ahead-of-time](#appendix-supporting-aot) and [Just-in-time](#appendix-supporting-it)
208+
compilation should be supported.
208209

209210
It's daunting to think of all the ways your library might be used and how to accomodate it,
210211
but you don't need to have a "one-size-fits-all" library.
@@ -248,7 +249,7 @@ code-example(language="json").
248249
- Compile with `ngc` for ES5 and ES2015.
249250
- Inline html and css inside the generated JavaScript files.
250251
- Copy typings, metatada, html and css.
251-
- Create each bundle using rollup.
252+
- Create each bundle using Rollup.
252253

253254
.l-main-section
254255
:marked
@@ -284,6 +285,9 @@ code-example(language="json").
284285
:marked
285286
## Publishing your library
286287

288+
The `.npmignore` file defines what files and folders will be published to NPM: `dist/`,
289+
`LICENSE`, `package.json` and `README.md`.
290+
287291
Every package on NPM has a unique name, and so should yours.
288292
If you haven't already, now is the time to change the name of your library.
289293

@@ -314,7 +318,7 @@ code-example(language="json").
314318
You'll need to create a NPM account, login on your local machine, and then you can publish updated
315319
by running `npm publish`.
316320

317-
Remember to follow [Semantic Versioning](http://semver.org/)!
321+
Remember to follow [Semantic Versioning](http://semver.org/) and document your library!
318322

319323
.l-main-section
320324
:marked
@@ -336,58 +340,19 @@ code-example(language="json").
336340

337341
content
338342

339-
=============
340-
341343
.l-main-section
342-
<a id="third-party-lib"></a>
343344
:marked
344-
## Creating a Third Party Library
345-
346-
This cookbook shows how to create a simple Hero-Profile library and publish it with support for AoT compilation and Tree Shaking.
347-
348-
A version of the library intended for JiT compiled applications will also be included.
349-
350-
The code for the Hero-Profile library can be found below.
345+
## Appendix: Supporting AOT
351346

352-
To support both AoT and JiT compilation there are two different tsconfig files.
353-
354-
`tsconfig.json` for JiT compilation and `tsconfig-aot.json` for AoT compilation.
355-
356-
+makeTabs(
357-
`cb-third-party-lib/hero-profile/hero-profile.module.ts,
358-
cb-third-party-lib/hero-profile/tsconfig-aot.json,
359-
cb-third-party-lib/ts/tsconfig.json,
360-
cb-third-party-lib/hero-profile/hero-profile.component.html,
361-
cb-third-party-lib/hero-profile/hero-profile.component.ts,
362-
cb-third-party-lib/hero-profile/hero-profile.component.css,
363-
cb-third-party-lib/hero-profile/hero.ts,
364-
cb-third-party-lib/hero-profile/package.json`,
365-
null,
366-
`hero-profile.module.ts,
367-
tsconfig-aot.json,
368-
tsconfig.json,
369-
hero-profile.component.html,
370-
hero-profile.component.ts,
371-
hero-profile.component.css,
372-
hero.ts,
373-
package.json`
374-
)(format='.')
375-
376-
.l-main-section
377-
<a id="aot"></a>
378-
:marked
379-
## Supporting AoT
347+
AOT plays an important role in optimizing Angular applications. It's therefore important that third party libraries be published in a format compatible with AOT compilation. Otherwise it will not be possible to include the library in an AOT compiled application.
380348

381-
AoT plays an important role in optimizing Angular applications. It's therefore important that third party libraries be published in a format compatible with AoT compilation. Otherwise it will not be possible to include the library in an AoT compiled application.
382-
383-
Only code written in TypeScript can be AoT compiled.
349+
Only code written in TypeScript can be AOT compiled.
384350

385351
Before publishing the library must first be compiled using the `ngc` compiler.
386352

387-
`ngc` extends the `tsc` compiler by adding extensions to support AoT compilation in addition to regular TypeScript compilation.
353+
`ngc` extends the `tsc` compiler by adding extensions to support AOT compilation in addition to regular TypeScript compilation.
388354

389-
:marked
390-
AoT compilation outputs three files that must be included in order to be compatible with AoT.
355+
AOT compilation outputs three files that must be included in order to be compatible with AOT.
391356

392357
*Transpiled JavaScript*
393358

@@ -405,7 +370,7 @@ code-example(language="json").
405370

406371
### NgFactories
407372

408-
`ngc` generates a series of files with an `.ngfactory` suffix as well. These files represent the AoT compiled source, but should not be included with the published library.
373+
`ngc` generates a series of files with an `.ngfactory` suffix as well. These files represent the AOT compiled source, but should not be included with the published library.
409374

410375
Instead the `ngc` compiler in the consuming application will generate `.ngfactory` files based on the JavaScript, Typings and meta data shipped with the library.
411376

@@ -417,143 +382,12 @@ code-example(language="json").
417382

418383
Publishing plain JavaScript with typings and meta data allows the consuming application to remain agnostic of the library's build environment.
419384

420-
<a id="tree-shaking"></a>
421-
:marked
422-
## Preparing the library for Tree Shaking
423-
424-
In addition to supporting AoT, the library code should also be "Tree Shakable".
425-
426-
Tree Shakers work best with `ES2015` JavaScript.
427-
428-
`ES2015` `import` and `export` statements make it easier to statically analyse the code to determine which modules are in use by the application.
429-
430-
By setting the `module` attribute in `tsconfig-aot.json` to `es2015`, the transpiled JavaScript will use `ES2015` modules.
431-
432-
The library is made up of several independent files. Bundler frameworks like `Rollup` and `Webpack` need a way to determine the entry point to the module. In this example the entry point is `index.js`, the transpiled version of the index.ts TypeScript barrel.
433-
434-
The entry point to the module is configured using the `module` attribute in `package.json`. In this case `module` is defined as `"module": "index.js"`.
435-
436-
<a id="jit"></a>
437-
:marked
438-
## Supporting JiT
439-
440-
AoT compiled code is the prefered format for production builds, but due to the long compilation time, it may not be practical to use AoT during development.
441-
442-
To create a more flexible developer experience, a JiT compatible build of the library should be published as well. The format of the JiT bundle is `umd`, which stands for Universal Module Definition. Shipping the bundle as `umd` ensures compatibility with most common module loading formats.
443-
444-
The `umd` bundle will ship as a single file containing the JavaScript and inlined versions of any external templates or css.
445-
446-
The path to the `umd` file identified in package.json as `"main": "bundles/hero-profile.umd.js"`
447-
448-
In `tsconfig.json` the module for the `umd` bundle is specified as `commonjs`, not `es2015`. This is done to ensure that the bundle can be executed "as is", without further transpilation or bundling.
449-
450-
To generate the bundle you will be using a framework called `Rollup`.
451-
452-
The JiT build assumes the following Rollup and TypeScript configuration.
453-
454-
+makeTabs(
455-
`cb-third-party-lib/hero-profile/rollup-config.js,
456-
cb-third-party-lib/ts/tsconfig.json`,
457-
null,
458-
`rollup-config.js,
459-
tsconfig.json`
460-
)(format='.')
461-
:marked
462-
Generate the `umd` bundle by running `node_modules/.bin/rollup -c rollup-config.js`.
463-
464-
<a id="publish"></a>
465-
:marked
466-
## Publish
467-
468-
:marked
469-
`Rollup` outputs the `umd` bundle, but prior to bundling, all external templates or css files must be inlined.
470-
471-
There are a few options for how to do inlining. This cookbook uses an approach borrowed from `Angular Material 2`.
472-
473-
The idea is to create a node script that will walk the component code and replace `templateUrl` and `styleUrls` references with inlined html and css.
474-
475-
Additionally the script will remove references to `module.id` since it's no longer needed after templates and css have been inlined.
476-
477-
Angular ships with its own set of `umd` bundles. These bundles can be referenced by Rollup when generating the `umd` bundle.
478-
479-
In this example there is a dependency on `@angular/core`.
480-
481-
By listing `@angular/core` as a `global` in the configuration, `Rollup` will point to the `@angular/core` umd bundle.
482-
483-
Normally third party libraries will be published to `npm`. This cookbook simulates publishing by executing all required steps in the `publish.js` script.
484-
485-
`publish.js` will do the following:
486-
487-
1) AoT compile the Hero-Profile library by running `node_modules/.bin/ngc -p tsconfig-aot.json`
488-
489-
2) Inline templates and css using the `inline-resources.js` script
490-
491-
3) Create an `umd` bundle by running `node_modules/.bin/rollup -c rollup-config.js`
492-
493-
+makeTabs(
494-
`cb-third-party-lib/hero-profile/publish.js,
495-
cb-third-party-lib/hero-profile/inline-resources.js`,
496-
null,
497-
`publish.js,
498-
inline-resources.js`
499-
)(format='.')
500-
501-
.l-main-section
502-
<a id="integrate-with-app"></a>
503-
:marked
504-
## Integrate with Application
505-
506-
The library is now ready to be integrated with either AoT compiled applications or JiT compiled applications. The following sections describes how to configure both.
507-
508-
For the purposes of this demo the JiT and the AoT versions are loaded using the same index.html page.
509-
510-
### AoT
511-
512-
AoT compiled applications will integrate the library in the compilation of the application as a whole.
513-
514-
As in the <a href="/docs/ts/latest/cookbook/aot-compiler.html">AoT compilation Cookbook</a> `ngc` is used in combination with `Rollup` to AoT and Tree Shake the application.
515-
516-
Run the command `ngc -p tsconfig-aot.json && rollup -c rollup-config.js` to execute the combined steps of AoT compilation and Tree Shaking.
517-
518-
`tsconfig-aot.json` and `rollup-config.js` contain the necessary configuration.
519-
520-
+makeTabs(
521-
`cb-third-party-lib/ts/rollup-config.js,
522-
cb-third-party-lib/ts/tsconfig-aot.json`,
523-
null,
524-
`rollup-config.js,
525-
tsconfig-aot.json`
526-
)(format='.')
527-
528-
:marked
529-
Inside `rollup-config` there is a `nodeResolve` section. This is where the `module` setting from `package.json` comes into play.
530-
531-
`nodeResolve` will look for either `module` or `jsnext` in `package.json` to determine how to bundle external libraries.
532-
533-
+makeExample('cb-third-party-lib/ts/rollup-config.js', 'nodeResolve', 'rollup-config.js')(format=".")
534-
535-
:marked
536-
The combined output of `ngc` and `Rollup` is a single `build.js` JavaScript file.
537-
538-
`build.js` contains the entire application including all Angular dependencies and the third party Hero-Profile library.
539-
540-
To run the application in AoT mode, include `build.js` as a script tag and the `my-aot-app` root level component tag in `index.html`
541-
542-
### JiT
543-
544-
JiT applications load the `umd` bundle using `SystemJS`. This requires a minor tweak to `systemjs.config.js` to register the `umd` bundle with `SystemJS`.
545-
546-
Simply add `'hero-profile': 'npm:hero-profile/bundles/hero-profile.umd.js'` to the map section of `systemjs.config.js`.
547-
548385
.l-main-section
549-
<a id="final-app"></a>
550386
:marked
551-
## Final Application
387+
## Appendix: Supporting JIT
552388

553-
If you have cloned the `Angular.io` repo, all the steps described in this cookbook can be executed by running the following command:
389+
AOT compiled code is the prefered format for production builds, but due to the long compilation time, it may not be practical to use AOT during development.
554390

555-
`gulp add-example-boilerplate && npm run build:aot:jit && npm run lite`
391+
To create a more flexible developer experience, a JIT compatible build of the library should be published as well. The format of the JIT bundle is `umd`, which stands for Universal Module Definition. Shipping the bundle as `umd` ensures compatibility with most common module loading formats.
556392

557-
After loading both the JiT and AoT versions of the library the final application looks like this:
558-
figure.image-display
559-
img(src="/resources/images/cookbooks/third-party-lib/third-party-lib.png" alt="Third-Party-Library")
393+
The `umd` bundle will ship as a single file containing ES5 JavaScript and inlined versions of any external templates or css.

0 commit comments

Comments
 (0)