Skip to content

Commit

Permalink
WIP 6: Scoped prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jul 23, 2018
1 parent 19034c0 commit 076d066
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ assert(app.plugins.express);

The plugins are registered as `router` and `express`, but the npm modules which were required were called 'monkey-router' and 'monkey-express'.

Scoped prefixes also work. e.g. with prefix '@monkey', 'routes' will be loaded from '@monkey/routes'.

NB `PLUGIN_PREFIX` is currently a string, but it may be changed to a `Symbol` in a future version. This will not be considered a semver major change, so always use it via `Pluggi.PLUGIN_PREFIX`.

### Options
Expand Down
7 changes: 6 additions & 1 deletion lib/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ module.exports = class Plugins extends Plugin {
* @returns {PluginsPlugin} - plugin for chaining
*/
addPrefix(prefix) {
if (
(prefix[0] == '@' && prefix.slice(-1) == '/') || // jshint ignore:line
prefix.slice(-1) == '-'
) prefix = prefix.slice(0, -1);
this.prefixes.push(prefix);
return this;
}
Expand Down Expand Up @@ -76,7 +80,8 @@ module.exports = class Plugins extends Plugin {
resolveModule(name) {
// Attempt to resolve as module with prefixes
for (let prefix of this.prefixes) {
const path = `${prefix}-${name}`;
const separator = (prefix[0] == '@' && !prefix.includes('/')) ? '/' : '-'; // jshint ignore:line
const path = `${prefix}${separator}${name}`;
if (tryResolve(path)) return path;
}

Expand Down
2 changes: 2 additions & 0 deletions test/all.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ describe('`.plugin()`', function() {
localOptions: true
});
});

// TODO Add tests for @-scoped prefix
});

describe('with arguments (name)', function() {
Expand Down

0 comments on commit 076d066

Please sign in to comment.