Skip to content

Commit

Permalink
feat(plugins): Accept object literal as plugin + options
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Apr 9, 2015
1 parent 66337a4 commit 757f492
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 17 deletions.
13 changes: 7 additions & 6 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,19 @@ module.exports = {
}

if (Immutable.Map.isMap(item)) {
item.forEach(function (value, key) {
loadPlugin(key, value);
});
loadPlugin(item.get("module"), item.get("options"));
}
});

function loadPlugin (name, opts) {
try {
opts = opts ? opts.toJS() : {};
opts.moduleName = name;
bs.registerPlugin(require(name), opts);

if (_.isString(name)) {
opts.moduleName = name;
bs.registerPlugin(require(name), opts);
} else {
bs.registerPlugin(name.toJS(), opts);
}
} catch (e) {
if (e.code === "MODULE_NOT_FOUND") {
return bs.logger.setOnce("useLevelPrefixes", true).error("Plugin {yellow:`%s`} was not found, did you forget to {cyan:npm install} it?", name);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
},
"devDependencies": {
"browser-sync-spa": "^1.0.2",
"bs-html-injector": "^2.0.1",
"bs-snippet-injector": "^2.0.1",
"chai": "^2.1.0",
"chalk": "^1.0.0",
Expand Down
24 changes: 15 additions & 9 deletions test/specs/plugins/user.plugins.inline.enabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ describe("Plugins: Setting the default state (false) if given in options", funct

var config = {
logLevel: "silent",
plugins: [{
"bs-snippet-injector": {
enabled: false,
file: ""
plugins: [
{
module: "bs-snippet-injector",
options: {
enabled: false,
file: ""
}
}
}]
]
};

instance = browserSync(config, done).instance;
Expand All @@ -44,11 +47,14 @@ describe("Plugins: Setting the default state (true) if given in options", functi

var config = {
logLevel: "silent",
plugins: [{
"bs-snippet-injector": {
enabled: true
plugins: [
{
module: "bs-snippet-injector",
options: {
enabled: true
}
}
}]
]
};

instance = browserSync(config, done).instance;
Expand Down
53 changes: 53 additions & 0 deletions test/specs/plugins/user.plugins.inline.obj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";

var browserSync = require("../../../");

var assert = require("chai").assert;

describe("Plugins: Retrieving user plugins when given inline as object", function () {

var instance;
var PLUGIN_NAME = "Test Plugin";

before(function (done) {

browserSync.reset();

var config = {
logLevel: "silent",
plugins: [
{
module: {
plugin: function () {
done();
},
"plugin:name": PLUGIN_NAME
},
options: {
files: "*.html"
}
}
]
};

instance = browserSync(config).instance;
});
after(function () {
instance.cleanup();
});
it("Should access to only the user-specified plugins", function (done) {
console.log(instance.getUserPlugins());
assert.equal(instance.getUserPlugins().length, 1);
done();
});
it("Should have access to only the user-specified plugins", function (done) {
var plugin = instance.getUserPlugins()[0];
assert.equal(plugin.name, PLUGIN_NAME);
done();
});
it("should have access to user provided opts", function (done) {
var plugin = instance.getUserPlugins()[0];
assert.equal(plugin.opts.files, "*.html");
done();
});
});
5 changes: 3 additions & 2 deletions test/specs/plugins/user.plugins.inline.options.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ describe("Plugins: Retrieving user plugins when given inline with options", func
logLevel: "silent",
plugins: [
{
"bs-snippet-injector": {
module: "bs-snippet-injector",
options: {
files: "*.html"
}
}
Expand All @@ -34,7 +35,7 @@ describe("Plugins: Retrieving user plugins when given inline with options", func
assert.equal(instance.getUserPlugins().length, 1);
done();
});
it("Should access to only the user-specified plugins", function (done) {
it("Should have access to only the user-specified plugins", function (done) {
var plugin = instance.getUserPlugins()[0];
assert.equal(plugin.name, PLUGIN_NAME);
done();
Expand Down

0 comments on commit 757f492

Please sign in to comment.