This is a project to learn and setup a lerna project in public github. The idea is to be able to automatically publish packages using Github and Travis.
The following are the steps I've been doing to set up this project, for future reference.
- Create a new folder for the project
mkdir @d13z-lerna && cd @d13z-lerna
- Initialise lerna with independent version
npx lerna init --independent
(this may take a while) - Rename the root package.json name field to
@d13z-lerna/root
- Run
npm install
in root - Create a new package using
lerna create library-a
- Rename library in package.json from
library-a
to@d13z-lerna/library-a
- Create a new package using
lerna create library-b
- Rename library in package.json from
library-b
to@d13z-lerna/library-b
- Import
@d13z-lerna/library-b
into@d13z-lerna/library-a
- Add and script to package.json to run lerna bootstrap
"lerna:bootstrap": "lerna bootstrap",
- Now the packages are symlinked and we can use the library-b in the library-a.
Let's start adding a .travis.yml file in the root folder. Travis will run on every
push, and if the push is in master and with a tag, it will run lerna:publish
.
- Add .travis.yml
- Add a bash script to execute it when a git tag is pushed to the repository
- Create a new
GITHUB_API_TOKEN
environment and upload it to travis - Update the test script to
"test": "echo \"Error: no test specified\" && exit 0"
for now (because we don't have tests) - Setup a new
NPM_TOKEN
for travis and upload it
This steps will make travis to release a new version every time you push to master and there is changes, effectively publishing your library.
Once we have the automatic release of our project, we will start adding some cool stuff to make our project maintainable, starting by Typescript.
- Add TypeScript to the project root with
npm install -D typescript
- Lets create a
tsconfig.json
in the root with the commandnpx tsc --init
- Optional: Change the configurations of your tsconfig.json to fit your needs
- Add a new
tsconfig.json
inlibrary-a
andlibrary-b
will extend the root one, like this - Add a new
build
script in each package.json which will run the typescript compilation"build": "tsc"
- Add a new
build
script in the root package.json that runs the following command"lerna:build": "lerna run build"
, this will run the command in every package - Update .travis.yml to execute the build step
To avoid having inconsistent code, we can use nice tools like ESLint (static checking) and Prettier (styling).
- Add the libraries on the root with
npm install -D eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
- Add a .eslintrc.js file to the root, this file will have the rules for ESLint
- Add the prettier libraries to the root with:
npm install -D prettier eslint-config-prettier eslint-plugin-prettier
- Add a .prettierrc.js file to the root. This file will have the formatting rules
- Add a new script task to the root package.json to run eslint and prettier:
"lint": "eslint '*/**/*.{js,ts}' --quiet",
"lint:fix": "npm run lint --fix",
This will run eslint in the specified files. The lint:fix
variation will try to fix the issues.
On a CI environment, we should run the lint without fix.
- Update .travis.yml to execute the lint step
Now we can keep our environment consistent over time (at least on the way we write the code).
Rollup is a very good bundler, specially for libraries. So we are going to install it and make Rollup handle the bundling instead of TypeScript. This is useful if you are going to add more libraries to the mix or styles.
- Install rollup in the root of the project with
npm install -D rollup
- Install the other libraries
npm install -D @rollup/plugin-alias rollup-plugin-analyzer @rollup/plugin-typescript @rollup/plugin-commonjs @rollup/plugin-node-resolve @rollup/plugin-replace rollup-plugin-terser
- Create a rollup directory rollup this directory will hold the base rollup config
- Create the base config with the base configurations.
- Install the following dependencies to manage some minor cleaning stuff
npm install -D cpx rimraf
- Change the scripts of the libraries to match this
- Create a
tsconfig.d.json
, this will be used only to generate the type definitions