I have a lerna repo that contains multiple packages organised in the usual structure:
package.json
/packages
- alpha
package.json
- bravo
package.json
- charlie
package.json
I need to transpile all packages, and I currently have the following scripts in each package's package.json
:
"build": "npm run build:noWatch -- --watch --verbose",
"build:noWatch": "babel src --out-dir lib --root-mode upward --ignore '**/*.test.js','**/__tests__'",
"prebuild": "rimraf lib/*"
I currently run a build using:
lerna run build --stream --parallel
However I don't want to duplicate these scripts for every package. I would like to define the scripts in a single place, but use them from all packages. I currently have linting scripts and testing scripts in my root package which make sense there as they are effectively traversing the whole monorepo looking for tests, or files to lint. It doesn't seeem to make sense to move the build scripts up there as well as they are scoped to the individual packages, and I like the fact that I get different colour output for each package when I use lerna run
.
An unsatisfying solution is to create some shell scripts in the root of the monorepo and call them from the packages' package.json
files:
In root/packages/example/package.json
:
"scripts": {
"build": "../../scripts/build.sh",
"build:noWatch": "../../scripts/build.sh",
"prebuild": "../../scripts/prebuild.sh"
},
Then in root/scripts/build.sh
:
#!/bin/sh
babel src --out-dir lib --root-mode upward --ignore '**/*.test.js','**/__tests__' --watch --verbose
Whilst this works, it doesn't feel right: it still involves duplication between the packages and requires setting permissions on the shell scripts (which complicates CI).
Is there a better way to share these commands amongst all my packages?
The Original Tool forJavaScript Monorepos Lerna is a fast modern build system for managing and publishing multiple JavaScript/TypeScript packages from the same repository.
Lerna is a popular and widely used tool written in JavaScript for setting and managing multi-package repositories for Node. js projects with npm and Git. Lerna has two modes: fixed and independent.
lerna create is a command to add a new package to be managed in Monorepo. To install an npm package, we can use the yarn workspace command in the root directory or the yarn add package-name command in the package directory where you'd like to install the package. Yarn updates package. json for the package and yarn.
Package all the build scripts into their own module and then use lerna --hoist
to host the common module so it is installed once but available to all the other packages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With