管理相依性
概念
- Deno 使用 URL 來進行相依性管理。
- 一個慣例是將所有這些相依的 URL 放入本地的
deps.ts
檔案中。然後從deps.ts
匯出功能供本機模組使用。 - 繼續這個慣例,開發人員僅有的相依性可以保留在
dev_deps.ts
檔案中。 - 另請參閱 模組。
概觀
Deno 中沒有套件管理員的概念,因為外部模組會直接匯入到本機模組中。這會產生一個問題,即如何在沒有套件管理員的情況下管理遠端依賴項。在有許多依賴項的大型專案中,如果將所有模組個別匯入到不同的模組中,更新模組會變得繁瑣且耗時。
在 Deno 中解決此問題的標準做法是建立一個 deps.ts
檔案。所有必要的遠端依賴項都會在此檔案中被參照,而必要的函式和類別則會被重新匯出。然後,依賴的本機模組會參照 deps.ts
,而不是遠端依賴項。現在,如果一個遠端依賴項在多個檔案中使用,則升級它會簡單得多,因為這一切都可以在 deps.ts
中管理。
開發依賴項也可以在一個獨立的 dev_deps.ts
檔案中管理,這允許在開發和生產依賴項之間進行明確的區分。
範例
/**
* deps.ts
*
* This module re-exports the required methods
* from the dependant remote Ramda module.
*/
export {
add,
multiply,
} from "https://x.nest.land/ramda@0.27.0/source/index.js";
此範例具有與 本機和遠端匯入範例 相同的功能。然而,在此情況下,Ramda 模組並非直接被參照,而是透過使用本機 deps.ts
模組來代理參照。
指令: deno run example.ts
/**
* example.ts
*/
import { add, multiply } from "./deps.ts";
function totalCost(outbound: number, inbound: number, tax: number): number {
return multiply(add(outbound, inbound), tax);
}
console.log(totalCost(19, 31, 1.2));
console.log(totalCost(45, 27, 1.15));
/**
* Output
*
* 60
* 82.8
*/