Scaffoling for setting up an open source libarary with the following stack
- code in JS/TypeScript
- Build with Rollup
- Test with Jest
- Publish to npm with CircleCI
Create a new directory and then run:
curl -fsSL https://github.com/cirbuk/jslib-scaffolding/archive/master.tar.gz | tar -xz --strip-components=1
For the discussion below, assume that the code resides in the src
folder and the entry point is index.ts
/index.ts
.
- Change the following fields
name
,version
,description
,repository
,author
,license
,bugs.url
andhomepage
as required - Change/remove
main
as per requirement. - Change/remove
module
as per requirement. - Change/remove
browser
as per requirement. - Run
npm install
- Add additional packages required by the library
types
inpackage.json
should point to the types declaration file generated for the library.- Write code in
index.ts
insrc
folder - Change the
tsconfig.json
file as required - Change all
input
inrollup.config.js
tosrc/index.ts
- Remove
types
and packages related to TypeScript fromdevDependencies
inpackage.json
- Remove
tsconfig.json
- Remove the entry
@babel/preset-typescript
from.babelrc
- Write code in
index.ts
insrc
folder - Remove
import typescript from 'rollup-plugin-typescript';
inrollup.config.js
- Remove
typescript()
call fromplugins
inrollup.config.js
- Change all
input
inrollup.config.js
tosrc/index.ts
If you have other rollup built libraries as dependencies, and you use some named exports from those libraries, these imports will have to be explicitly declared in your rollup.config.js as named exports. If these dependencies are peer dependencies, you can mark them as external in your rollup.config.js and avoid having to declare all named imports.
npm run build
will build the library
- Write your tests in the
tests
directory as files ending in.test.js
- Change the Jest config as required in
jest.config.js
npm test
will run the tests and print the results
- Login into Circle CI and follow the github project where the library resides
- Change the node version in
.circleci/config.yml
if required - Do
npm login
in local and extract the auth token from${HOME}/.npmrc
- The token can be configured in 2 ways
- In the Circle CI organization settings, create a context named
npm
and add and environment variable namednpm_TOKEN
with the copied auth token as it's value. This is the default approach configured in.circleci/config.yml
. - Otherwise, create an environment variable
npm_TOKEN
in Circle CI under project settings and paste the copied auth token as it's value. Afterwards, delete the linecontext: npm
(line 68) from.circleci/config.yml
- In the Circle CI organization settings, create a context named
Once all the above steps have been completed and the library is ready, commit to github.
- The tests will be run in CircleCI for every branch and tag
- Publish to npm will happen whenever a commit is tagged with
v${versionNumber}
.
Before tagging a commit for publish, make sure that the correct version number is updated in
package.json