converting an Angular 9 module into a library

Viewed 308

I have a large, Angular 9 application that is currently running on a production server.

I've been tasked to break apart the application into a main app shell with pluggable components.

Let's say I have this tree structure:
|-- main
    |-- core
        |
        |-- abstract
            |-- models
                |-- mapped-property-class.ts
                |-- json-objects.ts
                |-- abstract-model.ts
            |-- components
                 |-- abstract-base-component.ts
        |-- components //implement abstract-base-component
        |-- services

I want to generate a library that contains this same structure.

All of the blogs and tutorials on generating libraries end up with a folder containing a src/lib folder and a public-api.ts file inside the src folder wherein I export every, single .ts file from within the lib folder.

I've yet to read an article that completes a sample library from which I can:

import {MappedPropertyClass, JsonObject} from '@core-lib/models/abstract';

I see inside the node_modules folder that this can be done, so I just need any information that will start me on the right path to structuring libraries.

I'd like to start this correctly, so I wont have to dismantle and refactor the main app when It's time to plugin the library modules.

Any help would be appreciated.

Thanks...

1 Answers

Well, I finally found the answer.

It's not pretty, but it works.

Base on this blog: [The Best Way to Architect Your Angular Libraries][1]

Three files must be added to each folder you want to export:

package.json
index.ts
public-api.ts

In public-api.ts, export every code file you want to share:

export MappedPropertyClass from './mapped-property-class';
export JSONObject from './JsonObject';

In index.ts export the contents of public-api.ts.

export * from './public-api';

So, In my above list these file would be placed in abstract/models and abstract/components.

Now, I am able to import in my test app:

import { MappedPropertyClass } from '@soccs/root/core/lib/abstract/models';
Related