Skip to content

Commit

Permalink
Add 'loaders' option, as less-magical altenative to 'require' option. C…
Browse files Browse the repository at this point in the history
…loses GH-8

The 'require' option allowed passing the require() function, which would
be pre-populated with require('pluginame') in the calling module so
browserify could statically analyze the module dependencies. This was
somewhat confusing since calling require() when ignoring the return
value has non-obvious side-effects (browserify static analysis):

    require('voxel-skyhook');

    pluginOpts = {
        'voxel-skyhook': {},
        ...
    }

The new 'loaders' option to voxel-plugins is more explicit:

    loaders = {
        'voxel-skyhook': require('voxel-skyhook'),
        ...
    },

where the specific require()'d modules are passed in as object values.
This is somewhat more typing, but is clearer and easier to follow. The
old require option is still supported for compatibility, but 'loaders' is
preferred for clarity.
  • Loading branch information
deathcap committed Jan 10, 2016
1 parent c6c98ea commit 035a9ef
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function Plugins(game, opts) {
if (this.game) this.game.plugins = this;

opts = opts || {};
this.loaders = opts.loaders || {};
this.require = opts.require || require;
this.catchExceptions = false;
this.masterPluginName = opts.masterPluginName || 'voxel-engine'; // synthetic 'plugin' created as parent of all
Expand Down Expand Up @@ -48,7 +49,13 @@ Plugins.prototype.wrapExceptions = function(f) {
// Require the plugin module and return its factory constructor
// This does not construct the plugin instance, for that see instantiate()
Plugins.prototype.scan = function(name) {
var createPlugin = this.require(name); // factory for constructor
var createPlugin; // factory for constructor

if (name in this.loaders) {
createPlugin = this.loaders[name];
} else {
createPlugin = this.require(name);
}

return createPlugin;
};
Expand Down

0 comments on commit 035a9ef

Please sign in to comment.