Search code examples
angulartypescriptng-packagr

Angular library ng-packagr doesn't replace tsconfig paths with relative paths


I have an Angular workspace (v10.0.2) containing a library and an app (for testing the library).

Inside my projects/libname/src/lib/tsconfig.lib.json I have some paths:

"baseUrl": "./src",
"paths": {
  "@lib/*": ["lib/*"]
}

And I use it like this:

import { DateUtils } from '@lib/utils/date.utils';

I get this warning:

WARNING: No name was provided for external module '@lib/utils/date.utils' in output.globals – guessing 'date_utils'

AFAIK, this means the tsconfig paths aren't replaced so ng-packagr is considering it as an external module. When I looked into the dist folder I saw '@lib/utils/date.utils' still presents and is not replaced with the relative path. If I ignore the warning, when I serve the test app, it gives this error:

Cannot find module '@lib/utils/date' or its corresponding type declarations.

I've searched a lot and read lots of issues snd SO questions, found no solutions.


Solution

  • The only solution I found is to choose a prefix so that your paths will be exactly the same when the package is installed in node_modules

    For example, if your package name is xyz, you tsconfig will be:

    "paths": {
      "xyz/*": ["lib/*"]
    }
    

    I highly recommend using sub-entries as it not only solved this issue, but also it'll support tree shaking. This article will help