Auto-require plugin packages by prefix. (for i.e. Gulp, Grunt or other heavy plugin-dependent packages)
$ npm install auto-plugauto-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
varplugins=require('auto-plug')('foo')`and then accessed by plugins.this()or plugins.that().
Instead of a simple prefix string you can also set custom options:
varplugins=require('auto-plug')({prefix: 'foo',lazy: false});Just set the prefix option to gulp:
// Gulpfile.jsvargulp=require('gulp'),gulpPlugins=require('auto-plug')('gulp');gulp.task('default',function(){returngulp.src('some/glob').pipe(gulpPlugins.someGulpThing()).pipe(gulpPlugins.someOtherGulpThing())// ...}});Grunt needs it's own require function:
// Gruntfile.jsmodule.exports=function(grunt){require('auto-plug')({prefix: 'grunt',require: grunt.loadNpmTasks});// do grunt things as usual}… i.e. with Metalsmith:
varmetalsmith=require('metalsmith')metalsmithPlugins=require('auto-plug')('metalsmith');Metalsmith.source('.').use(metalsmithPlugins.someMetalsmithThing()).use(metalsmithPlugins.someOtherMetalsmithThing()).build();If you already loaded your package.json's data, pass it as config option to speed up things:
varpkg=require(process.cwd()+'/package.json'),plugins=require('auo-plug')({prefix: 'foo',config: pkg});You either have to define a prefix or a pattern and replaceExp. All other options are optional.
prefix
can be used to quickly definepatternandreplaceExprat once (see default options)pattern
(default:[prefix + '-*', prefix + '.*'])
a globbing pattern to find packages in config for requirereplaceExpr
(default:new RegExp('^' + prefix + '([\.-])'))
a regular expression for what shall be removed from a package name when adding to container objectscope
(default:['dependencies', 'devDependencies'])
which keys in config object contain packages to requiremodule(default: the module that executedrequire('auto-plug')) The module used to find the defaultconfigandrequireFnoptionsconfig
(default:moduleoption'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 requirerequireFn
(default:moduleoption'srequireproperty)
the function to be used for requiring packagescamelize
(default:true)
whether package names should be converted to camelcase when adding to container object or notlazy
(default:true)
whether packages should be lazy-loaded (loaded when called for the first time) or directly when calling auto-plugrename
(default:{})
a plain object for custom renaming; keys are original package names and values the respective rename string
{prefix: undefined,pattern: [prefix+'-*',prefix+'.*'],replaceExpr: newRegExp('^'+prefix+'([\.-])'),scope: ['dependencies','devDependencies'],module: module.parent,// the module that require()'d auto-plugconfig: findup('package.json',{cwd: path.dirname(this.options.module.filename)}),requireFn: this.options.module.require.bind(this.options.module),camelize: true,lazy: true,rename: {}}// get your AutoPlug instancevarAutoPlug=require('auto-plug').AutoPlug,autoPlug=newAutoPlug('gulp');// find matching packages, require them and add them to containerautoPlug.plug();// manually add a package to containerautoPlug.addPackageToContainer('runSequence');// get the containervarg=autoPlug.getContainer();