Reputation: 180
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?
Upvotes: 5
Views: 3590
Reputation: 1445
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.
Upvotes: 1
Reputation: 180
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
Upvotes: 6
Reputation: 1151
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"
}
Upvotes: 0