繁体   English   中英

将Typescript类发布为npm包

[英]Publish Typescript classes as npm package

我编写了一组Typescript类,我将其作为NPM模块发布到我们的内部包服务器。 我的其他项目能够使用NPM检索此依赖项。

我想以这样的方式发布类,我可以通过引用包本身来导入它们 ,类似于我使用的其他一些包,如Angular。

import { SomeClass } from 'mylibrary'

这是我目前的设置:

我写了一个./mylibrary.d.ts文件:

export * from './dist/foldera/folderb'
//etc

./package.json

"name": "mylibrary",
"main": "mylibrary.d.ts",
"files": [
  "mylibrary.d.ts",
  "dist/"
],
"types": "mylibrary.d.ts",
//ommitted other settings

src/tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "../dist/",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": true,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

当我尝试使用所需的语法导入包时,MS VS Code说一切正常,但运行我的项目会导致一个相当神秘的错误消息:

Module build failed: Error: Debug Failure. False expression: Output generation failed

我对模块,命名空间和包的术语感到迷茫。 任何帮助是极大的赞赏。

如果你将index.ts文件放在包的根文件夹中(与package.json相同),那么它会重新导出src /文件夹中的所有功能,而不是在不构建js的情况下发布它,保持请记住,它只会用在TS项目中。

这种方法的消极方面是,你的包将每次都与你的项目一起编译,并且TS环境中可能的变化可能导致无法编译整个项目(我在编译我的一个软件包<2.4.2时遇到问题, 例如)。

// index.ts
export { Class1 } from 'src/Class1'
export { Class2 } from 'src/Class2'

暂无
暂无

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

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