Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added renameFn #88

Merged
merged 2 commits into from
Oct 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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