auto-plug
Auto-require plugin packages by prefix. (for i.e. Gulp, Grunt or other heavy plugin-dependent packages)
Install
$ npm install auto-plug
Usage
auto-plug will return an object containing the required module exports.
If your config data (package.json) contains package names like foo-this
and
foo-that
, they can be autoloaded by
var plugins = 'foo'`
and then accessed by plugins.this()
or plugins.that()
.
Instead of a simple prefix string you can also set custom options:
var plugins = prefix: 'foo' lazy: false ;
with Gulp
Just set the prefix option to gulp
:
// Gulpfile.jsvar gulp =gulpPlugins = 'gulp';gulp;
with Grunt
Grunt needs it's own require function:
// Gruntfile.jsmodule {prefix: 'grunt' require: gruntloadNpmTasks ;// do grunt things as usual}
with any other thing
… i.e. with Metalsmith:
var metalsmith =metalsmithPlugins = 'metalsmith';Metalsmithsource'.';
Tip
If you already loaded your package.json's data, pass it as config option to speed up things:
var pkg =plugins = prefix: 'foo' config: pkg ;
Options
You either have to define a prefix
or a pattern
and replaceExp
. All other options are optional.
-
prefix
can be used to quickly definepattern
andreplaceExpr
at once (see default options) -
pattern
(default:[prefix + '-*', prefix + '.*']
)
a globbing pattern to find packages in config for require -
replaceExpr
(default:new RegExp('^' + prefix + '([\.-])')
)
a regular expression for what shall be removed from a package name when adding to container object -
scope
(default:['dependencies', 'devDependencies']
)
which keys in config object contain packages to require -
module
(default: the module that executedrequire('auto-plug')
) The module used to find the defaultconfig
andrequireFn
options -
config
(default:module
option's package.json data)
the config where auto-plug will look for packages to require; can be a plain object or a string containing a path to require -
requireFn
(default:module
option'srequire
property)
the function to be used for requiring packages -
camelize
(default:true
)
whether package names should be converted to camelcase when adding to container object or not -
lazy
(default:true
)
whether packages should be lazy-loaded (loaded when called for the first time) or directly when calling auto-plug -
rename
(default:{}
)
a plain object for custom renaming; keys are original package names and values the respective rename string
Default options
prefix: undefinedpattern: prefix + '-*' prefix + '.*'replaceExpr: '^' + prefix + '([\.-])'scope: 'dependencies' 'devDependencies'module: moduleparent // the module that require()'d auto-plugconfig:requireFn: thisoptionsmodulerequirecamelize: truelazy: truerename: {}
API Usage
// get your AutoPlug instancevar AutoPlug = AutoPlugautoPlug = 'gulp';// find matching packages, require them and add them to containerautoPlug;// manually add a package to containerautoPlug;// get the containervar g = autoPlug;