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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 8,900评论 0 6
  • 你别爱我了我是个自私的人,我只希望自己越来越好,我只渴望别人对我的好。你走吧,我不爱你,我没有办法勉强自己,我是个...
    韩聪明阅读 212评论 0 0
  • 一 对一件事情感觉很特别,记得很牢,总归会有原因,这个原因,或许是因为它发生在你记忆初开、刚刚懂事的时候,又或许是...
    艺文小舍阅读 881评论 6 11
  • 你玩着消消乐; 他看着今日头条; 而更多的似乎是在聊天, 他们用软件聊的, 因为只看见他们低着头按着手机。 ...
    小东记事阅读 168评论 0 1
  • 【同读一本书.杨平】2015-1-12-022 —————— —《白银谷》 —————— 正文: 生意上遭赔累不用...
    杨平的阅读 648评论 11 2