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

configure whether plugins should be nested or not. #126

Merged
merged 6 commits into from
Oct 31, 2016
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
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ gulpLoadPlugins({
lazy: true, // whether the plugins should be lazy loaded on demand
rename: {}, // a mapping of plugins to rename
renameFn: function (name) { ... }, // a function to handle the renaming of plugins (the default works)
postRequireTransforms: {} // see documentation below
postRequireTransforms: {}, // see documentation below
maintainScope: true // toggles loading all npm scopes like non-scoped packages
});
```

Expand Down Expand Up @@ -136,12 +137,22 @@ Note that if you specify the `renameFn` options with your own custom rename func

## 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:
`gulp-load-plugins` comes with [npm scope](https://docs.npmjs.com/misc/scope) support. By default, the scoped plugins are accessible through an object on `plugins` that represents the scope. When `maintainScope = false`, the plugins are available in the top level just like any other non-scoped plugins.

For example, if the plugin is `@myco/gulp-test-plugin` then you can access the plugin as shown in the following example:

```js
var plugins = require('gulp-load-plugins')();
var scoped = require('gulp-load-plugins')({
maintainScope: true,
});

scoped.myco.testPlugin();

var nonScoped = require('gulp-load-plugins')({
maintainScope: false,
});

plugins.myco.testPlugin();
nonScoped.testPlugin();
```

## Lazy Loading
Expand Down
22 changes: 15 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module.exports = function(options) {
var camelizePluginName = options.camelize !== false;
var lazy = 'lazy' in options ? !!options.lazy : true;
var renameObj = options.rename || {};
var maintainScope = 'maintainScope' in options ? !!options.maintainScope : true;

logDebug('Debug enabled with options: ' + JSON.stringify(options));

Expand Down Expand Up @@ -88,10 +89,14 @@ module.exports = function(options) {
}
}

function defineProperty(object, transform, requireName, name) {
function defineProperty(object, transform, requireName, name, maintainScope) {
var err;
if (object[requireName]) {
logDebug('error: defineProperty ' + name);
throw new Error('Could not define the property "' + requireName + '", you may have repeated dependencies in your package.json like' + ' "gulp-' + requireName + '" and ' + '"' + requireName + '"');
err = maintainScope ?
'Could not define the property "' + requireName + '", you may have repeated dependencies in your package.json like' + ' "gulp-' + requireName + '" and ' + '"' + requireName + '"' :
'Could not define the property "' + requireName + '", you may have repeated a dependency in another scope like' + ' "gulp-' + requireName + '" and ' + '"@foo/gulp-' + requireName + '"';
throw new Error(err);
}

if (lazy) {
Expand Down Expand Up @@ -139,16 +144,19 @@ module.exports = function(options) {

unique(micromatch(names, pattern)).forEach(function(name) {
var decomposition;
var fObject = finalObject;
if (scopeTest.test(name)) {
decomposition = scopeDecomposition.exec(name);

if (!finalObject.hasOwnProperty(decomposition[1])) {
finalObject[decomposition[1]] = {};
if (maintainScope) {
if (!fObject.hasOwnProperty(decomposition[1])) {
finalObject[decomposition[1]] = {};
}
fObject = finalObject[decomposition[1]];
}

defineProperty(finalObject[decomposition[1]], applyTransform, getRequireName(decomposition[2]), name);
defineProperty(fObject, applyTransform, getRequireName(decomposition[2]), name, maintainScope);
} else {
defineProperty(finalObject, applyTransform, getRequireName(name), name);
defineProperty(fObject, applyTransform, getRequireName(name), name, maintainScope);
}
});

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"Carlos Henrique",
"iamfrontender <iamfrontender@gmail.com>",
"Brian Woodward",
"Zach Schnackel <info@zslabs.com>"
"Zach Schnackel <info@zslabs.com>",
"Bret K. Ikehara <bret.k.ikehara@gmail.com>"
],
"dependencies": {
"array-unique": "^0.2.1",
Expand Down
26 changes: 25 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ describe('configuration', function() {
});
}, /Could not define the property "bar", you may have repeated dependencies in your package.json like "gulp-bar" and "bar"/);
});

it("throws a nice error if there're repeated package names pattern in package.json ", function() {
assert.throws(function() {
gulpLoadPlugins({
config: {
dependencies: {
'@foo/gulp-bar': '*',
'gulp-bar': '~0.0.12'
}
},
maintainScope: false
});
}, /Could not define the property "bar", you may have repeated a dependency in another scope like "gulp-bar" and "@foo\/gulp-bar"/);
});
});

// Contains common tests with and without lazy mode.
Expand Down Expand Up @@ -169,7 +183,7 @@ var commonTests = function(lazy) {
assert(output.indexOf('gulp-load-plugins') !== -1, 'Expected output to be logged to stdout');
});

it('supports loading scopped package', function() {
it('supports loading scopped package as a nested reference', function() {
var x = gulpLoadPlugins({
lazy: lazy,
config: { dependencies: { '@myco/gulp-test-plugin': '1.0.0' } }
Expand All @@ -178,6 +192,16 @@ var commonTests = function(lazy) {
assert.deepEqual(x.myco.testPlugin(), { name: 'test' });
});

it('supports loading scopped package as a top-level reference', function() {
var x = gulpLoadPlugins({
lazy: lazy,
maintainScope: false,
config: { dependencies: { '@myco/gulp-test-plugin': '1.0.0' } }
});

assert.deepEqual(x.testPlugin(), { name: 'test' });
});

it('supports custom rename functions', function () {
var x = gulpLoadPlugins({
renameFn: function () {
Expand Down