publishing a typescript library on npm: exported s

2019-07-20 04:20发布

I'm writing a typescript library which I intend to publish on npm. It is meant to run in node only, never in the browser. It will make sense to use the library only from typescript (I'm not expecting any Javascript users).

I have trouble finding guides how to publish typescript-for-typescript libraries on npm (also, these things seem to be changing rapidly in the typescript world).

Should I wrap all my code in a module? (I don't feel the need for modules in the library itself, it's currently only at around 1000 LOC). Should I create an index.ts file? I think I should ship .js, .js.map and .d.ts files but no .ts files for the npm package?

How do i call functions between files in the library without exporting them to users of the library? I currently don't use typescript modules. I tried using typedoc and it lists also symbols that I exported from individual files for the purpose of using in another file of the library. But I don't want these to be visible to the users of library.

Is there a library I could use as an example? I looked at typescript-collections, they don't use any module and have an index.ts. I think they export all their shared functions, so that doesn't help me with that issue.

2条回答
放我归山
2楼-- · 2019-07-20 04:53

I'd suggest you to take a look at TypeScript Library Starter. It configures out of the box:

  • Package.json configuration
  • Universal module bundles
  • Source Maps
  • Typings (.d.ts)
  • Docs using TypeDoc
  • Tests and coverage
查看更多
甜甜的少女心
3楼-- · 2019-07-20 04:54

What I did was having the index.ts export all files.

like

export { default as Actions } from './Actions'

then, in you package.json, add property "typings": "dist/index.d.ts" (change the location accordingly)

and yes, export definitions and source-maps. others can use source-map-support package to debug it easily later on.

I prefer pre-build them into .js when publishing.

查看更多
登录 后发表回答