Commonjs库的index.ts作为外部模块 [英] Commonjs library's index.ts as external module

查看:160
本文介绍了Commonjs库的index.ts作为外部模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些指导来正确开发可重用的commonjs库(npm包)。
可以说库结构如下:

I need some guidance to correctly develop reusable commonjs library (npm package). Lets say the library structure is the following:

—lib
——Helper.ts
——Serialization.ts
——meta
——— Entity.ts
—index.ts

依此类推。每个文件都是外部模块。

And so on. Each file is external module.

现在,我需要创建入口文件(例如index.ts),该文件将暴露库的功能并将其用作入口点。
在这里,我无法束手无策。我想通过输出所有内容来保持嵌套结构而不使其变平:

Now I need to create entry file (say index.ts) that will expose functionality of the library and will serve as entry point. Here I can’t wrap my head around the way to create it. I would like to keep the nested structure without flattening it by exporting everything:

export * from './lib/Helper’;
export * from './lib/Serialization’;
export * from './lib/meta/Entity’;
…

原因这将删除逻辑分组,并且将来可能导致名称冲突

Cause this will remove logical grouping and can in the future lead to possible name conflicts

我也尝试过使用index.ts:

I have also tried to have 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;

它很完美,直到我开始使用这样的导入类型:

It works perfect until I come to consume imported types like this:

import Core from ’nameOfTheNpmPackage';

let Entity = Core.Meta.Entity;

function f(e: Entity)//<—This produce error cannot find name ‘Entity'
{

}

由于它们不在类型声明空间中(如果它们不在命名空间中,是否可以将它们复制到那里?)

As they are not in the type declaration space (is there a way of copying them there if they are not in namespace?)

我尝试过的另一个选项是通过使用outFile + amd为整个库生成单个d.ts文件。但是该文件不是外部模块。

Another option I have tried is generating single d.ts file for the whole library by using outFile + amd. But that file is not an external module.

所以我的问题是-如何编写index.ts,使其成为外部模块并导出所有公开的功能

So my question is - how is it possible to write index.ts so that it will be external module and export all functionality exposed by library.

预先感谢。

推荐答案

浪费时间,我想出了丑陋的解决方案。
要保留导出的commonjs库的嵌套结构,您必须在每个级别内放置其自己的 index.ts ,以聚集并重新导出相同水平'。对于上面的示例,它看起来像:

One day of wasted time and I've come up with ugly solution. To keep nested structure of the exported commonjs library you must inside each 'level' put its own index.ts that will aggregate and reexport all functionality of the same 'level'. For my sample above it will look like:

-lib
——Helper.ts
——Serialization.ts
-元
——— index.ts
——— Entity.ts
-index.ts

其中的根index.ts:

Where root index.ts:

export * from './lib/Helper';
export * from './lib/Serialization';

import * as S from './lib/Serialization'; //This is case where one file contains several exported classes that we want to keen as separate module
export {S as Serialization};

import * as Meta from './lib/meta/index';
export {Meta};

和meta / index.ts:

And meta/index.ts:

export * from './Entity';

我真的很高兴知道是否可以在没有额外文件的情况下解决这个常见任务?每个级别。

I would be really glad to know if this common(?) task is possible to solve without extra files per 'level'.

这篇关于Commonjs库的index.ts作为外部模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆