Rush Stack商店網誌活動
跳至主要內容

@rushstack/heft-config-file

@rushstack/heft-config-file 函式庫是載入 Heft 組態檔的標準執行引擎。它提供許多功能,例如:

  • JSON 架構驗證
  • 具有直覺式錯誤訊息的「繼承」
  • 支援〈a href="/pages/intro/rig_packages/">rig 套件解析
  • 四種不同的「繼承」類型(附加、合併、取代、已計算),並預先設定預設值
  • 屬性繼承指令自訂化

屬性繼承指令

在使用「繼承」時,Heft 組態檔通常預先設定直覺式的預設策略,適用於每個 JSON 欄位。(在真實範例中,請參閱 JestPlugin.ts 中的 propertyInheritance 欄位。)

如果您需要特定設定的不同繼承類型,可將**屬性繼承指令**新增至您的 JSON 檔案。例如,假設我們正使用先前定義的 exampleObject 值(為鍵值物件),及 exampleArray 值(為陣列物件)來擴充假設檔案

{
"$schema": "https://developer.microsoft.com/json-schemas/heft/v0/example-config-file.schema.json",
"extends": "base-project/config/example-config-file.json",

"$exampleObject.inheritanceType": "merge", // valid choices are: "merge", "replace"
"exampleObject": {
"$exampleObjectMember.inheritanceType": "merge", // valid choices are: "merge", "replace"
"exampleObjectMember": { ... },

"$exampleArrayMember.inheritanceType": "append", // valid choices are: "append", "replace"
"exampleArrayMember": [ ... ]
},

"$exampleArray.inheritanceType": "replace", // valid choices are: "append", "replace"
"exampleArray": [ ... ]
}

一旦將物件設定為 inheritanceType 的覆寫,所有次屬性的 inheritanceType 值都將被忽略,因為最上層的物件已覆寫所有次屬性。

需要留意的是,編碼物件不同於陣列時使用不同的邏輯。這是為了明確表示陣列將按原狀附加,且在合併過程中不會進行額外的處理(例如,如果陣列打算設定為集合,則不會進行重複資料移除)。如果需要此類行為,則可以在實作方進行。在 @rushstack/heft-config-file 套件中移除陣列中含非原始物件的重複資料沒有什麼意義,這是因為並不容易定義移除陣列中非原始物件的重複資料。