Turborepo Codemod
當某項功能已棄用時,Turborepo 提供 Codemod 轉換和自動遷移腳本,以協助升級您的 Turborepo 程式碼庫。
Codemod 是以程式方式執行於您的程式碼庫的轉換。這允許大量變更套用,而無需手動瀏覽每個檔案。
用法
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/
執行 codemod
npx @turbo/codemod add-package-manager
create-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.dev.org.tw/schema.json",
+ "pipeline": {
+ ...
+ }
+}
用法
前往您的專案
cd path-to-your-turborepo/
執行 codemod
npx @turbo/codemod create-turbo-config
migrate-env-var-dependencies
在 v1.5.0 中推出
將 turbo.json
中的所有環境變數相依性從 dependsOn
和 globalDependencies
遷移到 env
和 globalEnv
。
例如
// Before, turbo.json
{
"$schema": "https://turbo.dev.org.tw/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.dev.org.tw/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/
執行 codemod
npx @turbo/codemod migrate-env-var-dependencies
set-default-outputs
在 v1.7.0 中推出
將 turbo.json
輸出遷移到包含先前推斷的 dist/
和 build/
。
例如
// Before, turbo.json
{
"$schema": "https://turbo.dev.org.tw/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.dev.org.tw/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/
執行 codemod
npx @turbo/codemod set-default-outputs
stabilize-env-mode
在 v1.10.0 中引入
將 turbo.json
的 experimentalGlobalPassThroughEnv
遷移至 globalPassThroughEnv
,並將 experimentalPassThroughEnv
遷移至 passThroughEnv
。
例如
// Before, turbo.json
{
"$schema": "https://turbo.dev.org.tw/schema.json",
"experimentalGlobalPassThroughEnv": ["CC"],
"pipeline": {
"build": {
"experimentalPassThroughEnv": ["GOROOT"],
}
}
}
// After, turbo.json
{
"$schema": "https://turbo.dev.org.tw/schema.json",
"globalPassThroughEnv": ["CC"],
"pipeline": {
"build": {
"passThroughEnv": ["GOROOT"],
}
}
}
用法
前往您的專案
cd path-to-your-turborepo/
執行 codemod
npx @turbo/codemod stabilize-env-mode
transform-env-literals-to-wildcards
在 v1.10.0 中引入
更新任何現有的環境變數欄位,其內容對於新的萬用字元語法來說可能會造成歧義。
例如
// Before, turbo.json
{
"$schema": "https://turbo.dev.org.tw/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.dev.org.tw/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/
執行 codemod
npx @turbo/codemod transform-env-literals-to-wildcards