0

I have a yarn 2 workspaces project with two workspaces:

|-foo
\-bar

Now, in the root package.json, I pull in common dev-depenencies:

"devDependencies": {
    "@rollup/plugin-commonjs": "^14.0.0",
    "@rollup/plugin-node-resolve": "^8.4.0",
    "@rollup/plugin-replace": "^2.3.3",
    "@rollup/plugin-typescript": "^5.0.2",
    "@types/jest": "^26.0.13",
    "nollup": "^0.13.10",
    "rimraf": "^3.0.2",
    "rollup": "^2.23.1",
    "ts-jest": "^26.3.0",
    "tslib": "^2.0.1",
    "typescript": "^4.0.2"
  }

How can I easily (without too much boilerplate) now reference rollup, etc. from scripts in the package.json of foo and bar?

Example: foo/package.json

"build": "rollup ...",

Writing "../node_modules/.bin/rollup" sucks.

Note, I don't want to install rollup etc globally.

RobC
  • 22,977
  • 20
  • 73
  • 80
user3612643
  • 5,096
  • 7
  • 34
  • 55

2 Answers2

10

To run executable installed in root workspace, you can say:

"build": "run -T rollup",

-T is for --top-level and is not currently documented anywhere. I was informed about its existence on Yarn discord server by one of the maintainers.

https://github.com/yarnpkg/berry/blob/bef203949d9acbd42da9b47b2a2dfe3615764eaf/packages/plugin-essentials/sources/commands/run.ts#L47-L49

raine
  • 1,694
  • 17
  • 14
  • 1
    This is now mentioned in yarn 2 documentation: https://yarnpkg.com/cli/run/#options-T%2C-top-level – Fab313 Oct 18 '21 at 14:19
0

So, I found a not-too-bad solution. In the workspace root, I add some executable files for the commands I want to use in my scripts, e.g.:

tsc:

#!/bin/bash
$(dirname "${BASH_SOURCE[0]}")/node_modules/.bin/tsc -b -f "$@"

rollup:

#!/bin/bash
$(dirname "${BASH_SOURCE[0]}")/node_modules/.bin/rollup "$@"

These basically "forward" the call to the actual binaries and may add some common parameters.

In my foo/bar package.json, I can now reference those scripts:

    "dev:compile": "../tsc",
    "build": "../tsc && ../rollup -c rollup.config.js",
user3612643
  • 5,096
  • 7
  • 34
  • 55