最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to Share Build Scripts In Lerna Packages - Stack Overflow

programmeradmin1浏览0评论

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 plicates CI).

Is there a better way to share these mands amongst all my packages?

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 plicates CI).

Is there a better way to share these mands amongst all my packages?

Share Improve this question edited Dec 5, 2018 at 12:21 Undistraction asked Dec 3, 2018 at 19:23 UndistractionUndistraction 43.4k63 gold badges206 silver badges336 bronze badges 2
  • have you stumbled on this yet? github./Cosium/dry-dry – Tiago Coelho Commented Dec 11, 2018 at 23:41
  • 1 @TiagoCoelho Thanks for the link. That looks interesting. – Undistraction Commented Dec 12, 2018 at 12:30
Add a ment  | 

1 Answer 1

Reset to default 6 +50

Package all the build scripts into their own module and then use lerna --hoist to host the mon module so it is installed once but available to all the other packages.

发布评论

评论列表(0)

  1. 暂无评论