diff --git a/README.md b/README.md index f7404d9..cd890ac 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.js b/index.js index 0355a3d..1965a87 100644 --- a/index.js +++ b/index.js @@ -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(-|\.)/; @@ -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]) { @@ -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; diff --git a/package.json b/package.json index 72fa8b6..6bccddd 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "Nicolas Froidure", "Sindre Sorhus", "Julien Fontanet ", - "Callum Macrae" + "Callum Macrae", + "Hutson Betts" ], "license": "MIT", "dependencies": { diff --git a/test/index.js b/test/index.js index b90da45..51d8af7 100644 --- a/test/index.js +++ b/test/index.js @@ -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' }) }); })(); @@ -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() {