Skip to content

Commit a4b403d

Browse files
committed
Merge pull request #63 from hbetts/feat/scopes/support
feat(scopes): Add npm scope support.
2 parents f2ddb26 + 4a0fb7b commit a4b403d

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

Diff for: README.md

+10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ gulpLoadPlugins({
8080
});
8181
```
8282

83+
## npm Scopes
84+
85+
`gulp-load-plugins` comes with [npm scope](https://docs.npmjs.com/misc/scope) support. The major difference is that scopped plugins are accessible through a variable 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:
86+
87+
```js
88+
var plugins = require('gulp-load-plugins')();
89+
90+
plugins.myco.testPlugin();
91+
```
92+
8393
## Lazy Loading
8494

8595
In 0.4.0 and prior, lazy loading used to only work with plugins that return a function. In newer versions though, lazy loading should work for any plugin. If you have a problem related to this please try disabling lazy loading and see if that fixes it. Feel free to open an issue on this repo too.

Diff for: index.js

+31-9
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = function(options) {
1919
var requireFn;
2020
options = options || {};
2121

22-
var pattern = arrayify(options.pattern || ['gulp-*', 'gulp.*']);
22+
var pattern = arrayify(options.pattern || ['gulp-*', 'gulp.*', '@*/gulp{-,.}*']);
2323
var config = options.config || findup('package.json', {cwd: parentDir});
2424
var scope = arrayify(options.scope || ['dependencies', 'devDependencies', 'peerDependencies']);
2525
var replaceString = options.replaceString || /^gulp(-|\.)/;
@@ -52,7 +52,19 @@ module.exports = function(options) {
5252

5353
pattern.push('!gulp-load-plugins');
5454

55-
multimatch(names, pattern).forEach(function(name) {
55+
function defineProperty(object, requireName, name) {
56+
if(lazy) {
57+
Object.defineProperty(object, requireName, {
58+
get: function() {
59+
return requireFn(name);
60+
}
61+
});
62+
} else {
63+
object[requireName] = requireFn(name);
64+
}
65+
}
66+
67+
function getRequireName(name) {
5668
var requireName;
5769

5870
if(renameObj[name]) {
@@ -62,15 +74,25 @@ module.exports = function(options) {
6274
requireName = camelizePluginName ? camelize(requireName) : requireName;
6375
}
6476

65-
if(lazy) {
66-
Object.defineProperty(finalObject, requireName, {
67-
get: function() {
68-
return requireFn(name);
69-
}
70-
});
77+
return requireName;
78+
}
79+
80+
var scopeTest = new RegExp('^@');
81+
var scopeDecomposition = new RegExp('^@(.+)/(.+)');
82+
83+
multimatch(names, pattern).forEach(function(name) {
84+
if(scopeTest.test(name)) {
85+
var decomposition = scopeDecomposition.exec(name);
86+
87+
if(!finalObject.hasOwnProperty(decomposition[1])) {
88+
finalObject[decomposition[1]] = {};
89+
}
90+
91+
defineProperty(finalObject[decomposition[1]], getRequireName(decomposition[2]), name);
7192
} else {
72-
finalObject[requireName] = requireFn(name);
93+
defineProperty(finalObject, getRequireName(name), name);
7394
}
95+
7496
});
7597

7698
return finalObject;

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"Nicolas Froidure",
2525
"Sindre Sorhus",
2626
"Julien Fontanet <[email protected]>",
27-
"Callum Macrae"
27+
"Callum Macrae",
28+
"Hutson Betts"
2829
],
2930
"license": "MIT",
3031
"dependencies": {

Diff for: test/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ var gulpLoadPlugins = (function() {
2323
'wrap': wrapInFunc({ name: 'insert.wrap' })
2424
},
2525
'gulp.baz': wrapInFunc({ name: 'baz' }),
26-
'findup-sync': function() { return null; }
26+
'findup-sync': function() { return null; },
27+
'@myco/gulp-test-plugin': wrapInFunc({ name: 'test' })
2728
});
2829
})();
2930

@@ -131,6 +132,15 @@ var commonTests = function(lazy) {
131132

132133
assert.deepEqual(x.bar(), { name: 'foo' });
133134
});
135+
136+
it('supports loading scopped package', function() {
137+
var x = gulpLoadPlugins({
138+
lazy: lazy,
139+
config: { dependencies: { '@myco/gulp-test-plugin': '1.0.0' } },
140+
});
141+
142+
assert.deepEqual(x.myco.testPlugin(), { name: 'test' });
143+
});
134144
};
135145

136146
describe('no lazy loading', function() {

0 commit comments

Comments
 (0)