Skip to content

Commit

Permalink
Merge pull request #88 from callumacrae/rename-fn
Browse files Browse the repository at this point in the history
Added renameFn
  • Loading branch information
jackfranklin committed Oct 15, 2015
2 parents 22fb861 + 36485cf commit ace14ca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
```

Expand All @@ -80,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:
Expand Down
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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;
Expand Down
21 changes: 21 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit ace14ca

Please sign in to comment.