【问题标题】:Workspace-global bin-scripts in Yarn 2Yarn 2 中的工作区全局 bin 脚本
【发布时间】:2020-09-15 12:49:44
【问题描述】:

我有一个带有两个工作区的 yarn 2 工作区项目:

|-foo
\-bar

现在,在根 package.json 中,我引入了共同的开发依赖项:

"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"
  }

我现在如何轻松(无需太多样板)从 foo 和 bar 的 package.json 中的脚本中引用汇总等?

示例:foo/package.json

"build": "rollup ...",

写“../node_modules/.bin/rollup”很糟糕。

注意,我不想全局安装汇总等。

【问题讨论】:

    标签: yarnpkg yarn-workspaces yarnpkg-v2


    【解决方案1】:

    要运行安装在根工作区中的可执行文件,您可以说:

    "build": "run -T rollup",
    

    -T 用于--top-level,目前未在任何地方记录。一位维护人员告知我它在 Yarn discord 服务器上的存在。

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

    【讨论】:

    【解决方案2】:

    所以,我找到了一个不错的解决方案。在工作区根目录中,我为要在脚本中使用的命令添加了一些可执行文件,例如:

    tsc:

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

    rollup:

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

    这些基本上将调用“转发”到实际的二进制文件,并可能添加一些常用参数。

    在我的 foo/bar package.json 中,我现在可以引用这些脚本:

        "dev:compile": "../tsc",
        "build": "../tsc && ../rollup -c rollup.config.js",
    

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2021-06-23
      • 2021-06-14
      • 2020-02-15
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      • 1970-01-01
      • 2021-06-25
      相关资源
      最近更新 更多