Skip to content

Commit e9e5e67

Browse files
Bret Ikeharajackfranklin
authored andcommitted
configure whether plugins should be nested or not. (#126)
* configure whether plugins should be nested or not. * rename nesting to scoped. * document the npm scoped config option * rename option to maintainScope * fix typos * add in specific error message about duplicate package names
1 parent 5f49d5f commit e9e5e67

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ gulpLoadPlugins({
6767
lazy: true, // whether the plugins should be lazy loaded on demand
6868
rename: {}, // a mapping of plugins to rename
6969
renameFn: function (name) { ... }, // a function to handle the renaming of plugins (the default works)
70-
postRequireTransforms: {} // see documentation below
70+
postRequireTransforms: {}, // see documentation below
71+
maintainScope: true // toggles loading all npm scopes like non-scoped packages
7172
});
7273
```
7374

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

137138
## npm Scopes
138139

139-
`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:
140+
`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.
141+
142+
For example, if the plugin is `@myco/gulp-test-plugin` then you can access the plugin as shown in the following example:
140143

141144
```js
142-
var plugins = require('gulp-load-plugins')();
145+
var scoped = require('gulp-load-plugins')({
146+
maintainScope: true,
147+
});
148+
149+
scoped.myco.testPlugin();
150+
151+
var nonScoped = require('gulp-load-plugins')({
152+
maintainScope: false,
153+
});
143154

144-
plugins.myco.testPlugin();
155+
nonScoped.testPlugin();
145156
```
146157

147158
## Lazy Loading

index.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = function(options) {
4545
var camelizePluginName = options.camelize !== false;
4646
var lazy = 'lazy' in options ? !!options.lazy : true;
4747
var renameObj = options.rename || {};
48+
var maintainScope = 'maintainScope' in options ? !!options.maintainScope : true;
4849

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

@@ -88,10 +89,14 @@ module.exports = function(options) {
8889
}
8990
}
9091

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

97102
if (lazy) {
@@ -139,16 +144,19 @@ module.exports = function(options) {
139144

140145
unique(micromatch(names, pattern)).forEach(function(name) {
141146
var decomposition;
147+
var fObject = finalObject;
142148
if (scopeTest.test(name)) {
143149
decomposition = scopeDecomposition.exec(name);
144-
145-
if (!finalObject.hasOwnProperty(decomposition[1])) {
146-
finalObject[decomposition[1]] = {};
150+
if (maintainScope) {
151+
if (!fObject.hasOwnProperty(decomposition[1])) {
152+
finalObject[decomposition[1]] = {};
153+
}
154+
fObject = finalObject[decomposition[1]];
147155
}
148156

149-
defineProperty(finalObject[decomposition[1]], applyTransform, getRequireName(decomposition[2]), name);
157+
defineProperty(fObject, applyTransform, getRequireName(decomposition[2]), name, maintainScope);
150158
} else {
151-
defineProperty(finalObject, applyTransform, getRequireName(name), name);
159+
defineProperty(fObject, applyTransform, getRequireName(name), name, maintainScope);
152160
}
153161
});
154162

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"Carlos Henrique",
3535
"iamfrontender <[email protected]>",
3636
"Brian Woodward",
37-
"Zach Schnackel <[email protected]>"
37+
"Zach Schnackel <[email protected]>",
38+
"Bret K. Ikehara <[email protected]>"
3839
],
3940
"dependencies": {
4041
"array-unique": "^0.2.1",

test/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ describe('configuration', function() {
5454
});
5555
}, /Could not define the property "bar", you may have repeated dependencies in your package.json like "gulp-bar" and "bar"/);
5656
});
57+
58+
it("throws a nice error if there're repeated package names pattern in package.json ", function() {
59+
assert.throws(function() {
60+
gulpLoadPlugins({
61+
config: {
62+
dependencies: {
63+
'@foo/gulp-bar': '*',
64+
'gulp-bar': '~0.0.12'
65+
}
66+
},
67+
maintainScope: false
68+
});
69+
}, /Could not define the property "bar", you may have repeated a dependency in another scope like "gulp-bar" and "@foo\/gulp-bar"/);
70+
});
5771
});
5872

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

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

195+
it('supports loading scopped package as a top-level reference', function() {
196+
var x = gulpLoadPlugins({
197+
lazy: lazy,
198+
maintainScope: false,
199+
config: { dependencies: { '@myco/gulp-test-plugin': '1.0.0' } }
200+
});
201+
202+
assert.deepEqual(x.testPlugin(), { name: 'test' });
203+
});
204+
181205
it('supports custom rename functions', function () {
182206
var x = gulpLoadPlugins({
183207
renameFn: function () {

0 commit comments

Comments
 (0)