简体   繁体   English

如何在NodeJS中公开TypeScript模块?

[英]How to expose TypeScript modules in NodeJS?

I have a library that I'm building in TypeScript. 我有一个我在TypeScript中构建的库。 I'd like to include this library in both TypeScript and JavaScript Node projects. 我想在TypeScript和JavaScript Node项目中包含这个库。 What is the general strategy to do this? 这样做的一般策略是什么? Should I compile and have two versions or is there some other strategy I should be using? 我应该编译并有两个版本,还是我应该使用其他一些策略?

I'd like to include this library in both TypeScript and JavaScript Node projects. 我想在TypeScript和JavaScript Node项目中包含这个库。 What is the general strategy to do this? 这样做的一般策略是什么?

Compile with the following to get the js output: 使用以下内容编译以获取js输出:

  • --module commonjs --outDir ./dist

This should make your project consumable by JS projects. 这应该使您的项目可以被JS项目消耗。 To make it consumable by TS projects you need to generate a declaration file . 要使TS项目可以使用它,您需要生成一个声明文件 This can be done using https://github.com/SitePen/dts-generator See usage for details : https://github.com/SitePen/dts-generator#usage 这可以使用https://github.com/SitePen/dts-generator完成。详情请参阅用法: https//github.com/SitePen/dts-generator#usage

Note: There is discussion on removing the dts-generator dependency : https://github.com/Microsoft/TypeScript/issues/2338 注意:有关于删除dts-generator依赖关系的讨论: https//github.com/Microsoft/TypeScript/issues/2338

In most cases I have seen you have a /dist/ folder where the compiled JavaScript is located. 在大多数情况下,我看到你有一个编译JavaScript所在的/ dist /文件夹。

Usually there is also a minified version like yourfilename.min.js - the rest is either outside or in a /src/ folder, so outside you have only the license/readme.md, package.json left and maybe the file for Grunt/Gulp (it is considered polite to - if you use a taskrunner - include a Grunt/Gulp file for compiling the typescript and minify the .js file afterwards, as far as I got it) 通常还有一个像yourfilename.min.js这样的缩小版本 - 其余的都在外面或在/ src /文件夹中,所以在你外面你只有license / readme.md,package.json,也许是Grunt /的文件Gulp(如果您使用taskrunner,它被认为是礼貌的 - 包括一个Grunt / Gulp文件,用于编译打字稿并在之后缩小.js文件,据我所知)

If you want to preserve the TypeScript advantages when using it in TypeScript projects, then you obviously have to expose a TypeScript version of the code so the TypeScript compiler can see the TypeScript declarations for your interface. 如果要在TypeScript项目中使用它时保留TypeScript优势,那么显然必须公开TypeScript版本的代码,以便TypeScript编译器可以看到您的接口的TypeScript声明。

But, if you want people to be able to use your library in plain Javascript projects (that don't compile TypeScript into JS), then you have to offer a version that has a plain Javascript interface and where the code has already been compiled into plain JS. 但是,如果你希望人们能够在普通的Javascript项目中使用你的库(不能将TypeScript编译成JS),那么你必须提供一个具有普通Javascript接口的版本,并且代码已经被编译成了普通的JS。

So, if you want both of those advantages, then you have to offer two separate versions. 因此,如果您想要这两个优点,那么您必须提供两个单独的版本。 The plain JS version can obviously just be a compiled version of the TypeScript (compiled into plain JS). 简单的JS版本显然只是TypeScript的编译版本(编译成普通的JS)。

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

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