Write a library using TypeScript library starter
Alex Jover Apr 01, 2017
Explained in a video lesson on Egghead
Writing a library or project always requires some work:
- Prepare configuration: package.json, tsconfig.json, CI, bundling, build, minification, types generation...
- Testing tools: Unit test (with watch mode), coverage...
- Release versions and changelog: This is a repetitive work that takes some time
- API Docs generation
Typescript library starter solves all of that and more, plus:
- Uses latest tools: RollupJS (with tree-shaking), Prettier, Jest, TSLint, Typedoc...
- Multiple bundle generation, as described in 2ality
- Focused on automation: with a set of scripts you as a user have all the work done
- Allows automatic releases to Github and Npm by using and configuring Semantic Release
- Is already configured to work on Travis, Coveralls, generate types, and more!
Let's see how easy is to use it.
Let's start by running:
git clone https://github.com/alexjoverm/typescript-library-starter.git cd typescript-library-starter npm install
A prompt will show up asking your library name:
When you do so, it will configure the project for you based on the library name and your git config. It re-inits the git directory, configures rollup, package.json and your entry files (both src and test).
See it yourself! Try running
npm t, it will run a sample test. Or run
npm run build, it will create a
dist folder with the bundle, typings file and documentation.
See all the commands available on the repo Readme
Now start coding. You'll see when you make a commit, your code is automatically formatted! That's thanks to Prettier :)
Automatic releases and Travis CI
We need to do some previous steps. First, make sure you've created an account on:
Also, make sure you've set the
repository.url property on your
Now, run the following script:
At this point, you just need to install semantic release. Answer NO to the "Generate travis.yml" question, because it is already prepared for you.
npm install -g semantic-release-cli semantic-release setup
From now on, better use
npm run commit. It will show you a prompt which is very convenient for creating a conventional commit. Note that running
git commit -a without the proper commit message will fail.
You'll notice that when you run
git push, the exactly same command that Travis CI runs is run locally on your machine, so you can see early if the build will fail.
We haven't dived into any kind of configuration, but we were able to not only write a dummy library, but also make sure all quality steps (linting, testing, coverage) are asserted and that we can create bundles, builds, typings and all things needed for a TypeScript library.
Not only that, but also how to make maintainability easy by automating releases!