Search code examples
angularangular-materialangular-cling-packagr

How to only have secondary entry points in ng-packagr


I have an Angular CLI library where each component should only be imported through their individual entry-points (like Angular Material) :

import {Foo} from '@myLib/foo
import {Bar} from '@myLib/bar

So I don't want a main entry point in ng-packagr config.

I already have all the secondary entry-points defined and I want to remove the primary entry-point so users cannot just "import {Bar} from '@myLib", but ng-package.json requires an entryFile and when I leave the entryFile empty I get an error

ERROR: Internal error: failed to get symbol for entrypoint

I have to add at least one valid export in the the entryFile.

The Angular Material team seem to have got this right - https://github.com/angular/components/blob/master/src/material/index.ts

Any ideas on how to do this?


Solution

  • I'm not sure if this scenario is supported.

    I'm using ng-packagr for a similar setup and what seems to work (although not pefect) is the below:

    if you have the following structure:

    @mylib
    ├── src
    |   ├── public_api.ts
    |   └── *.ts
    ├── ng-package.json
    ├── package.json
    └── foo
    |   ├── src
    |   |   ├── public_api.ts
    |   |   └── *.ts
    |   └── ng-package.json
    └── bar
        ├── src
        |   ├── public_api.ts
        |   └── *.ts
        └── ng-package.json
    

    if you set your root package.json with the name of the scope

    {
      "name": "@myLib"
    }
    

    you set your root ng-package.json to this

    {
      "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
      "dest": "../../dist/@myLib"
    }
    

    and put an empty object export in your main public_api.ts (that is /src/public_api.ts)

    export default {};
    

    then you should get the structure you are looking for

    hope it helps