安裝變體
有時您可能想要使用修改過的相依性集合來組建整個單一儲存庫。例如,假設您剛完成升級到框架的主要新版本,但您打算在過渡期間維持與先前版本的相容性。您的開發人員應該使用新的變體進行日常工作,但在 PR 組建中,您希望 CI 工作組建整個儲存庫兩次 -- 一次使用舊的變體,一次使用新的變體。
看起來您可以透過撰寫簡單的指令碼來搜尋+取代 package.json 檔案中的版本來解決此問題,但您很快就會遇到其他受影響的檔案
- shrinkwrap 檔案:除非您為兩個變體維護不同的 shrinkwrap 檔案,否則組建將不會是決定性的
- common-versions.json:
preferredVersions
或allowedAlternativeVersions
可能需要對兩個變體有所不同 - pnpmfile.js:如果您有針對行為不良的舊版套件的因應措施,則它們可能需要對兩個變體有所不同
這個問題似乎需要一組獨立的平行組態檔案。從 Rush 5.4.0 開始,現在有一個開箱即用的解決方案可以解決此問題。
設定變體
假設「widget-sdk」是一個假設的程式庫,剛出貨一個主要的新版本 3,而且我們已升級到版本 3,但我們想要維持與版本 2 的相容性。我們可以使用寬鬆的 SemVer 範圍在我們的 package.json 檔案中表示這一點
libraries/my-controls/package.json
{
"name": "my-controls",
"version": "1.0.0",
"description": "An example library project",
"license": "MIT",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"scripts": {
"build": "node_modules/.bin/my-build"
},
"dependencies": {
"widget-sdk": "^2.3.4 || ^3.0.2"
},
"devDependencies": {
"my-toolchain": "^1.0.0",
"typescript": "^3.0.3"
}
}
"^2.3.4 || ^3.0.2"
範圍表示我們的程式庫將接受 widget-sdk 版本 2.x (但不早於 2.3.4) 或版本 3.x (但不早於 3.0.2)。當您執行 rush update
時,您通常會取得最新的相容版本。如何使用較舊的版本 2 進行組建和測試?讓我們設定一個變體!
1. 定義您的變體。 在 rush.json 組態檔案中,我們新增如下的定義
*rush.json 摘錄*
"variants": [
// {
// /**
// * The folder name for this variant.
// */
// "variantName": "example-variant",
//
// /**
// * An informative description
// */
// "description": "Build this repo using the previous release of the SDK"
// }
{
"variantName": "old-widget-sdk",
"description": "Build this repo using version 2 of the widget-sdk"
}
],
2. 複製組態檔案。 若要開始您的變體,請將您現有的組態檔案從 common/config/rush 複製到您的變體資料夾 common/config/rush/variants/old-widget-sdk。目前支援三個組態檔案(未來可能會新增其他檔案)
- shrinkwrap.yaml、npm-shrinkwrap.json 或 yarn.lock,具體取決於您的套件管理員
- common-versions.json
- 如果您使用 PNPM 作為您的套件管理員,則為 pnpmfile.js
請務必將複製的檔案新增至 Git
git add .
git commit -m "Creating a new variant"
3. 覆寫變體的相依性版本。 在此範例中,我們將降級 widget-sdk 以使用版本 2.x。這可以使用 Rush 的偏好版本功能來完成。我們將使用萬用字元,以便 rush update --full
仍然選取次要/修補程式版本
*common-versions.json 摘錄*
/**
* A table that specifies a "preferred version" for a dependency package. The "preferred version"
* is typically used to hold an indirect dependency back to a specific version, however generally
* it can be any SemVer range specifier (e.g. "~1.2.3"), and it will narrow any (compatible)
* SemVer range specifier. See the Rush documentation for details about this feature.
*/
"preferredVersions": {
/**
* When someone asks for "^1.0.0" make sure they get "1.2.3" when working in this repo,
* instead of the latest version.
*/
// "some-library": "1.2.3"
"widget-sdk": "^2.3.9"
},
請注意,^2.3.9
符合我們在上面的 package.json 中指定的 SemVer 範圍 ^2.3.4 || ^3.0.2
。(如果這不是真的,那麼偏好的版本將不會有任何效果!)
4. 安裝您的變體並進行測試。 讓我們從執行 rush update
來安裝新的相依性版本集開始
rush update --full --variant old-widget-sdk
這將更新檔案 common/config/rush/old-widget-sdk/shrinkwrap.yaml、在 common/temp/node_modules 中安裝這些相依性,並連結每個專案以使用這些相依性。rush install
指令也支援 --variant
選項。當您的 CI 工作使用舊的 widget-sdk 版本進行組建時,可以使用此選項。
現在您可以組建和測試您的變體
rush rebuild
👉 如果您厭倦了輸入 --variant
,您也可以使用 RUSH_VARIANT 環境變數來指定變體名稱。
5. 還原原始狀態。 當您完成測試您的變體時,您可以透過執行不使用 --variant
選項的 rush install
來返回原始狀態。我們稱此為「預設變體」,因為它與未定義任何變體的儲存庫的預設行為相同
# Restore the original state by omitting "--variant":
rush install
提示:如果您忘記哪個變體處於活動狀態,您可以在 common/temp/current-variant.json 檔案中查看。如果您在文字編輯器中開啟此檔案,您應該會看到類似如下的一行
{
"variant": "old-widget-sdk"
}