Skip to content

Commit

Permalink
Adding support for inline plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Mar 17, 2015
1 parent 48cfc6f commit 87a2381
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/async-tasks.js
Expand Up @@ -45,6 +45,10 @@ module.exports = [
step: "Merge UI settings",
fn: async.mergeUiSettings
},
{
step: "Resolve user plugins from options",
fn: async.resolveInlineUserPlugins
},
{
step: "Init user plugins",
fn: async.initUserPlugins
Expand Down
30 changes: 30 additions & 0 deletions lib/async.js
Expand Up @@ -242,6 +242,36 @@ module.exports = {
}
});
},
/**
* Try to load plugins that were given in options
* @param {BrowserSync} bs
* @param {Function} done
*/
resolveInlineUserPlugins: function (bs, done) {

bs.options.get("plugins").forEach(function (item) {

if (_.isString(item)) {
loadPlugin(item);
}

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

function loadPlugin (name, opts) {
try {
bs.registerPlugin(require(name), opts);
} catch (e) {
bs.logger.error("NOPE");
}
}

done();
},
/**
* @param {BrowserSync} bs
* @param {Function} done
Expand Down
8 changes: 8 additions & 0 deletions lib/default-config.js
Expand Up @@ -241,6 +241,14 @@ module.exports = {
*/
reloadDelay: 0,

/**
* User provided plugins
* @property plugins
* @type Array
* @default []
*/
plugins: [],

/**
* @property injectChanges
* @type Boolean
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -57,6 +57,7 @@
"ua-parser-js": "^0.7.3"
},
"devDependencies": {
"bs-html-injector": "^1.3.0",
"chai": "^2.1.0",
"chalk": "^1.0.0",
"cli-color": "^0.3.2",
Expand Down
36 changes: 36 additions & 0 deletions test/specs/plugins/user.plugins.inline.js
@@ -0,0 +1,36 @@
"use strict";

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

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

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

var instance;
var PLUGIN_REQUIRE = "bs-html-injector";
var PLUGIN_NAME = "HTML Injector";

before(function (done) {

browserSync.reset();

var config = {
logLevel: "silent",
plugins: [PLUGIN_REQUIRE]
};

instance = browserSync(config, done).instance;
});
after(function () {
instance.cleanup();
});
it("Should access to only the user-specified plugins", function (done) {
assert.equal(instance.getUserPlugins().length, 1);
done();
});
it("Should access to only the user-specified plugins", function (done) {
var plugin = instance.getUserPlugins()[0];
assert.equal(plugin.name, PLUGIN_NAME);
done();
});
});
47 changes: 47 additions & 0 deletions test/specs/plugins/user.plugins.inline.options.js
@@ -0,0 +1,47 @@
"use strict";

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

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

describe("Plugins: Retrieving user plugins when given inline with options", function () {

var instance;
var PLUGIN_NAME = "HTML Injector";

before(function (done) {

browserSync.reset();

var plugin = {
"bs-html-injector": {
files: "*.html"
}
};

var config = {
logLevel: "silent",
plugins: [
{
"bs-html-injector": {
files: "*.html"
}
}
]
};

instance = browserSync(config, done).instance;
});
after(function () {
instance.cleanup();
});
it("Should access to only the user-specified plugins", function (done) {
assert.equal(instance.getUserPlugins().length, 1);
done();
});
it("Should access to only the user-specified plugins", function (done) {
var plugin = instance.getUserPlugins()[0];
assert.equal(plugin.name, PLUGIN_NAME);
done();
});
});

0 comments on commit 87a2381

Please sign in to comment.