From 87a23819ed944472a72dbca8f632925e15eee2eb Mon Sep 17 00:00:00 2001 From: Shane Osbourne Date: Tue, 17 Mar 2015 07:42:48 +0000 Subject: [PATCH] Adding support for inline plugins --- lib/async-tasks.js | 4 ++ lib/async.js | 30 ++++++++++++ lib/default-config.js | 8 ++++ package.json | 1 + test/specs/plugins/user.plugins.inline.js | 36 ++++++++++++++ .../plugins/user.plugins.inline.options.js | 47 +++++++++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 test/specs/plugins/user.plugins.inline.js create mode 100644 test/specs/plugins/user.plugins.inline.options.js diff --git a/lib/async-tasks.js b/lib/async-tasks.js index 030acf12e..46cdeec94 100644 --- a/lib/async-tasks.js +++ b/lib/async-tasks.js @@ -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 diff --git a/lib/async.js b/lib/async.js index 437717f45..6cf4a7c57 100644 --- a/lib/async.js +++ b/lib/async.js @@ -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 diff --git a/lib/default-config.js b/lib/default-config.js index 320588770..3e2dd3590 100644 --- a/lib/default-config.js +++ b/lib/default-config.js @@ -241,6 +241,14 @@ module.exports = { */ reloadDelay: 0, + /** + * User provided plugins + * @property plugins + * @type Array + * @default [] + */ + plugins: [], + /** * @property injectChanges * @type Boolean diff --git a/package.json b/package.json index 81dcd88bb..cabb68f15 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/specs/plugins/user.plugins.inline.js b/test/specs/plugins/user.plugins.inline.js new file mode 100644 index 000000000..5c08d881e --- /dev/null +++ b/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(); + }); +}); diff --git a/test/specs/plugins/user.plugins.inline.options.js b/test/specs/plugins/user.plugins.inline.options.js new file mode 100644 index 000000000..fd35500d6 --- /dev/null +++ b/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(); + }); +});