简体   繁体   English

使用Typescript创建Webpack捆绑库

[英]Creating a Webpack Bundled Library with Typescript

I'm trying to build a library of typescript components that will be consumed by another library that is also typescript based. 我正在尝试建立一个打字稿组件库,该组件将由另一个也是基于打字稿的库使用。 We are currently using webpack to accomplish our bundling, with ts-loader as the loader to consume the TS files. 我们目前正在使用webpack来完成捆绑,其中ts-loader作为使用TS文件的加载器。

I'm unsure of how to generate the .d.ts files for the example library below. 我不确定如何为下面的示例库生成.d.ts文件。 Typescript does build the .d.ts files (with declaration set to true) but it is one file per module where I need one .d.ts file that can be used by consumers. Typescript确实构建了.d.ts文件( declaration设置为true),但是每个模块一个文件,我需要一个供消费者使用的.d.ts文件。

https://github.com/raybooysen/typescript-webpack-example https://github.com/raybooysen/typescript-webpack-example

So my question is, is there a canonical example of how to accomplish this? 所以我的问题是,是否有一个规范的例子来说明这一点? Is there a way that typescript consumers can import these .d.ts files correctly? 有没有办法使打字稿使用者能够正确导入这些.d.ts文件? At the moment there seems to be very little documentation and examples on the net and would appreciate any help. 目前,网上似乎很少有文档和示例,希望对您有所帮助。

I eventually opened an issue on the Typescript Github page and they have confirmed this is not possible: 我最终在Typescript Github页面上打开了一个问题,他们已经确认这是不可能的:

https://github.com/Microsoft/TypeScript/issues/8372#issuecomment-217219512 https://github.com/Microsoft/TypeScript/issues/8372#issuecomment-217219512

So my question is, is there a canonical example of how to accomplish this? 所以我的问题是,是否有一个规范的例子来说明这一点? Is there a way that typescript consumers can import these .d.ts files correctly 有没有办法使打字稿使用者能够正确导入这些.d.ts文件

You generally do not want to use the bundle as a module consumer. 通常,您不希望将捆绑软件用作模块使用者。 That is a job of the person who uses the library as a final website artifact. 那是使用图书馆作为最终网站工件的人的工作。 This means you only have foo.ts / foo.js next to each other in your node module deployed artifacts. 这意味着在节点模块部署的工件中,只有foo.ts / foo.js彼此相邻。 These can be consumed as it is with the TypeScript compiler. 这些可以使用TypeScript编译器直接使用。

More 更多

https://basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html https://basarat.gitbooks.io/typescript/content/docs/quick/nodejs.html

dts-bundle works like a charm for this purpose. 为此, dts-bundle就像一个护身符。

Here is an example of how to set it up as a Webpack plugin: Typings in one bundle 这是一个如何将其设置为Webpack插件的示例: 打包输入

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

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