Turborepo 代码修改
Turborepo 提供代码修改转换和自动迁移脚本,以帮助您在功能弃用时升级 Turborepo 代码库。
代码修改是通过编程方式在您的代码库上运行的转换。这允许对大量更改进行应用,而无需手动浏览每个文件。
用法
npx @turbo/codemod <transform> <path>transform- 转换名称,请参见下文提供的可用转换。path- 要转换的文件或目录--dry- 进行试运行,不会编辑任何代码--print- 打印更改后的输出以供比较
Turborepo 1.x
- add-package-manager
- create-turbo-config
- migrate-env-var-dependencies
- set-default-outputs
- stabilize-env-mode
- transform-env-literals-to-wildcards
add-package-manager
在 v1.1.0 中引入
转换根目录 package.json,以便 packageManager 键作为检测到的包管理器 (yarn, npm, pnpm) 和版本(例如 yarn@1.22.17)。此键现在 受 Node.js 支持 (在新标签页中打开),并由 Turborepo 用于更快的包管理器检测(与仅从文件系统推断相比)。
例如,对于 Yarn v1
// Before
{
"name": "turborepo-basic",
"version": "0.0.0",
"private": true,
"workspaces": ["apps/*", "packages/*"]
// ...
}{
"name": "turborepo-basic",
"version": "0.0.0",
"private": true,
+ "packageManager": "yarn@1.22.17",
"workspaces": [
"apps/*",
"packages/*"
]
}用法
转到您的项目
cd path-to-your-turborepo/运行代码修改
npx @turbo/codemod add-package-managercreate-turbo-config
在 v1.1.0 中引入
基于 package.json 中的 "turbo" 键,在您项目的根目录中创建 turbo.json 文件。随后从 package.json 中删除 "turbo" 键。
例如
// Before, package.json
{
"name": "Monorepo root",
"private": true,
"turbo": {
"pipeline": {
...
}
},
...
}// After, package.json
{
"name": "Monorepo root",
"private": true,
- "turbo": {
- "pipeline": {
- ...
- }
- },
...
}
// After, turbo.json
+{
+ "$schema": "https://turbo.net.cn/schema.json",
+ "pipeline": {
+ ...
+ }
+}用法
转到您的项目
cd path-to-your-turborepo/运行代码修改
npx @turbo/codemod create-turbo-configmigrate-env-var-dependencies
在 v1.5.0 中引入
将 turbo.json 中的所有环境变量依赖项从 dependsOn 和 globalDependencies 分别迁移到 env 和 globalEnv。
例如
// Before, turbo.json
{
"$schema": "https://turbo.net.cn/schema.json",
"globalDependencies": [".env", "$CI_ENV"],
"pipeline": {
"build": {
"dependsOn": ["^build", "$API_BASE"],
"outputs": [".next/**", "!.next/cache/**"]
},
"lint": {},
"dev": {
"cache": false,
"persistent": true
}
}
}// After, turbo.json
{
"$schema": "https://turbo.net.cn/schema.json",
- "globalDependencies": [".env", "$CI_ENV"],
+ "globalDependencies": [".env"],
+ "globalEnv": ["CI_ENV"],
"pipeline": {
"build": {
- "dependsOn": ["^build", "$API_BASE"],
+ "dependsOn": ["^build"],
+ "env": ["API_BASE"],
"outputs": [".next/**", "!.next/cache/**"],
},
"lint": {},
"dev": {
"cache": false,
"persistent": true
}
}
}用法
转到您的项目
cd path-to-your-turborepo/运行代码修改
npx @turbo/codemod migrate-env-var-dependenciesset-default-outputs
在 v1.7.0 中引入
将 turbo.json 输出迁移到包括之前推断的 dist/ 和 build/。
例如
// Before, turbo.json
{
"$schema": "https://turbo.net.cn/schema.json",
"globalDependencies": [".env"],
"globalEnv": ["CI_ENV"],
"pipeline": {
"build": {
"dependsOn": ["^build"],
"env": ["API_BASE"],
"outputs": [".next/**", "!.next/cache/**"]
},
"lint": {
"outputs": []
},
"dev": {
"cache": false,
"persistent": true
}
}
}// After, turbo.json
{
"$schema": "https://turbo.net.cn/schema.json",
"globalDependencies": [".env"],
"globalEnv": ["CI_ENV"],
"pipeline": {
"build": {
"dependsOn": ["^build"],
"env": ["API_BASE"],
"outputs": [".next/**", "!.next/cache/**"]
},
- "lint": {
- "outputs": []
- },
+ "lint": {},
"dev": {
"cache": false,
"persistent": true,
+ "outputs": ["dist/**", "build/**"]
}
}
}
用法
转到您的项目
cd path-to-your-turborepo/运行代码修改
npx @turbo/codemod set-default-outputsstabilize-env-mode
在 v1.10.0 中引入
将 turbo.json 的 experimentalGlobalPassThroughEnv 迁移到 globalPassThroughEnv,并将 experimentalPassThroughEnv 迁移到 passThroughEnv。
例如
// Before, turbo.json
{
"$schema": "https://turbo.net.cn/schema.json",
"experimentalGlobalPassThroughEnv": ["CC"],
"pipeline": {
"build": {
"experimentalPassThroughEnv": ["GOROOT"],
}
}
}// After, turbo.json
{
"$schema": "https://turbo.net.cn/schema.json",
"globalPassThroughEnv": ["CC"],
"pipeline": {
"build": {
"passThroughEnv": ["GOROOT"],
}
}
}用法
转到您的项目
cd path-to-your-turborepo/运行代码修改
npx @turbo/codemod stabilize-env-modetransform-env-literals-to-wildcards
在 v1.10.0 中引入
更新任何现有环境变量字段,其内容对新的通配符语法将是模棱两可的。
例如
// Before, turbo.json
{
"$schema": "https://turbo.net.cn/schema.json",
"globalEnv": ["THIS_*_IS_LITERAL"],
"globalPassThroughEnv": ["!LITERAL_LEADING_EXCLAMATION"],
"pipeline": {
"build": {
"env": ["50_PERCENT_OFF*_HAS_SMALL_PRINT"],
"passThroughEnv": ["**BOLDED**"],
}
}
}// After, turbo.json
{
"$schema": "https://turbo.net.cn/schema.json",
"globalEnv": ["THIS_\\*_IS_LITERAL"],
"globalPassThroughEnv": ["\\!LITERAL_LEADING_EXCLAMATION"],
"pipeline": {
"build": {
"env": ["50_PERCENT_OFF\\*_HAS_SMALL_PRINT"],
"passThroughEnv": ["\\*\\*BOLDED\\*\\*"],
}
}
}用法
转到您的项目
cd path-to-your-turborepo/运行代码修改
npx @turbo/codemod transform-env-literals-to-wildcards