Search code examples
angulartypescriptnrwl-nxng-packagr

Angular library: make local TypeScript dependency part of the compilation


I have a Angular lib inside a Nx workspace...

The lib consumes code from another local lib for some shared TS code.

The shared libs path is defined in the tsconfig paths setting:

"paths": {
    "@myOrg/sharedLib": ["libs/shared/src/index.ts"],
}

Code is imported from the shared lib via TypeScript imports like this.

import {someUtilFn} from '@myOrg/sharedLib'

The question is: How can I make sure, that the code of the shared lib, becomes part of the built / compiled Angular library? Till now the built Angular library only contains its own code, skipping external dependencies. The user of the Angular lib (who installs it e.g. via npm) will be forced to install the shared lib was well.

Because the Angular lib is going to be installed via npm... it would be possible to publish the shared lib as well and make it a dependency / peer dependency of the Angular library. But I want to avoid that the user of the Angular lib can import code from the shared lib.


Solution

  • updated answer

    only application flatten the dependencies, but libraries doesn't there is a discussion here https://github.com/nrwl/nx/issues/4829 but the nx teams seems not doing it

    so your option is to publish the common library to npm, and add it as a dependency in your package.json not peer dependency

    this will make sure when the user download/update the main library it will download the other one as well

    the difference between them is that peer dependency is something the end-user will be using and also your library, but a dependency is something that the user won't be using