Dark mode
คู่มือการตั้งค่า Turborepo (Turborepo Configuration Guide)
ไฟล์ turbo.json
เป็นหัวใจสำคัญของการตั้งค่า Turborepo ซึ่งใช้กำหนดวิธีการทำงานของ tasks, การแคช และการจัดการ dependencies
การตั้งค่าพื้นฐาน (Basic Configuration)
ตัวอย่างการตั้งค่าพื้นฐานใน turbo.json
:
json
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"outputs": ["dist/**", ".next/**"],
"dependsOn": ["^build"]
},
"test": {
"dependsOn": ["build"],
"inputs": ["src/**/*.tsx", "src/**/*.ts", "test/**/*.ts"],
"outputs": []
},
"lint": {
"outputs": []
},
"dev": {
"cache": false,
"persistent": true
}
}
}
ตัวอย่างการตั้งค่า (Configuration Examples)
1. การตั้งค่าสำหรับโปรเจค Next.js
ตัวอย่างนี้เหมาะสำหรับโปรเจคที่ใช้ Next.js:
json
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"outputs": [".next/**", "!.next/cache/**"],
"dependsOn": ["^build"]
},
"dev": {
"cache": false,
"persistent": true
},
"lint": {},
"test": {
"dependsOn": ["build"]
}
}
}
2. การตั้งค่าสำหรับ Full-Stack Monorepo
ตัวอย่างการตั้งค่าสำหรับโปรเจคขนาดใหญ่ที่มีทั้ง frontend และ backend:
json
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**", ".next/**"]
},
"db:migrate": {
"cache": false
},
"test": {
"dependsOn": ["build"],
"inputs": ["src/**/*.tsx", "src/**/*.ts", "test/**/*.ts"],
"outputs": ["coverage/**"]
},
"lint": {
"outputs": []
},
"deploy": {
"dependsOn": ["build", "test", "lint"],
"outputs": []
}
}
}
แนวคิดสำคัญ (Key Concepts)
Pipeline Tasks (งานในไปป์ไลน์)
- build: การคอมไพล์และสร้างเอาต์พุต
- test: การรันเทสต์
- lint: การตรวจสอบคุณภาพโค้ด
- dev: เซิร์ฟเวอร์สำหรับการพัฒนา
- deploy: งานสำหรับการ deploy
ตัวเลือกในการตั้งค่า (Configuration Options)
dependsOn (การพึ่งพา)
^build
: ต้องรอให้ task build ของ dependencies ทั้งหมดเสร็จก่อนbuild
: ต้องรอให้ task build ของตัวเองเสร็จก่อน
outputs (เอาต์พุต)
- ระบุไฟล์หรือโฟลเดอร์ที่ต้องการแคช
- ใช้
!
เพื่อยกเว้นบางพาธ
inputs (อินพุต)
- กำหนดไฟล์ที่จะทำให้แคชถูกล้าง
- รองรับรูปแบบ glob patterns
cache (การแคช)
true
: เปิดใช้งานการแคช (ค่าเริ่มต้น)false
: ปิดใช้งานการแคช
persistent (การทำงานต่อเนื่อง)
true
: สำหรับงานที่ต้องทำงานต่อเนื่อง (เช่น dev servers)false
: สำหรับงานที่ทำครั้งเดียวจบ (ค่าเริ่มต้น)
ตัวอย่างขั้นสูง (Advanced Examples)
การตั้งค่าเฉพาะ Workspace
ตัวอย่างการตั้งค่าที่แตกต่างกันในแต่ละ workspace:
json
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**"]
},
"web#build": {
"dependsOn": ["^build"],
"outputs": [".next/**"],
"env": ["NEXT_PUBLIC_API_URL"]
},
"docs#build": {
"dependsOn": ["^build"],
"outputs": [".docusaurus/**"],
"env": ["DOCS_URL"]
}
}
}
ตัวแปรสภาพแวดล้อม (Environment Variables)
json
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**"],
"env": ["API_KEY", "NODE_ENV"]
}
},
"globalEnv": ["CI", "GITHUB_TOKEN"]
}
แนวทางปฏิบัติที่ดี (Best Practices)
- ระบุ
outputs
เสมอสำหรับงานที่ต้องการแคช - ใช้
cache: false
สำหรับงานที่มีการแก้ไขข้อมูลภายนอก - ระบุตัวแปร
env
ที่จำเป็นเพื่อให้การแคชทำงานถูกต้อง - ใช้การตั้งค่าเฉพาะ workspace เมื่อจำเป็น
- ปิดการแคชสำหรับงานพัฒนาด้วย
cache: false
โปรดจำไว้ว่าการแคชของ Turborepo อิงตามเนื้อหา ดังนั้นจะรันงานใหม่เฉพาะเมื่อ inputs มีการเปลี่ยนแปลงจริงๆ เท่านั้น