处理平台

Node.js 版本

¥Node.js versions

为了兼容 Node.js 版本,请使用 package.json 中的 engine 键。Turborepo 会记录此字段的更改,并相应地缓存丢失。

¥To account for Node.js versions, use the engines key in package.json. Turborepo will account for changes to this field and miss cache accordingly.

操作系统、架构和其他任意条件

¥Operating systems, architecture, and other arbitrary conditions

对于高级用例,你可能希望操作系统 (OS)、架构或其他外部因素影响你的哈希值。

¥For advanced use cases, you may want the operating system (OS), architecture, or other external factors to contribute to your hash.

1. 将任意文件写入磁盘

¥ Write an arbitrary file to disk

首先,创建一个脚本,用于记录你感兴趣的哈希值贡献者。例如,以下是一段 Node.js 脚本,用于识别平台和架构,并将这些详细信息写入文件 (turbo-cache-key.json):

¥First, create a script that accounts for the hash contributors that you are interested in. For example, here is a Node.js script that identifies platform and architecture and writes those details to a file (turbo-cache-key.json):

./scripts/create-turbo-cache-key.js
#!/usr/bin/env node
 
const { writeFileSync } = require('fs');
const { join } = require('path');
 
const { platform, arch } = process;
const file = 'turbo-cache-key.json';
const str = JSON.stringify({ platform, arch });
console.log(`Generating cache key: ${str}`);
writeFileSync(file, str);

2. 将文件添加到你的 .gitignore 文件

¥ Add the file to your .gitignore

由于此文件依赖于环境,因此你不需要将其提交到源代码管理。将其添加到你的 .gitignore

¥You won't want to commit this file to source control since it's dependent on environment. Add it to your .gitignore:

.gitignore
+ turbo-cache-key.json

3. 将文件添加到哈希值

¥ Add the file to the hash

现在,通过将文件添加到任务输入中,确保 turbo 知道该文件。你可以通过两种方式执行此操作:

¥Now, make sure that turbo is aware of the file by adding it to task inputs. You can do this two ways:

  • 对于特定任务:在任务的 inputs 数组 中包含该文件:

    ¥For specific tasks: Include the file in the inputs array of the task(s):

Turborepo logo
./turbo.json
{
  "tasks": {
    "build-for-platforms": {
      "dependsOn": ["^build"],
      "inputs": ["$TURBO_DEFAULT$", "turbo-cache-key.json"]
    }
  }
}
Turborepo logo
./turbo.json
{
  "globalDependencies": ["turbo-cache-key.json"],
  "tasks": {
    ...
  }
}

4. 运行 turbo 之前生成文件

¥ Generate the file before running turbo

最后,你需要确保在运行 turbo 之前运行该脚本。例如:

¥Last, you'll want to ensure that you run the script before running turbo. For example:

./package.json
{
  "scripts": {
    "build-for-platforms": "node ./scripts/create-turbo-cache-key.js && turbo run build"
  }
}

turbo run build 现在将在计算 build 任务的哈希值时考虑 turbo-cache-key.json 的内容。

¥turbo run build will now take into account the contents of turbo-cache-key.json when calculating the hash for the build task.