3

I created an Angular Library by running:

ng new my-library-lib
cd my-library-lib
ng generate library my-library

This creates an Angular application and within it a folder called "projects" with the library code:

Application code:

- projects
-- my-library
--- src
---- lib
----- <code of the library (components, services)>
---- public_api.ts
--- <config files (ng-package.json, package.json, ...)>
- src
-- <code of the container application>

So having this structure, I don' know how to publish the library in Bitbucket to be consumed by other applications in this way:

package.json:

"my-library": "git+https://bitbucket/.../.../my-library.git#v0.0.1"

I followed a tutorial to create the library and I'm able to use it in the container application. Also I managed to use it in other applications by installing the .tgz created:

"build_lib": "ng build my-library",
"npm_pack_lib": "cd dist/my-library && npm pack",
"package_lib": "npm run build_lib && npm run npm_pack_lib"

The last step is to publish it and tag it to be consumed.

Alavaros
  • 1,665
  • 7
  • 32
  • 52

1 Answers1

1

For publishing a lib there are lots of things that need to be taken care of, fortunately, there is a tool ng-packagr which makes our life really easy, ng-packagr is a node library that can compile and package a TypeScript library to Angular Package Format.

https://www.npmjs.com/package/ng-packagr
Akshay Rajput
  • 1,978
  • 1
  • 12
  • 22
  • Correct me if I'm wrong, but I believe that `ng build my-library` does the same thing. I already have the dist folder created – Alavaros Dec 17 '18 at 09:00
  • Sorry, i didn't knew that I use nrwl(nrwl.io) to create my angular projects, once the build is done we go to dist directory and do npm pack a zip file is created, which can be published/ used in other projects. – Akshay Rajput Dec 17 '18 at 09:09
  • Should I publish the dist folder as a project in another repository? – Alavaros Dec 17 '18 at 10:27
  • you can create a new folder library, push your zip file inside it, assuming its name is XYZ, do npm i ./library/XYZ from the root, it will update the package.json, – Akshay Rajput Dec 17 '18 at 10:30
  • Do you mean to create a new repo with only the zip file? What I want to achieve, which I don't know if I can, is to upload the code to a repository in Bitbucket, tag it and import it into any other application as I have put in the description, pointing to the repository and the tag – Alavaros Dec 17 '18 at 14:12
  • 1
    either you can copy that zip file inside a new project in a new folder below src, and npm i (path), else you can publish the package on npmjs.com and use it without need to do all this process manually for all the projects, to publish a package on npm refer this article https://hackernoon.com/publish-your-own-npm-package-946b19df577e – Akshay Rajput Dec 18 '18 at 04:19