Search code examples
yarnpkgyarn-workspacesyarnpkg-v2

Workspace-global bin-scripts in Yarn 2


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.


Solution

  • 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",