Skip to content

feat(scopes): Add npm scope support. #63

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

Merged
merged 2 commits into from
Mar 20, 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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ gulpLoadPlugins({
});
```

## npm Scopes

`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:

```js
var plugins = require('gulp-load-plugins')();

plugins.myco.testPlugin();
```

## Lazy Loading

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.
Expand Down
40 changes: 31 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function(options) {
var requireFn;
options = options || {};

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

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

multimatch(names, pattern).forEach(function(name) {
function defineProperty(object, requireName, name) {
if(lazy) {
Object.defineProperty(object, requireName, {
get: function() {
return requireFn(name);
}
});
} else {
object[requireName] = requireFn(name);
}
}

function getRequireName(name) {
var requireName;

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

if(lazy) {
Object.defineProperty(finalObject, requireName, {
get: function() {
return requireFn(name);
}
});
return requireName;
}

var scopeTest = new RegExp('^@');
var scopeDecomposition = new RegExp('^@(.+)/(.+)');

multimatch(names, pattern).forEach(function(name) {
if(scopeTest.test(name)) {
var decomposition = scopeDecomposition.exec(name);

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

defineProperty(finalObject[decomposition[1]], getRequireName(decomposition[2]), name);
} else {
finalObject[requireName] = requireFn(name);
defineProperty(finalObject, getRequireName(name), name);
}

});

return finalObject;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"Nicolas Froidure",
"Sindre Sorhus",
"Julien Fontanet <julien.fontanet@vates.fr>",
"Callum Macrae"
"Callum Macrae",
"Hutson Betts"
],
"license": "MIT",
"dependencies": {
Expand Down
12 changes: 11 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var gulpLoadPlugins = (function() {
'wrap': wrapInFunc({ name: 'insert.wrap' })
},
'gulp.baz': wrapInFunc({ name: 'baz' }),
'findup-sync': function() { return null; }
'findup-sync': function() { return null; },
'@myco/gulp-test-plugin': wrapInFunc({ name: 'test' })
});
})();

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

assert.deepEqual(x.bar(), { name: 'foo' });
});

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

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

describe('no lazy loading', function() {
Expand Down