[英]Ignore “Local NPM module not found” warning in Grunt
我正在使用matchdep將package.json
文件中的依賴項讀取到grunt
。
require('matchdep').filterAll('grunt-*').forEach(grunt.loadNpmTasks);
我的依賴項分為dependencies
(適用於每個人)和devDependencies
(適用於前端開發人員)。
我們的后端開發人員將運行以下內容以構建靜態資產,而無需茉莉花,phantomJS等(這些內容將由前端開發人員和CI服務器運行)
$ npm install --production
$ grunt build
但是,當使用--production
版本時, grunt.loadNpmTasks()
將針對任何缺少的軟件包發出警告。
>> Local Npm module "grunt-contrib-watch" not found. Is it installed?
有沒有辦法抑制這個警告?
您必須質疑為什么您的“后端開發人員”必須實際構建您的軟件包-否則,為什么他們需要笨拙而不需要devDependencies。 這是一種倒退(要求用戶構建您的軟件包肯定是一種反模式)。
話雖這么說,使用matchdep,您可以/應該使用:
require('matchdep').filter
require('matchdep').filterAll
在“開發”目標中 當然,這將需要您專門化grunt build
(例如:使用grunt builddev
和grunt buildproduction
或使用環境變量)-但同樣,請參見上文...
您可以使用CLI標志將選項傳遞給grunt。 為了保持一致性,我使用--production
標志,就像使用npm一樣。
因此,從CLI:
$ grunt build --production
然后在Gruntfile中:
var dependencies;
// test for the production flag
if (grunt.option('production')) {
// scan dependencies but ignore dev
dependencies = require('matchdep').filter('grunt-*');
} else {
// scan all dependencies
dependencies = require('matchdep').filterAll('grunt-*');
}
// load only relevant dependencies
dependencies.forEach(grunt.loadNpmTasks);
這是在注冊任何自定義任務之前在模塊頂部完成的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.