API Extractor 外掛模組
外掛模組套件 | @rushstack/heft-api-extractor-plugin |
外掛模組名稱 | api-extractor-plugin 由 ApiExtractorPlugin.ts 所實作 |
外掛模組組態檔案 | api-extractor-task.json |
heft.json 選項 | (無) |
此外掛模組呼叫 API Extractor 工具,該工具會將 TypeScript 宣告(.d.ts 檔案)作為輸入,並產生三種類型的輸出
1. API 報告 - API Extractor 可以追蹤專案主要進入點的所有輸出,並產生報告做為 API 檢閱工作流程的基礎。
2. .d.ts 彙總 - 與 Webpack 能將所有 JavaScript 檔案彙總為單一 .js 檔案以進行發佈的方式類似,API Extractor 能將 TypeScript 宣告彙總為單一的 .d.ts 檔案。
3. API 文件 - API Extractor 能為每個專案產生「文件模型」JSON 檔案。此 JSON 檔案會包含提取的型別簽章和文件註解。api-documenter 輔助工具可以使用這些檔案產生 API 參考網站,或你可以將這些檔案用作自訂文件流程的輸入。
請參閱 API Extractor 文件 以了解其運作方式的詳細資料。
何時使用
我們建議為每個 TypeScript 函式庫專案使用 API Extractor,特別是當它已發佈為 NPM 套件時。
package.json 相依性
如果您使用 @rushstack/heft-node-rig 或 @rushstack/heft-web-rig 等標準設備,則 API 萃取器已經載入並組態。
否則,您需要將這些套件新增到專案中
- Rush
- NPM
# If you are using Rush, run this shell command in your project folder:
rush add --package @rushstack/heft-api-extractor-plugin --dev
rush add --package @microsoft/api-extractor --dev
# If you are using vanilla NPM, run this shell command in your project folder:
npm install @rushstack/heft-api-extractor-plugin --save-dev
npm install @microsoft/api-extractor --save-dev
組態
如果 API 萃取器尚未由設備提供,您可以在 heft.json 組態檔 中呼叫它,如以下範例所示
<專案資料夾>/config/heft.json
{
"$schema": "https://developer.microsoft.com/json-schemas/heft/v0/heft.schema.json",
. . .
"phasesByName": {
"build": {
"cleanFiles": [{ "sourcePath": "dist" }, { "sourcePath": "lib" }, { "sourcePath": "lib-commonjs" }],
"tasksByName": {
"typescript": {
"taskPlugin": {
"pluginPackage": "@rushstack/heft-typescript-plugin"
}
},
"api-extractor": {
"taskDependencies": ["typescript"],
"taskPlugin": {
"pluginPackage": "@rushstack/heft-api-extractor-plugin"
}
},
. . .
}
}
. . .
}
}
Heft 會尋找 API 萃取器的組態檔 config/api-extractor.json。這個檔案可以透過呼叫 api-extractor init 指令建立。這個檔案是 可設定設備 的。
對於進階場景,可選擇性的 api-extractor-task.json 組態檔提供了一些其他 Heft 專屬設定。
相關主題
- Heft 的 config/api-extractor.json 組態檔
- API 萃取器 網站