task

运行的linttest等在turbo中被称作任务即task 在代码库的根目录中可以看到,每个命令运行的其实是通过trubo run xxx来运行的, 而这个任务都是需要通过在turbo.json中的pipleline中去定义的,否则找不到该任务。 image.pngimage.png

turbo run xxx运行的时候是会找到每个工作区中在package.json中在script中定义了xxx脚本的。 比如,trubo run dev,实际上是会找在各个workspace中的package.jsonscript中定义了dev脚本的去运行,如果没有定义就不会运行

cache

turborepo中是使用**本地缓存**来进行加速的。 image.png

  1. 首先会评估你本次任务的输入文件(默认是gitignored中没有忽略的文件),并且根据这些文件生成对应的hash值。(78awdk123)
  2. 在本地文件系统中查找缓存(eg: ./node_modules/.cache/turbo/78awdk123)
  3. 如果没有查找到对应hash的文件夹,说明没有缓存,那么就会执行任务
  4. **任务执行完成后,**会将对应的输出(包括输出的文件以及日志log)全部缓存,以便下次使用。

经过上面的步骤之后,当下次执行同样task时,就会命中缓存。 image.png

  1. 当输入的文件没有发生任何变化时,计算出的hash是一样的
  2. turborepo会去缓存目录中匹配hash值的文件
  3. 匹配到之后,就不会再去执行命令,而是直接拿出缓存中的输出文件以及log直接使用

配置-输出

可以通过outputs来配置需要缓存哪些文件。 当设置为空数组时,只缓存**log**

json
{
  "$schema": "https://turborepo.org/schema.json",
  "pipeline": {
    "build": {
      "outputs": ["dist/**", ".next/**"],
      "dependsOn": ["^build"]
    },
    "test": {
      "outputs": [], // leave empty to only cache logs
      "dependsOn": ["build"]
    }
  }
}

配置-输出

默认情况下当工作区的任何文件更改,都会任务是该工作区的更新,hash值就会刷新。但是有时我们只想关注部分文件(与该任务相关的文件),那么inputs属性可以让我们指定当前任务相关的文件。只要这些配置的相关文件更新才会影响到该任务,其他文件的更新并不会影响

json
{
  "$schema": "https://turborepo.org/schema.json",
  "pipeline": {
    // ... omitted for brevity
 
    "test": {
      // A workspace's `test` task depends on that workspace's
      // own `build` task being completed first.
      "dependsOn": ["build"],
      "outputs": [],
      // A workspace's `test` task should only be rerun when
      // either a `.tsx` or `.ts` file has changed.
      "inputs": ["src/**/*.tsx", "src/**/*.ts", "test/**/*.ts"]
    }
  }
}

DANGER

package.json文件会一直被当做输入文件。 因为turbo中的任务被定义在script中,一旦package.json文件变动,那么缓存就会失效

配置-关闭缓存

  1. 命令行参数--no-cacheeg: turbo run dev --no-cache
  2. turbo.json中配置
json
{
  "$schema": "https://turborepo.org/schema.json",
  "pipeline": {
    "dev": {
      "cache": false // 关闭缓存
    }
  }
}

环境变量-env

环境变量也会对缓存产生影响。 不过turborepo,对一些常用的框架中集成的环境变量已经做到了自动引用,不用我们去手动的声明.

json
{
  "$schema": "https://turborepo.org/schema.json",
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],
      // env vars will impact hashes of all "build" tasks
      "env": ["SOME_ENV_VAR"],
      "outputs": ["dist/**"]
    },
 
    // override settings for the "build" task for the "web" app
    "web#build": {
      "dependsOn": ["^build"],
      "env": [
        // env vars that will impact the hash of "build" task for only "web" app
        "STRIPE_SECRET_KEY",
        "NEXT_PUBLIC_STRIPE_PUBLIC_KEY",
        "NEXT_PUBLIC_ANALYTICS_ID"
      ],
      "outputs": [".next/**"],
    },
  },
 "globalEnv": [
   "GITHUB_TOKEN" // env var that will impact the hashes of all tasks,
 ]
}

对于一些自定义的环境变量我们还是需要手动的声明。 这里官方推荐了两个eslint相关的插件来规范我们的环境变量相关配置。会帮助我们检测一些忽略掉的环境变量声明 https://turbo.build/repo/docs/core-concepts/caching#eslint-config-turbo

强制重写缓存

--force

powershell
# Run `build` npm script in all workspaces,
# ignoring cache hits.
turbo run build --force