0

Our Angular project has evolved to have a bunch of functions that don't really belong in any class (in addition to all the normal classes of course). We now need a new Angular project that wants to also use some of these functions. These are all private and will never be in a public repo

I've made a new project and am using it but all the imports look like

import { func1 } from "common-library"

i.e. there's no concept of a namespace here.

How do I structure this such that I end up with something like

import { func1 } from "common-library/functionset1"
import { func1 } from "common-library/functionset2"

And ditto for the classes. I want to to avoid the possibly of a class with a generic name like Item causing a conflict in the future

4
  • I've made a new project Could you elaborate? Did you follow the steps in the docs to create a new Angular library, based on ng-packagr? Commented Oct 17, 2024 at 10:46
  • I can't see anything in that doc to answer my specific question. The guide I followed was dev.to/jsanddotnet/…. To be clear, the library was created using "ng generate library ..." Commented Oct 17, 2024 at 10:49
  • ng generate library creates a library based on ng-packagr. What you are looking for are secondary entry points: github.com/ng-packagr/ng-packagr/blob/main/docs/… Commented Oct 17, 2024 at 10:56
  • That does look like what I'm after. I'll read up and play with that before I go any further. Thanks! Commented Oct 17, 2024 at 10:59

1 Answer 1

1

You can achieve this with secondary entry points. To create one, make sure to follow this folder structure:

common-library
├── src
|   ├── public_api.ts
|   └── *.ts
├── ng-package.json
├── package.json
└── functionset1
    ├── src
    |   ├── public_api.ts
    |   └── *.ts
    └── ng-package.json
└── functionset2
    ├── src
    |   ├── public_api.ts
    |   └── *.ts
    └── ng-package.json

The content of the ng-package.json file inside functionset1 and functionset2 can be simply {}.

Also make sure that your common-library/src/public_api.ts file exports something, even if you don't have anything to export. Otherwise there will be a build error, see this GitHub issue. For example:

export const emptyPackage = true;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.