/ng2-cache

Client side caching service for Angular2

Primary LanguageTypeScriptMIT LicenseMIT

ng2-cache

npm npm

ng2-cache library compatible with AoT compilation & Tree shaking like an official package.

This lib allows you to use ng2-cache library for Angular v5 apps written in TypeScript, ES6 or ES5. The project is based on the official Angular packages.

Get the Changelog.

How to use

  1. npm install ng2-cache --save

  2. Import it in your main app module

    @NgModule({
    declarations: [
        ....
    ],
    imports: [
        ....
        Ng2CacheModule
    ],
    providers: [],
    bootstrap: [AppComponent]
    })
    export class AppModule { }
    
  3. Use it inside your Component by DI

    ...
    import { CacheService, CacheStoragesEnum } from 'ng2-cache';
    ...
    
    export class AppComponent {
    
    constructor(private _cacheService: CacheService) { 
        this._cacheService.set('key', ['some data']);
    } 
    

That's it. Now you don't have to mention it's reference in tsfoncig.json

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 umd bundles
    • rollup.es.config.js Rollup configuration for building the es2015 bundles
    • tsconfig-build.json ngc compiler options for AoT compilation
    • build.js building process using ShellJS
  • Extra:
    • tslint.json Angular TSLint Preset (TypeScript linter rules with Codelyzer)
    • travis.yml Travis CI configuration

2 Building

The following command:

npm run build
  • starts TSLint with Codelyzer using Angular TSLint Preset
  • starts AoT compilation using ngc compiler
  • creates dist folder with all the files of distribution, following Angular Package Format (APF) v5.0:
└── dist
    ├── bundles
    |   ├── ng2-cache.umd.js
    |   ├── ng2-cache.umd.js.map
    |   ├── ng2-cache.umd.min.js
    |   └── ng2-cache.umd.min.js.map
    ├── esm5
    |   ├── ng2-cache.js
    |   └── ng2-cache.js.map
    ├── esm2015
    |   ├── ng2-cache.js
    |   └── ng2-cache.js.map
    ├── src
    |   └── **/*.d.ts
    ├── ng2-cache.d.ts
    ├── ng2-cache.metadata.json
    ├── LICENSE
    ├── package.json
    ├── public_api.d.ts
    └── README

To test locally the npm package before publishing:

npm run pack:lib

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

npm install [path]ng2-cache-[version].tgz

3 Publishing

Before publishing the first time:

npm run publish:lib

4 Using the library

Installing

npm install ng2-cache --save 

Loading

Using SystemJS configuration

System.config({
    map: {
        'ng2-cache': 'node_modules/ng2-cache/bundles/ng2-cache.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/ng2-cache/bundles/ng2-cache.umd.js"></script>

and use global ng.ng2Cache namespace.

AoT compilation

The library is compatible with AoT compilation.

5 What it is important to know

  1. package.json

    • "main": "./bundles/ng2-cache.umd.js" legacy module format
    • "module": "./esm5/ng2-cache.js" flat ES module, for using module bundlers such as Rollup or webpack: package module
    • "es2015": "./esm2015/ng2-cache.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 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
  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

License

MIT