From 4dac6d5c3f7dc2baea0e6eea4961d8f5e93f1c56 Mon Sep 17 00:00:00 2001 From: Bret Ikehara Date: Fri, 14 Oct 2016 23:18:40 -0700 Subject: [PATCH 1/6] configure whether plugins should be nested or not. --- index.js | 14 +++++++++----- test/index.js | 12 +++++++++++- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index f75a8f5..10bc74f 100644 --- a/index.js +++ b/index.js @@ -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 nested = 'nested' in options ? !!options.nested : true; logDebug('Debug enabled with options: ' + JSON.stringify(options)); @@ -139,16 +140,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 (nested) { + 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); } else { - defineProperty(finalObject, applyTransform, getRequireName(name), name); + defineProperty(fObject, applyTransform, getRequireName(name), name); } }); diff --git a/test/index.js b/test/index.js index 9004af6..cfe09a4 100644 --- a/test/index.js +++ b/test/index.js @@ -169,7 +169,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 nested', function() { var x = gulpLoadPlugins({ lazy: lazy, config: { dependencies: { '@myco/gulp-test-plugin': '1.0.0' } } @@ -178,6 +178,16 @@ var commonTests = function(lazy) { assert.deepEqual(x.myco.testPlugin(), { name: 'test' }); }); + it('supports loading scopped package not nested', function() { + var x = gulpLoadPlugins({ + lazy: lazy, + nested: 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 () { From 952634613ea112eb4df1e085bf0bbd8a2d828374 Mon Sep 17 00:00:00 2001 From: Bret Ikehara Date: Fri, 14 Oct 2016 23:26:53 -0700 Subject: [PATCH 2/6] rename nesting to scoped. --- index.js | 4 ++-- test/index.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 10bc74f..b3aabdb 100644 --- a/index.js +++ b/index.js @@ -45,7 +45,7 @@ module.exports = function(options) { var camelizePluginName = options.camelize !== false; var lazy = 'lazy' in options ? !!options.lazy : true; var renameObj = options.rename || {}; - var nested = 'nested' in options ? !!options.nested : true; + var scoped = 'scoped' in options ? !!options.scoped : true; logDebug('Debug enabled with options: ' + JSON.stringify(options)); @@ -143,7 +143,7 @@ module.exports = function(options) { var fObject = finalObject; if (scopeTest.test(name)) { decomposition = scopeDecomposition.exec(name); - if (nested) { + if (scoped) { if (!fObject.hasOwnProperty(decomposition[1])) { finalObject[decomposition[1]] = {}; } diff --git a/test/index.js b/test/index.js index cfe09a4..baf21b2 100644 --- a/test/index.js +++ b/test/index.js @@ -169,7 +169,7 @@ var commonTests = function(lazy) { assert(output.indexOf('gulp-load-plugins') !== -1, 'Expected output to be logged to stdout'); }); - it('supports loading scopped package nested', 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' } } @@ -178,10 +178,10 @@ var commonTests = function(lazy) { assert.deepEqual(x.myco.testPlugin(), { name: 'test' }); }); - it('supports loading scopped package not nested', function() { + it('supports loading scopped package as a top-level reference', function() { var x = gulpLoadPlugins({ lazy: lazy, - nested: false, + scoped: false, config: { dependencies: { '@myco/gulp-test-plugin': '1.0.0' } } }); From 0b1ee635a5d82a50077748b8bfa4bb6f3e40a897 Mon Sep 17 00:00:00 2001 From: Bret Ikehara Date: Mon, 24 Oct 2016 23:55:21 -0700 Subject: [PATCH 3/6] document the npm scoped config option --- README.md | 19 +++++++++++++++---- package.json | 3 ++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8c257a5..132a1de 100644 --- a/README.md +++ b/README.md @@ -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 + scoped: true // loads all npm scopes like non-scoped packages }); ``` @@ -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 `scoped = false`, the plugins are availble 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')({ + scoped: true, +}); + +scoped.myco.testPlugin(); + +var nonScoped = require('gulp-load-plugins')({ + scoped: false, +}); -plugins.myco.testPlugin(); +nonScoped.testPlugin(); ``` ## Lazy Loading diff --git a/package.json b/package.json index 27066de..3a69610 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ "Carlos Henrique", "iamfrontender ", "Brian Woodward", - "Zach Schnackel " + "Zach Schnackel ", + "Bret K. Ikehara " ], "dependencies": { "array-unique": "^0.2.1", From 38db904ce97337858a420e15355407d720f150f7 Mon Sep 17 00:00:00 2001 From: Bret Ikehara Date: Tue, 25 Oct 2016 12:00:40 -0700 Subject: [PATCH 4/6] rename option to maintainScope --- README.md | 8 ++++---- index.js | 4 ++-- test/index.js | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 132a1de..9749a85 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ gulpLoadPlugins({ 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 - scoped: true // loads all npm scopes like non-scoped packages + maintainScope: true // toggles loadin all npm scopes like non-scoped packages }); ``` @@ -137,19 +137,19 @@ 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. By default, the scoped plugins are accessible through an object on `plugins` that represents the scope. When `scoped = false`, the plugins are availble in the top level just like any other non-scoped plugins. +`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 availble 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 scoped = require('gulp-load-plugins')({ - scoped: true, + maintainScope: true, }); scoped.myco.testPlugin(); var nonScoped = require('gulp-load-plugins')({ - scoped: false, + maintainScope: false, }); nonScoped.testPlugin(); diff --git a/index.js b/index.js index b3aabdb..d7ab436 100644 --- a/index.js +++ b/index.js @@ -45,7 +45,7 @@ module.exports = function(options) { var camelizePluginName = options.camelize !== false; var lazy = 'lazy' in options ? !!options.lazy : true; var renameObj = options.rename || {}; - var scoped = 'scoped' in options ? !!options.scoped : true; + var maintainScope = 'maintainScope' in options ? !!options.maintainScope : true; logDebug('Debug enabled with options: ' + JSON.stringify(options)); @@ -143,7 +143,7 @@ module.exports = function(options) { var fObject = finalObject; if (scopeTest.test(name)) { decomposition = scopeDecomposition.exec(name); - if (scoped) { + if (maintainScope) { if (!fObject.hasOwnProperty(decomposition[1])) { finalObject[decomposition[1]] = {}; } diff --git a/test/index.js b/test/index.js index baf21b2..24397c3 100644 --- a/test/index.js +++ b/test/index.js @@ -181,7 +181,7 @@ var commonTests = function(lazy) { it('supports loading scopped package as a top-level reference', function() { var x = gulpLoadPlugins({ lazy: lazy, - scoped: false, + maintainScope: false, config: { dependencies: { '@myco/gulp-test-plugin': '1.0.0' } } }); From ffd8c74cd51aae2aa1a98bc0b10c0d2b550b1855 Mon Sep 17 00:00:00 2001 From: Bret Ikehara Date: Wed, 26 Oct 2016 09:24:07 -0700 Subject: [PATCH 5/6] fix typos --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9749a85..e2f31be 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ gulpLoadPlugins({ 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 - maintainScope: true // toggles loadin all npm scopes like non-scoped packages + maintainScope: true // toggles loading all npm scopes like non-scoped packages }); ``` @@ -137,7 +137,7 @@ 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. By default, the scoped plugins are accessible through an object on `plugins` that represents the scope. When `maintainScope = false`, the plugins are availble in the top level just like any other non-scoped plugins. +`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: From b7e001714ffdce238b0ca3b5a9bdd57d6f5137a8 Mon Sep 17 00:00:00 2001 From: Bret Ikehara Date: Thu, 27 Oct 2016 14:24:17 -0700 Subject: [PATCH 6/6] add in specific error message about duplicate package names --- index.js | 12 ++++++++---- test/index.js | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index d7ab436..5cdd6c2 100644 --- a/index.js +++ b/index.js @@ -89,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) { @@ -150,9 +154,9 @@ module.exports = function(options) { fObject = finalObject[decomposition[1]]; } - defineProperty(fObject, applyTransform, getRequireName(decomposition[2]), name); + defineProperty(fObject, applyTransform, getRequireName(decomposition[2]), name, maintainScope); } else { - defineProperty(fObject, applyTransform, getRequireName(name), name); + defineProperty(fObject, applyTransform, getRequireName(name), name, maintainScope); } }); diff --git a/test/index.js b/test/index.js index 24397c3..f43100a 100644 --- a/test/index.js +++ b/test/index.js @@ -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.