Giter Site home page Giter Site logo

jvquarck / angular-library-starter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from robisim74/angular-library-starter

0.0 2.0 0.0 67 KB

Build an Angular library compatible with AoT compilation and Tree shaking

License: MIT License

JavaScript 75.28% TypeScript 24.72%

angular-library-starter's Introduction

angular-library-starter

Build Status

Build an Angular library compatible with AoT compilation & Tree shaking.

This starter allows you to create a library for Angular 4+ apps written in TypeScript, ES6 or ES5. The project is based on the official Angular packages.

Get the Changelog.

Contents

1 Project structure

  • Library:
    • src folder for the classes
    • public_api.ts entry point for all public APIs of the package
    • package.json npm options
    • rollup.config.js Rollup configuration for building the bundles
    • tsconfig-build.json ngc compiler options for AoT compilation
    • build.js building process using ShellJS
  • Testing:
    • tests folder for unit & integration tests
    • karma.conf.js Karma configuration that uses webpack to build the tests
    • spec.bundle.js defines the files used by webpack
  • Extra:
    • tslint.json TypeScript linter rules with Codelyzer
    • travis.yml Travis CI configuration

2 Customizing

  1. Update Node & npm.

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

  3. Update in package.json file:

    and run npm install.

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

  5. You can create only one module for the whole library: I suggest you create different modules for different functions, so that the user can import only those he needs and optimize Tree shaking of his app.

  6. Update in rollup.config.js file globals external dependencies with those that actually you use.

  7. Create unit & integration tests in tests folder, or unit tests next to the things they test in src folder, always using .spec.ts extension. Karma is configured to use webpack only for *.ts files: if you need to test different formats, you have to update it.

3 Testing

The following command run unit & integration tests that are in the tests folder, and unit tests that are in src folder:

npm test 

4 Building

The following command:

npm run build
  • starts TSLint with Codelyzer
  • starts AoT compilation using ngc compiler
  • creates dist folder with all the files of distribution

To test locally the npm package:

npm run pack-lib

Then you can install it in an app to test it:

npm install [path]my-library-[version].tgz

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

Using SystemJS configuration

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

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.

Plain JavaScript

Include the umd bundle in your index.html:

<script src="node_modules/my-library/bundles/my-library.umd.js"></script>

and use global ng.myLibrary namespace.

AoT compilation

The library is compatible with AoT compilation.

8 What it is important to know

  1. package.json

    • "main": "./bundles/angular-library-starter.umd.js" legacy module format
    • "module": "./bundles/angular-library-starter.es5.js" flat ES module, for using module bundlers such as Rollup or webpack: package module
    • "es2015": "./bundles/angular-library-starter.js" ES2015 flat ESM format, experimental ES2015 build
    • "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:

      • "skipTemplateCodegen": true, skips generating AoT files
      • "annotateForClosureCompiler": true for compatibility with Google Closure compiler
      • "strictMetadataEmit": true without emitting metadata files, the library will not compatible with AoT compilation
  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 prerendering

    If you want the library will be compatible with server-side prerendering:

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

License

MIT

angular-library-starter's People

Contributors

robisim74 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.