You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 4, 2017. It is now read-only.
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.
13
13
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.
15
15
16
16
<aid="toc"></a>
17
17
:marked
18
18
## Table of contents
19
19
20
20
[Creating a Third Party Library](#third-party-lib)
21
21
22
-
[Supporting AoT](#aot)
22
+
[Supporting AOT](#AOT)
23
23
24
24
[Preparing the library for Tree Shaking](#tree-shaking)
25
25
26
-
[Supporting JiT](#jit)
26
+
[Supporting JIT](#JIT)
27
27
28
28
[Publish](#publish)
29
29
@@ -200,11 +200,12 @@ table(width="100%")
200
200
In order to understand how to build and publish a library, you have to consider _how_ the library is going to be consumed.
201
201
202
202
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.
208
209
209
210
It's daunting to think of all the ways your library might be used and how to accomodate it,
210
211
but you don't need to have a "one-size-fits-all" library.
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.
380
348
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.
384
350
385
351
Before publishing the library must first be compiled using the `ngc` compiler.
386
352
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.
388
354
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.
`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.
409
374
410
375
Instead the `ngc` compiler in the consuming application will generate `.ngfactory` files based on the JavaScript, Typings and meta data shipped with the library.
Publishing plain JavaScript with typings and meta data allows the consuming application to remain agnostic of the library's build environment.
419
384
420
-
<aid="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
-
<aid="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.
Generate the `umd` bundle by running `node_modules/.bin/rollup -c rollup-config.js`.
463
-
464
-
<aid="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`
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.
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
-
548
385
.l-main-section
549
-
<aid="final-app"></a>
550
386
:marked
551
-
## Final Application
387
+
## Appendix: Supporting JIT
552
388
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.
554
390
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.
556
392
557
-
After loading both the JiT and AoT versions of the library the final application looks like this:
0 commit comments