Tuesday, 25 April 2017

Project Setup: Creating a Typescript library package for nodejs using npm and webpack

I want to create a library in Typescript that I can share via npm. Specifically, I want to use webpack to generate a js bundle along with a definition file to share the types with the js. So I'd have a tree of files like:

├── lib
│   ├── lib.d.ts
│   └── lib.min.js
├── test
...
├── ts
│   ├── errors
│   │   ├── CannotModifyAlteredObject.ts
│   ├── Lib.ts
│   ├── PostProcessors.ts
│   ├── Serializers.ts
├── tsconfig.json
├── typings.json
├── LICENSE
├── package.json
├── README.md
└── webpack.lib.config.js

And all the types exported by ts/Lib.ts would be exported to a single .d.ts in the lib directory to sit next to the js bundle.

I've looked at the following questions/sources:

However, none of these provide an example using webpack. Being able to bundle everything you need to use the library (apart from the nodejs runtime) into a single file is pretty important for my use case, so webpack fits this role well. I'd like to be able to generate a .d.ts file that maps to what webpack creates. However, I want to avoid creating the .d.ts file manually - it should be possible to automatically extract the types without having manually created .d.ts files get out of sync with my source code. Is there a way of doing this?



via Orphid

No comments:

Post a Comment