title: Project Setup: Creating a Typescript library package for nodejs using npm and webpack
body: <p>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:</p>
  
  <pre><code>├── 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
  </code></pre>
  
  <p>And all the types exported by <code>ts/Lib.ts</code> would be exported to a single <code>.d.ts</code> in the <code>lib</code> directory to sit next to the js bundle. </p>
  
  <p>I've looked at the following questions/sources:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/30928253/writing-npm-modules-in-typescript">Writing npm modules in typescript</a></li>
  <li><a href="https://stackoverflow.com/questions/29217451/how-to-create-a-typescript-library">How to create a typescript library</a> (<a href="https://stackoverflow.com/questions/21233108/cross-project-references-between-two-projects">and the question it duplicates</a>)</li>
  <li><a href="https://stackoverflow.com/questions/38950769/how-to-create-a-typescript-library-which-has">This unanswered question</a></li>
  <li><a href="https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html" rel="nofollow noreferrer">The offical typescript guide to creating packages</a></li>
  <li><a href="https://github.com/basarat/ts-npm-module" rel="nofollow noreferrer">This example typescript library project</a></li>
  <li><a href="https://stackoverflow.com/questions/41372014/publishing-a-typescript-library-on-npm-exported-symbols-modules">And another SO question</a></li>
  </ul>
  
  <p>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 <code>.d.ts</code> file that maps to what webpack creates. However, I want to avoid creating the <code>.d.ts</code> file manually - it should be possible to automatically extract the types without having manually created <code>.d.ts</code> files get out of sync with my source code. Is there a way of doing this?</p>