简体   繁体   English

如何导出 Typescript 库中的子模块

[英]How to export a submodule in a Typescript library

So my goal is to create a library in Typescript.所以我的目标是在 Typescript 中创建一个库。 My intention is to split up core parts of the library into submodules like RxJS or Angular Material.我的目的是将库的核心部分拆分为子模块,如 RxJS 或 Angular Material。

RxJS and Angular both support imports like so: RxJS 和 Angular 都支持这样的导入:

// RxJS
import { map, filter } from 'rxjs/operators';

// Angular
import { MatButtonModule } from '@angular/material/button';

However, I am unable to replicate this myself.但是,我自己无法复制这一点。

My goal is to do something similar and allow you to import a class with import { foo } from 'package/bar ;我的目标是做类似的事情,并允许您使用import { foo } from 'package/bar ;

I have looked at RxJS's source on Github and have tried replicating what they've done but it's not working.我在 Github 上查看了 RxJS 的源代码,并尝试复制他们所做的工作,但它不起作用。

The library compiles fine but when I go about importing it I always get a Cannot resolve dependency 'package/foo' error.该库编译得很好,但是当我开始导入它时,我总是收到Cannot resolve dependency 'package/foo'错误。 Meanwhile doing import { test } from package (without the submodule part) works completely fine.同时做import { test } from package (没有子模块部分)完全正常。

I've tried using paths in tsconfig to no avail.我试过在 tsconfig 中使用路径无济于事。 If that is the answer then I'm doing it wrong.如果那是答案,那么我做错了。

How do I go about doing this?我该怎么做?

In order to achieve that, you can create your "sub-modules" in different directories (although they are technically part of the same module), and create an index.ts on each of those directories exporting there whatever you want to publish.为了实现这一点,您可以在不同的目录中创建“子模块”(尽管它们在技术上是同一模块的一部分),并在每个目录上创建一个index.ts导出您想要发布的任何内容。

For example, I have done this in my NodeJs package Flowed , and you can for example do this:例如,我做这在我的包装的NodeJS流动且,你可以如这样做:

import { FlowManager } from 'flowed/dist/engine';

Where the corresponding index.ts file isthis one if you want to check.如果要查看,对应的index.ts文件就是这个

Although in my case I put also available all the stuff from the root, so the previous line would be equivalent to:尽管在我的情况下,我还从根目录中放置了所有可用的东西,因此前一行相当于:

import { FlowManager } from 'flowed';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM