Wednesday, 2 September 2020

Angular create, pack, use, publish library

 // Create a new Application 


ng new myDemoApp



/*

* The following require an angular application

*/


// Create library, created library will be in root/projects. Regular appliaction will  be in root/src

// angular.json will have projects : {'DemoLib" : {projectTyle:library..}}

ng g library DemoLib


// Create component for library (This library name must be the same name in angular.json)

cd rootApp

ng g c DemoComponent --project DemoLib


// Then in projects/demo-lib/src/lib/demo-lib.module need to export DemoComponent

// Then in projects/demo-lib/src/public-api.ts need to export * from './lib/demo-grid/demo-grid.component';


// package.json dependencies will  have ng-pacjkager. tsconfig.json will have path: demo-lib : dist/demo-lib, this is output path for library when doing ng build --demolib.

// build libray (This library name must be the same name in angular.json)

ng build --DemoLib


// Creating libraries with dependcies are not recommended, need to move to peerDependencies on package.json && in ng-package.json add     "whitelistedNonPeerDependencies": [

        "." //All

    ]



// Test library in current App

// Current App need to install the dependencies required by library such as ag-grid community etc

npm install --save ag-grid-community ag-grid-angular

// Current APP need to add style sheet in styles.scss


App/src/app.module import{DemoLibModule} from './demoLib' (specified in tsconfig.json -> path, should be mapped to dist/demo-lib"

use library component in app.component.ts


// create .tgz for libray

cd dist/demolib

npm pack


// Test generated .tgz from app

npm install fullpath to .tgz


// publish to npm 

npm adduser


// push to npm 

npm publish --access public


// install library

npm install



// CD into library

cd projects/demo-lib


// Install ag-grid-community and ag-grid-angular for the library

npm install --save ag-grid-community ag-grid-angular


// Will receive NPM WARN a list of things in peer dependecies not installed in the library,must do manual installation 

// Install them and save in devdependencies in pacakge JSON

npm install -save-dev f @angular/common@^9.0.0

npm install --save-dev @angular/core@^9.1.12 

 npm install --save-dev rxjs@^6.5.3

npm install --save-dev  tslib@^1.10.0

npm install --save-dev  zone.js@~0.10.3


No comments:

Post a Comment