• Stars
    star
    756
  • Rank 60,056 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Automatically load in gulp plugins

gulp-load-plugins

npm

Loads gulp plugins from package dependencies and attaches them to an object of your choice.

Build Status

Node Version Requirements

Due to the native support of ES2015 syntax in newer versions of Node, this plugin requires at least Node v8. If you need to maintain support for older versions of Node, version 1.6.0 of this plugin is the last release that will support Node versions less than 8.

Install

NPM:

$ npm install --save-dev gulp-load-plugins

Yarn:

$ yarn add -D gulp-load-plugins

Usage

Given a package.json file that has some dependencies within:

{
    "dependencies": {
        "gulp-jshint": "*",
        "gulp-concat": "*"
    }
}

Adding this into your Gulpfile.js:

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

Or, even shorter:

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

Will result in the following happening (roughly, plugins are lazy loaded but in practice you won't notice any difference):

plugins.jshint = require('gulp-jshint');
plugins.concat = require('gulp-concat');

You can then use the plugins just like you would if you'd manually required them, but referring to them as plugins.name(), rather than just name().

This frees you up from having to manually require each gulp plugin.

Options

You can pass in an object of options that are shown below: (the values for the keys are the defaults):

gulpLoadPlugins({
    DEBUG: false, // when set to true, the plugin will log info to console. Useful for bug reporting and issue debugging
    pattern: ['gulp-*', 'gulp.*', '@*/gulp{-,.}*'], // the glob(s) to search for
    overridePattern: true, // When true, overrides the built-in patterns. Otherwise, extends built-in patterns matcher list.
    config: 'package.json', // where to find the plugins, by default searched up from process.cwd()
    scope: ['dependencies', 'devDependencies', 'peerDependencies'], // which keys in the config to look within
    replaceString: /^gulp(-|\.)/, // what to remove from the name of the module when adding it to the context
    camelize: true, // if true, transforms hyphenated plugins names to camel case
    lazy: true, // whether the plugins should be lazy loaded on demand
    rename: {}, // a mapping of plugins to rename
    renameFn: function (name) { ... }, // a function to handle the renaming of plugins (the default works)
    postRequireTransforms: {}, // see documentation below
    maintainScope: true // toggles loading all npm scopes like non-scoped packages
});

Multiple config locations

While it's possile to grab plugins from another location, often times you may want to extend from another package that enables you to keep your own package.json free from duplicates, but still add in your own plugins that are needed for your project. Since the config option accepts an object, you can merge together multiple locations using the lodash.merge package:

const merge = require('lodash.merge');

const packages = merge(
  require('dep/package.json'),
  require('./package.json')
);

// Utilities
const $ = gulpLoadPlugins({
  config: packages
});

postRequireTransforms (1.3+ only)

This enables you to transform the plugin after it has been required by gulp-load-plugins.

For example, one particular plugin (let's say, gulp-foo), might need you to call a function to configure it before it is used. So you would end up with:

const $ = require('gulp-load-plugins')();
$.foo = $.foo.configure(...);

This is a bit messy. Instead you can pass a postRequireTransforms object which will enable you to do this:

const $ = require('gulp-load-plugins')({
  postRequireTransforms: {
    foo: function(foo) {
      return foo.configure(...);
    }
  }
});

$.foo // is already configured

Everytime a plugin is loaded, we check to see if a transform is defined, and if so, we call that function, passing in the loaded plugin. Whatever this function returns is then used as the value that's returned by gulp-load-plugins.

For 99% of gulp-plugins you will not need this behaviour, but for the odd plugin it's a nice way of keeping your code cleaner.

Renaming

From 0.8.0, you can pass in an object of mappings for renaming plugins. For example, imagine you want to load the gulp-ruby-sass plugin, but want to refer to it as just sass:

gulpLoadPlugins({
  rename: {
    'gulp-ruby-sass': 'sass'
  }
});

Note that if you specify the renameFn options with your own custom rename function, while the rename option will still work, the replaceString and camelize options will be ignored.

npm Scopes

gulp-load-plugins comes with npm scope support. By default, the scoped plugins are accessible through an object on plugins that represents the scope. When maintainScope = false, the plugins are available in the top level just like any other non-scoped plugins.

Note: maintainScope is only available in Version 1.4.0 and up.

For example, if the plugin is @myco/gulp-test-plugin then you can access the plugin as shown in the following example:

const scoped = require('gulp-load-plugins')({
  // true is the default value
  maintainScope: true,
});

scoped.myco.testPlugin();

const nonScoped = require('gulp-load-plugins')({
  maintainScope: false,
});

nonScoped.testPlugin();

Lazy Loading

In 0.4.0 and prior, lazy loading used to only work with plugins that return a function. In newer versions though, lazy loading should work for any plugin. If you have a problem related to this please try disabling lazy loading and see if that fixes it. Feel free to open an issue on this repo too.

Override Pattern

In 1.4.0 and prior, configuring the pattern option would override the built-in ['gulp-*', 'gulp.*', '@*/gulp{-,.}*']. If overridePattern: false, the configured pattern will now extends the built-in matching.

For example, both are equivilant statements.

const overridePlugins = require('gulp-load-plugins')({
  // true is the default value
  overridePattern: true,
  pattern: ['gulp-*', 'gulp.*', '@*/gulp{-,.}*', 'foo-bar']
});

const extendedPlugins = require('gulp-load-plugins')({
  overridePattern: false,
  pattern: ['foo-bar']
});

Credit

Credit largely goes to @sindresorhus for his load-grunt-plugins plugin. This plugin is almost identical, just tweaked slightly to work with Gulp and to expose the required plugins.

Changelog

2.0.8
  • Fixes #141 - module.parent deprecated in Node 14+. Thanks @DaveyJake
  • Update dependencies
2.0.7
  • Update dependencies
2.0.6
  • Update dependencies and add power support for Travis on ppc64le - thanks @dineshks1 - PR
2.0.5
  • Update dependencies
2.0.4
  • Update dependencies
2.0.3
  • Update dependencies
2.0.2
  • Update dependencies
2.0.1
  • Update dependencies and minor JS improvements
2.0.0
  • Drop support for old Node. Minimum version now Noda >= 8. Update all dependencies. Refactor some code with ES6. - thanks @TheDancingCode - PR
1.6.0
  • Bump some dependencies that had security vulnerabilities - thanks @tombye - PR
1.5.0
  • added overridePattern - thanks @bretkikehara - PR
1.4.0
  • added maintainScope - thanks @bretkikehara - PR
1.3.0
  • added postRequireTransforms - thanks @vinitm - PR
1.2.4
  • Fix bug in 1.2.3 release that stopped logging output in Gulp 3 - thanks @doowb
1.2.3
  • Update dependencies in line with Gulp 4 - PR - thanks @doowb
1.2.2
  • revert the previous PR in 1.2.1 which broke configuration loading for some users
1.2.1
  • fix using the wrong require function - PR - thanks @mwessner
1.2
  • throw an error if two packages are loaded that end up having the same name after the replaceString has been removed - thanks @carloshpds
1.1
  • added DEBUG option to turn on logging and help us debug issues - thanks @dcamilleri
1.0.0
  • added renameFn function to give users complete control over the name a plugin should be given when loaded - thanks @callumacrae
1.0.0-rc.1
  • This is the first release candidate for what will become version 1 of gulp-load-plugins. Once a fix for #70 is landed, I plan to release V1.
  • Breaking Change support for NODE_PATH is no longer supported. It was causing complexities and in the PR that droppped support no one shouted that they required NODE_PATH support.
0.10.0
  • throw a more informative error if a plugin is loaded that gulp-load-plugins can't find. PR - thanks @connor4312
  • allow require to look on the NODE_PATH if it can't find the module in the working directory. PR - thanks @chmanie
0.9.0
  • add support for npm-scoped plugins. PR - thanks @hbetts
0.8.1
  • fixed a bug where gulp-load-plugins would use the right package.json file but the wrong node_modules directory - thanks @callumacrae
0.8.0
  • add the ability to rename plugins that gulp-load-plugins loads in.
0.7.1
  • add files property to package.json so only required files are downloaded when installed - thanks @shinnn
0.7.0
  • support loading plugins with a dot in the name, such as gulp.spritesmith - thanks to @MRuy
  • upgrade multimatch to 1.0.0
0.6.0
  • Fix issues around plugin picking wrong package.json file - thanks @iliakan (see issue).
0.5.3
  • Show a nicer error if the plugin is unable to load any configuration and hence can't find any dependencies to load
0.5.2
  • Swap out globule for multimatch, thanks @sindresorhus.
0.5.1
  • Updated some internal dependencies which should see some small improvements - thanks @shinnn for this contribution.
0.5.0
  • improved lazy loading so it should work with plugins that don't just return a function. Thanks to @nfroidure for help with this.
0.4.0
  • plugins are lazy loaded for performance benefit. Thanks @julien-f for this.
0.3.0
  • turn the camelize option on by default
0.2.0
  • added camelize option, thanks @kombucha.
  • renamed to gulp-load-plugins.
0.1.1
  • add link to this repository into package.json (thanks @ben-eb).
0.1.0
  • move to gulpLoadplugins returning an object with the tasks define.
0.0.5
  • added replaceString option to configure exactly what gets replace when the plugin adds the module to the context
0.0.4
  • fixed keyword typo so plugin appears in search for gulp plugins
0.0.3
  • removed accidental console.log I'd left in
0.0.2
  • fixed accidentally missing a dependency out of package.json
0.0.1
  • initial release

More Repositories

1

test-data-bot

TypeScript
666
star
2

dotfiles

My dotfiles for my dev environment, compromising of tmux, vim, zsh and git.
Lua
235
star
3

jQuery-Form-Validator

Got bored of using other ones, so made my own
JavaScript
233
star
4

pulldown

The minimal JavaScript package manager.
JavaScript
175
star
5

demopack

A prepackaged Webpack for easy frontend demos.
JavaScript
172
star
6

remote-data-js

Dealing with remote data and all its states properly in JavaScript applications.
JavaScript
149
star
7

universal-react-example

An example Universal ReactJS application
JavaScript
144
star
8

the-refactoring-tales

HTML
138
star
9

tilvim

TIL Vim: Short posts on Vim tips and tricks
HTML
89
star
10

tweet-parser

Parsing tweets into lists of entities.
JavaScript
76
star
11

react-css-modules-webpack

React + Webpack Dev Server + Hot Reloading + CSS Modules
JavaScript
72
star
12

jspm-es6-react-example

JavaScript
69
star
13

fetch-factory

Easy JS objects for talking to your APIs
JavaScript
67
star
14

jspm-dev-builder

Incremental development builds with jspm
JavaScript
66
star
15

elmplayground

An Elm blog, written in Elm, about Elm.
Elm
63
star
16

react-remote-data

JavaScript
57
star
17

so-fetch

TypeScript
57
star
18

html-dist

A tool for editing HTML files to add new scripts
JavaScript
47
star
19

javascriptplayground.com

HTML
39
star
20

react-hot-load-webpack-boilerplate

JavaScript
37
star
21

doccy

Generate Markdown documentation from JavaScript
JavaScript
31
star
22

elm-for-js-developers-talk

Elm
30
star
23

vue2-demo-proj

JavaScript
28
star
24

react-training-workshop

Code, notes and more for a day long React workshop.
JavaScript
27
star
25

todo-react-testing

A silly small React TODO app used in a blog post about testing React apps.
JavaScript
25
star
26

rollup-plugin-markdown

JavaScript
23
star
27

react-no-webpack-required

Using React without Babel & Webpack
JavaScript
22
star
28

do-you-even-elm

How much Elm do you do?
Elm
21
star
29

node-todo

A Node.js & Express.js todo app
JavaScript
19
star
30

vscode-go-to-file

TypeScript
18
star
31

svg-to-elm

TypeScript
17
star
32

Forrst-API-jQuery-Wrapper

A jQuery wrapper for the Forrst API. As the Forrst API grows, so will this.
JavaScript
17
star
33

elm-boilerplate

Starting point for Elm-Lang projects
JavaScript
17
star
34

interactive-es6

Learn ES6 in your browser in an app built in ES6.
JavaScript
16
star
35

responsiveImages

Responsive Images in JavaScript!
CoffeeScript
16
star
36

jspm-es6-example

JavaScript
16
star
37

doubler

Simple doubles for JS testing
JavaScript
14
star
38

react-ast-viewer

An Abstract Syntax Tree browser, written in React
JavaScript
14
star
39

pure-react-forms

JavaScript
14
star
40

JavaScript-Playground--Simple-jQuery-PubSub

Code for the blog post on JavaScriptPlayground.com on a simple jQuery PubSub
JavaScript
13
star
41

cannon-blog

A ReactJS blogging engine
JavaScript
13
star
42

jspm-es6-angular-example

JavaScript
13
star
43

totesy

My own TODO app
JavaScript
12
star
44

elm-game-of-life

Building Conway's Game of Life in Elm
Elm
12
star
45

JS-Regex

Live JS Regular Expression Testing
JavaScript
12
star
46

prettier-scripts

Some shell scripts for Prettier.
JavaScript
12
star
47

github-proxy

GitHub API but cached and offline-able.
JavaScript
11
star
48

authoring-es6-module-example

JavaScript
10
star
49

node_cli_router

A router for parsing command line flags and arguments in your Node CLI scripts.
JavaScript
9
star
50

wouldlike.js

JavaScript
8
star
51

client-webpack-boilerplate

A teeny client side JS webpack boilerplate to get up and running quickly.
JavaScript
8
star
52

react-fundamentals-2019-workshop

JavaScript
7
star
53

skillswap

Share your skills with others and find people to help you learn new things.
JavaScript
7
star
54

half-stack-webpack

JavaScript
7
star
55

zip-list

TypeScript
7
star
56

testing-react-with-jest

JavaScript
7
star
57

react-introduction-exercises

JavaScript
7
star
58

universal-react-talk

JavaScript
7
star
59

jQuery-UK-2012-Conference-Notes

6
star
60

elm-github-info

Fetching info from GitHub using Elm
Elm
6
star
61

ST2-MDN-Search

Search the MDN from within Sublime Text 2
Python
6
star
62

ng2-jspm-typescript

JavaScript
6
star
63

vim-markdown-writer

Some Vim functions to make writing blog posts in Markdown easier.
Vim Script
6
star
64

introduction-to-elm-workshop

Elm
5
star
65

elm-statey

A state machine library in Elm
Elm
5
star
66

elm-json-decoding

Elm
5
star
67

rollup-plugin-import-glob

JavaScript
5
star
68

advent-of-code-2018

Elm
5
star
69

angular-router-browserify

Angular's router module to be used with browserify.
JavaScript
4
star
70

project_templater

A gem for quickly creating project structures.
Ruby
4
star
71

githubissuebrowser

Browse all issues across your repositories to find something to work on.
Ruby
4
star
72

ecmascript-evaluator

JavaScript
4
star
73

JS-Instagram-Wrapper

JavaScript
4
star
74

HTML5-Placeholder-PolyFill

Adds placeholder functionality to input fields in browsers that don't support HTML5 Placeholders
JavaScript
4
star
75

london-elm-018-talk

JavaScript
4
star
76

jspm-test

Browser test runner for jspm projects
JavaScript
4
star
77

elm-console-log

An easier log function for Elm.
Elm
4
star
78

react-webpack-boilerplate

JavaScript
4
star
79

testing-javascript-workshop

JavaScript
3
star
80

elm-connect-four

Connect 4 written in Elm
Elm
3
star
81

jQuote

3
star
82

epic-fel-es6-today-links

3
star
83

devcastin

Ruby
3
star
84

beginning-react

HTML
3
star
85

jack-sublime-snippets

3
star
86

jQuery-Split-List

Splits lists in half
JavaScript
3
star
87

Base-Template

A starting point for all my development.
JavaScript
3
star
88

reactjs-workshop

Code and notes for a ReactJS workshop
JavaScript
3
star
89

Deck.js---Gamepad-API

Deck.js slideshows controlled with the Gamepad API
JavaScript
3
star
90

jsFrame

Combining a jQuery Plugin with a CSS Grid for lovely coding.
JavaScript
3
star
91

MarkdownTwitter

Automatically linking Twitter mentions in Markdown blog posts
Ruby
2
star
92

jackfranklindotcodotuk

JavaScript
2
star
93

frontendlondonfeb2015-jspm-demo

JavaScript
2
star
94

CoffeePubSub

A very small Pub/Sub implementation in CoffeeScript
CoffeeScript
2
star
95

Back-to-Top-Firefox-Plugin

An easy way to get back to the top of any webpage in Firefox.
JavaScript
2
star
96

util-fns

JavaScript
2
star
97

interactive-react

JavaScript
2
star
98

rash

Ruby hash methods in JavaScript
JavaScript
2
star
99

Big-M-Workshop-Notes

2
star
100

langtons-ant-elm

It's an ant innit.
Elm
2
star