From e5838e3f675e813dc5d58c212a9f06393687dc87 Mon Sep 17 00:00:00 2001 From: Callum Macrae Date: Wed, 14 Oct 2015 17:12:05 +0100 Subject: [PATCH 1/2] Added renameFn --- README.md | 3 ++- index.js | 10 +++++++--- test/index.js | 21 +++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e164c0e..a3939af 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,8 @@ gulpLoadPlugins({ 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 + rename: {}, // a mapping of plugins to rename + renameFn: function (name) { ... } // a function to handle the renaming of plugins (the default works) }); ``` diff --git a/index.js b/index.js index 64c16a5..d3fc52a 100644 --- a/index.js +++ b/index.js @@ -24,10 +24,15 @@ module.exports = function(options) { var config = options.config || findup('package.json', {cwd: parentDir}); var scope = arrayify(options.scope || ['dependencies', 'devDependencies', 'peerDependencies']); var replaceString = options.replaceString || /^gulp(-|\.)/; - var camelizePluginName = options.camelize === false ? false : true; + var camelizePluginName = options.camelize !== false; var lazy = 'lazy' in options ? !!options.lazy : true; var renameObj = options.rename || {}; + var renameFn = options.renameFn || function (name) { + name = name.replace(replaceString, ''); + return camelizePluginName ? camelize(name) : name; + }; + if(typeof options.requireFn === 'function') { requireFn = options.requireFn; } else if(typeof config === 'string') { @@ -71,8 +76,7 @@ module.exports = function(options) { if(renameObj[name]) { requireName = options.rename[name]; } else { - requireName = name.replace(replaceString, ''); - requireName = camelizePluginName ? camelize(requireName) : requireName; + requireName = renameFn(name); } return requireName; diff --git a/test/index.js b/test/index.js index d09535f..8671468 100644 --- a/test/index.js +++ b/test/index.js @@ -141,6 +141,27 @@ var commonTests = function(lazy) { assert.deepEqual(x.myco.testPlugin(), { name: 'test' }); }); + + it('supports custom rename functions', function () { + var x = gulpLoadPlugins({ + renameFn: function () { + return 'baz'; + }, + config: { + dependencies: { + 'gulp-foo-bar': '*' + } + } + }); + + assert.throws(function () { + x.fooBar(); + }); + + assert.deepEqual(x.baz(), { + name: 'foo-bar' + }); + }); }; describe('no lazy loading', function() { From 36485cfc9060fd29e4a35c925fc7d1a65949f5c8 Mon Sep 17 00:00:00 2001 From: Callum Macrae Date: Thu, 15 Oct 2015 10:22:03 +0100 Subject: [PATCH 2/2] Added more renameFn info to readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index a3939af..fab1c31 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,8 @@ gulpLoadPlugins({ }); ``` +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](https://docs.npmjs.com/misc/scope) support. The major difference is that scoped plugins are accessible through an object on `plugins` that represents the scope. For example, if the plugin is `@myco/gulp-test-plugin` then you can access the plugin as shown in the following example: