建立外掛
- 使用
npm install -g grunt-init
安裝 grunt-init - 使用
git clone git://github.com/gruntjs/grunt-init-gruntplugin.git ~/.grunt-init/gruntplugin
(Windows 上為%USERPROFILE%\.grunt-init\gruntplugin
) 安裝 gruntplugin 範本。 - 在空目錄中執行
grunt-init gruntplugin
。 - 執行
npm install
準備開發環境。 - 撰寫您的外掛。
- 執行
npm publish
將 Grunt 外掛發布到 npm!
注意事項
命名您的任務
「grunt-contrib」命名空間保留給 Grunt 團隊維護的任務,請為您的任務命名適當的名稱,避免使用此命名慣例。
除錯
Grunt 預設會隱藏錯誤堆疊追蹤,但可以使用 --stack
選項啟用,以利任務除錯。如果您希望 Grunt 在發生錯誤時總是記錄堆疊追蹤,請在您的 shell 中建立別名。例如,在 bash 中,您可以執行 alias grunt='grunt --stack'
。
儲存任務檔案
僅將資料檔案儲存在專案根目錄的 .grunt/[npm-module-name]/ 目錄中,並在適當時機自行清理。這並非暫時性暫存檔案的解決方案,請使用其中一個常見的 npm 模組(例如 temporary、tmp),它們會利用作業系統層級的暫時目錄來處理這種情況。
避免變更目前的作業目錄:process.cwd()
預設情況下,目前的作業目錄設定為包含 gruntfile 的目錄。使用者可以在 gruntfile 中使用 grunt.file.setBase()
變更目錄,但外掛應注意不要變更目錄。
path.resolve('foo')
可用於取得相對於 Gruntfile
的檔案路徑 'foo' 的絕對路徑。
建立您的任務
在文件中發現錯誤?提交問題。