Automatically Load Gulp Plugins with gulp-load-plugins

I've recently discovered gulp-load-plugins for Gulp and it's really neat.Rather than have to require each plugin,gulp-load-plugins will search your package.json file and automatically include them as plugins.pluginName().

Once installed it's simple to use.For example, we've got some dependencies in our package.json file like:

{
  "name": "angular-coffee-frontend",
  "version": "1.0.0",
  "description": "angular project with coffee, gulp",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "michael.mo",
  "license": "ISC",
  "devDependencies": {
    "coffee-script": "^1.10.0",
    "gulp-autoprefixer": "^3.1.0",
    "gulp-cache": "^0.4.2",
    "gulp-concat": "^2.6.0",
    "gulp-imagemin": "^2.4.0",
    "gulp-less": "^3.0.5",
    "gulp-minify-css": "^1.2.4",
    "gulp-notify": "^2.2.0",
    "gulp-rename": "^1.2.2",
    "gulp-uglify": "^1.5.3"
  }
}

Then in the gulpfile.coffee include the plugins using gulpLoadPlugins:

gulp = require 'gulp'
gulpLoadPlugins = require 'gulp-load-plugins'
plugins = gulpLoadPlugins()

Or even simpler:

gulp = require 'gulp'
plugins = require('gulp-load-plugins')()

We can then use the included plugins as plugins.less() and plugins.rename().

Essentially the following is kind of happening:

plugins.less=  require `gulp-less`
plugins.rename = require `gulp-rename`

'kind of' mean it's actually lazy loading the plugins so they are not loaded until you use them.This is good because if you are just running one specific task in your gulpfile.js it will only load the plugins used by it rather than loading a list of plugins defined at the top of the file, as is often done, many of which may not be required by the particular task.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 9,053評(píng)論 0 6
  • 你別愛(ài)我了我是個(gè)自私的人,我只希望自己越來(lái)越好,我只渴望別人對(duì)我的好。你走吧,我不愛(ài)你,我沒(méi)有辦法勉強(qiáng)自己,我是個(gè)...
    韓聰明閱讀 277評(píng)論 0 0
  • 一 對(duì)一件事情感覺(jué)很特別,記得很牢,總歸會(huì)有原因,這個(gè)原因,或許是因?yàn)樗l(fā)生在你記憶初開、剛剛懂事的時(shí)候,又或許是...
    藝文小舍閱讀 1,216評(píng)論 6 11
  • 你玩著消消樂(lè); 他看著今日頭條; 而更多的似乎是在聊天, 他們用軟件聊的, 因?yàn)橹豢匆?jiàn)他們低著頭按著手機(jī)。 ...
    小東記事閱讀 188評(píng)論 0 1
  • 【同讀一本書.楊平】2015-1-12-022 —————— —《白銀谷》 —————— 正文: 生意上遭賠累不用...
    楊平的閱讀 702評(píng)論 11 2

友情鏈接更多精彩內(nèi)容