Build an Angular library compatible with AoT compilation & Tree shaking like an official package.
This starter allows you to create a library for Angular apps. The project is based on the official Angular packages.
Get the Changelog.
- 1 Project structure
- 2 Customizing
- 3 Testing
- 4 Building
- 5 Publishing
- 6 Documentation
- 7 Using the library
- 8 What it is important to know
- 9 Inlining of templates and stylesheets
- Built with this starter
- Previous versions
- Library:
- src folder for the classes
- public_api.ts entry point for all public APIs of the package
- package.jsonnpm options
- rollup.config.jsRollup configuration for building the umd bundles
- rollup.es.config.jsRollup configuration for building the es2015 bundles
- tsconfig-build.jsonngc compiler options for AoT compilation
- build.js building process using ShellJS
- Testing:
- tests folder for unit & integration tests
- karma.conf.jsKarma configuration that uses webpack to build the tests
- spec.bundle.js defines the files used by webpack
- Extra:
- tslint.jsonAngular TSLint Preset (TypeScript linter rules with Codelyzer)
- travis.ymlTravis CI configuration
Update Node & npm.
Rename
angular-library-starterandangularLibraryStartereverywhere tomy-libraryandmyLibrary.Customize the
license-banner.txtfile with your library license.Update in
package.jsonfile:- version: Semantic Versioning
- description
- urls
- packages (optional): make sure you use a version of TypeScript compatible with Angular Compiler
and run
npm install.Create your classes in
srcfolder, and export public classes inmy-library.ts.You can create only one module for the whole library: I suggest you create different modules for different functions, so that the host app can only import the modules it uses, and optimize its Tree shaking.
Update in
rollup.config.jsfileglobalsexternal dependencies with those that actually you use to build the umd bundle.Create unit & integration tests in
testsfolder, or unit tests next to the things they test insrcfolder, always using.spec.tsextension.
The following command runs unit & integration tests that are in the tests folder (you can change the folder in spec.bundle.js file):
npm testor in watch mode:
npm run test:watchIt also reports coverage using Istanbul.
The following command:
npm run build- starts TSLint with Codelyzer using Angular TSLint Preset
- starts AoT compilation using ngc compiler
- creates
distfolder with all the files of distribution, following Angular Package Format (APF):
└── dist
├── bundles
| ├── my-library.umd.js
| ├── my-library.umd.js.map
| ├── my-library.umd.min.js
| └── my-library.umd.min.js.map
├── esm5
| ├── **/*.js
| └── **/*.js.map
├── esm2015
| ├── **/*.js
| └── **/*.js.map
├── fesm5
| ├── my-library.js
| └── my-library.js.map
├── fesm2015
| ├── my-library.js
| └── my-library.js.map
├── src
| └── **/*.d.ts
├── my-library.d.ts
├── my-library.metadata.json
├── LICENSE
├── package.json
├── public_api.d.ts
└── README
To test locally the npm package before publishing:
npm run pack:libThen you can install it in an app to test it:
npm install [path]my-library-{version}.tgzBefore publishing the first time:
- you can register your library on Travis CI: you have already configured
.travis.ymlfile - you must have a user on the npm registry: Publishing npm packages
npm run publish:libTo generate the documentation, this starter uses compodoc:
npm run compodoc
npm run compodoc:serve npm install my-library --save No need to set up anything, just import it in your code.
No need to set up anything, just import it in your code.
System.config({map: {'my-library': 'node_modules/my-library/bundles/my-library.umd.js'}});Include the umd bundle in your index.html:
<scriptsrc="node_modules/my-library/bundles/my-library.umd.js"></script>and use global ng.myLibrary namespace.
The library is compatible with AoT & Ivy.
package.json"main": "./bundles/angular-library-starter.umd.js"legacy module format"module": "./esm5/angular-library-starter.js"flat ES module, for using module bundlers such as Rollup or webpack"es2015": "./esm2015/angular-library-starter.js"ES2015 flat ESM format"typings"declaration files for TypeScript compiler"peerDependencies"the packages and their versions required by the library when it will be installed
tsconfig.jsonfile used by TypeScript compiler- Compiler options:
"strict": trueenables TypeScriptstrictmaster option
- Compiler options:
tsconfig-build.jsonfile used by ngc compilerCompiler options:
"declaration": trueto emit TypeScript declaration files"module": "es2015"&"target": "es2015"are used by Rollup to create the ES2015 bundle
Angular Compiler Options:
"enableResourceInlining": trueinlining of templates & styles"skipTemplateCodegen": trueskips generating AoT files"strictMetadataEmit": truewithout emitting metadata files, the library will not be compatible with AoT compilation: it is intended to report syntax errors immediately rather than produce a .metadata.json file with errors"flatModuleId": "@scope/package"full package name has to include scope as well, otherwise AOT compilation will fail in the consumed application"enableIvy": falselibraries don't need to enable Ivy
rollup.config.jsfile used by Rollupformat: 'umd'the Universal Module Definition pattern is used by Angular for its bundlesmoduleName: 'ng.angularLibraryStarter'defines the global namespace used by JavaScript appsexternal&globalsdeclare the external packages
Server Side Rendering
If you want the library will be compatible with Server Side Rendering:
window,document,navigatorand other browser types do not exist on the server- don't manipulate the nativeElement directly
Now ngc compiler supports inlining of templates & styles. Moreover, this starter allows you to use .scsssass files. If you need, you can use different pre-processors.
- angular-auth-oidc-clientAn OpenID Connect Implicit Flow client for Angular
- ngx-infinite-scrollAn infinite scroll directive for Angular compatible with AoT compilation and Tree shaking
- ngx-typeaheadA simple but yet powerful typeahead component for Angular
- ng2-youtube-playerA Powerful Youtube Player Component for Angular
- ng2-completerAngular autocomplete component
- ngx-storeAngular Storage library for managing
localStorage,sessionStorageand cookies, allowing to watch storage changes. Includes easy-to-use decorators, services and API based on builder pattern. - ngx-table-editorA library for Angular that transforms HTML tables into dynamic editable components.
- ngx-ui-scrollAn Angular
*ngFor-like directive for infinite/virtual scrolling
MIT