Seeking advice on properly developing a reusable commonjs library (npm package). Let's consider the library structure as shown below:
—lib
——Helper.ts
——Serialization.ts
——meta
——— Entity.ts
—index.ts
Each file serves as an external module.
My dilemma lies in creating an entry file (such as index.ts) that not only exposes the functionality of the library but also acts as the entry point. I aim to maintain the nested structure without flattening it through exporting everything:
export * from './lib/Helper’;
export * from './lib/Serialization’;
export * from './lib/meta/Entity’;
…
This approach, however, may eliminate logical grouping and potentially result in name conflicts down the line.
I've attempted another method with an index.ts like this:
import {Helper} from './lib/Helper';
import * as Serialization from './lib/Serialization';
import * as Decorators from './lib/meta/Decorators';
let core = {
Helper: Helper,
Serialization: Serialization,
Meta: {
Entity: Entity,
}
}
export default core;
While this solution functions smoothly, issues arise when trying to access imported types like so:
import Core from ’nameOfTheNpmPackage';
let Entity = Core.Meta.Entity;
function f(e: Entity)//<—This produce error cannot find name ‘Entity'
{
}
Since these types are not within the type declaration space, is there a way to include them there if they aren't in a namespace?
One other approach I experimented with involved generating a single d.ts file for the entire library using outFile + amd. However, this file did not become an external module.
Hence, my question remains - how can I write an index.ts to be an external module while exporting all functionality exposed by the library?
Many thanks.