Skip to content

Instantly share code, notes, and snippets.

@GongT
Last active September 3, 2018 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GongT/4a6ae2c10fb4cae4c09b78c63b86e30a to your computer and use it in GitHub Desktop.
Save GongT/4a6ae2c10fb4cae4c09b78c63b86e30a to your computer and use it in GitHub Desktop.
Create A Library With Typescript And Rollup

You want:

  1. write some code as library
  2. split your code into many files

Then you can just write every file standalone, compile them all (to commonjs format), and others can use. perfect.

If you also want:

  1. manage imports using absolute path in your module (eg. import xxx from "vs/code/workbench")
    • without requires user mock require call to handle absolute import path
  2. remove "src" or "out" from import path
    • this can be done by place package.json in the dist folder

Then you must do some job, there's two ways:

  1. handle compile process, translate all import path to relative
  2. write a index.ts and export everything to user

If you want the fiest way, you can press Ctrl+W now.

To automatically create a index.ts

Need to do:

  1. load a tsconfig to know what to include
  2. read every file, get exported symbols
  3. resolve file path to index.ts, (re)export symbols from that file
  4. have a way to only compile index.ts (tsconfig is used to find all files)

1,2 and 3 can be done with typescript api.
4 can be done by tsc cli

just create a new tsconfig.index.json next to the tsconfig.json, write:

{
	"extends": "./tsconfig.json",
  "files": ["_index.ts"]
}

But it's better to do with rollup. although it's harder to configure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment