robisim74 / angular-library-starter

Build an Angular library compatible with AoT compilation and Tree shaking like an official package
MIT License
269 stars 58 forks source link
angular aot-compilation library tree-shaking typescript

angular-library-starter

Build Status

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.

Contents

1 Project structure

2 Customizing

  1. Update Node & npm.

  2. Rename angular-library-starter and angularLibraryStarter everywhere to my-library and myLibrary.

  3. Customize the license-banner.txt file with your library license.

  4. Update in package.json file:

    • version: Semantic Versioning
    • description
    • urls
    • packages (optional): make sure you use a version of TypeScript compatible with Angular Compiler

    and run npm install.

  5. Create your classes in src folder, and export public classes in my-library.ts.

  6. 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.

  7. Update in rollup.config.js file globals external dependencies with those that actually you use to build the umd bundle.

  8. Create unit & integration tests in tests folder, or unit tests next to the things they test in src folder, always using .spec.ts extension.

3 Testing

The following command runs unit & integration tests that are in the tests folder (you can change the folder in spec.bundle.js file):

npm test 

or in watch mode:

npm run test:watch

It also reports coverage using Istanbul.

4 Building

The following command:

npm run build

5 Publishing

Before publishing the first time:

npm run publish:lib

6 Documentation

To generate the documentation, this starter uses compodoc:

npm run compodoc
npm run compodoc:serve 

7 Using the library

Installing

npm install my-library --save 

Loading

Angular-CLI

No need to set up anything, just import it in your code.

Rollup or webpack

No need to set up anything, just import it in your code.

Using SystemJS configuration

System.config({
    map: {
        'my-library': 'node_modules/my-library/bundles/my-library.umd.js'
    }
});

Plain JavaScript

Include the umd bundle in your index.html:

<script src="https://github.com/robisim74/angular-library-starter/raw/master/node_modules/my-library/bundles/my-library.umd.js"></script>

and use global ng.myLibrary namespace.

AoT compilation

The library is compatible with AoT & Ivy.

8 What it is important to know

  1. 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
  2. tsconfig.json file used by TypeScript compiler

    • Compiler options:
      • "strict": true enables TypeScript strict master option
  3. tsconfig-build.json file used by ngc compiler

    • Compiler options:

      • "declaration": true to emit TypeScript declaration files
      • "module": "es2015" & "target": "es2015" are used by Rollup to create the ES2015 bundle
    • Angular Compiler Options:

      • "enableResourceInlining": true inlining of templates & styles
      • "skipTemplateCodegen": true skips generating AoT files
      • "strictMetadataEmit": true without 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": false libraries don't need to enable Ivy
  4. rollup.config.js file used by Rollup

    • format: 'umd' the Universal Module Definition pattern is used by Angular for its bundles
    • moduleName: 'ng.angularLibraryStarter' defines the global namespace used by JavaScript apps
    • external & globals declare the external packages
  5. Server Side Rendering

    If you want the library will be compatible with Server Side Rendering:

    • window, document, navigator and other browser types do not exist on the server
    • don't manipulate the nativeElement directly

9 Inlining of templates and stylesheets

Now ngc compiler supports inlining of templates & styles. Moreover, this starter allows you to use .scss sass files. If you need, you can use different pre-processors.

Built with this starter

Previous versions

License

MIT