Angular libraries cross-dependency
Asked Answered
T

3

5

I am looking for some help with Angular7 libraries.

I have a project A in which I have developed two libraries - library 1 and library 2. The second library (library 2) has a dependency on the first library (library 1). Later, in another project, let's say project B I may use library 2.

My problem is with specifying that library 2 has a dependency on library 1. Currently, the two libraries are built in a folder libs/ in the root of the project - which makes library 2 imports from library 1 works with and without specifying that it has a dependency on library 1 in its package.json file.

Library 1 package.json

{
  "name": "library-1",
  "version": "0.0.1",
  "peerDependencies": {
    "@angular/common": "^7.1.0",
    "@angular/core": "^7.1.0"
  }
}

Library 2 package.json

{
  "name": "library-2",
  "version": "0.0.1",
  "peerDependencies": {
    "@angular/common": "^7.1.0",
    "@angular/core": "^7.1.0",
    "@angular/material": "7.2.0",
    "library-1": "0.0.1"
  }
}

Also, their build+dev location is specified in the main tsconfig.json file:

{
  ...,
  "compilerOptions": {
    ...,
    "paths": {
      "library-1": ["libs/library-1", "projects/library-1/src/"],
      "library-1/*": ["libs/library-1/*", "projects/library-1/src/*"],
      "library-2": ["libs/library-2", "projects/library-2/src/"],
      "library-2/*": ["libs/library-2/*", "projects/library-2/src/*"]
    }
  }
}

Is there a way where I can make it explicit that the second library does not compile if the first one is not installed?

Therianthropic answered 18/2, 2019 at 13:6 Comment(0)
T
6

If anyone else is having a similar setup and wants to know what I did.

1.Added the paths for the different libraries which point to the specific public_api.ts in the root level tsconfig.json. This allows on the fly compilation when you use the library in the main project.

{ 
   ...options,
   "paths": { 
       "library-1": "projects/library-1/src/public_api.ts",
       "library-2": "projects/library-2/src/public_api.ts"
   }
}

2.Added peer dependency in package.json of library-2 to library-1. This would show a warning if you use library-2 in a project without adding library-1 as a dependency as well.

{
    ...options, 
    "peerDependencies": {
         "library-1": "0.1"
     }
}

3. Added an extra tsconfig.build.json extends the tsconfig.lib.json and overrides the path for library-1 from the root tsconfig to use the compiled code of that library. This would build library-2 with compiled sources and not just mingle it and directly import from library-1.

{
  "extends": "./tsconfig.lib.json",
  "compilerOptions": {
    "paths": {
      "library-1": ["dist/libs/library-1"]
    }
  }
}

4.Use the following command to build library-2 so the new tsconfig.build.json is used.

ng-packagr -p projects/library-2/ng-package.json -c projects/library-2/tsconfig.build.json
Therianthropic answered 5/11, 2019 at 13:16 Comment(0)
O
1

I had the same error we had on tsconfig.json

 "paths": {
      "dxp-lib-commons": [
        "projects/dxp-lib-commons/src/public-api.ts"
      ],
      "dxp-lib-components": [
        "projects/dxp-lib-components/src/public-api.ts"
      ],
      "dxp-lib-services": [
        "projects/dxp-lib-services/src/public-api.ts"
      ]
    },

So commons it is the base for the other two, i had pointed to the public-api,ts on develop and use it fast. Yet when we did a build on this, it will crash with 'rootDir' is expected to contain all source files.

So after looking several posts i discovered that on build it is necesary to point to the dist and compiled commons not the public-api.ts so on components and service i added on the tsconfig.lib.prod.json

"paths": {
  "dxp-lib-commons": [
    "dist/dxp-lib-commons"
  ]
}

And worked.

Otic answered 5/10, 2021 at 2:36 Comment(0)
I
0

Yes you can specify the first project as a dependency on the second project package.json file:

"dependencies": {
    "Local first project": "file:../myprojectfolder/myprojectpackagefolder"
    "Git First project": "git+ssh://[email protected]:first-project.git" 
}
Irreclaimable answered 18/2, 2019 at 13:19 Comment(1)
Yes, but if it is not there (the dependency) it will still succeed. Maybe that is an intended way of working of the libraries - all libraries in the same project can be imported in one another?Therianthropic

© 2022 - 2024 — McMap. All rights reserved.