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.