Skip to content

Commit 757f492

Browse files
committed
feat(plugins): Accept object literal as plugin + options
1 parent 66337a4 commit 757f492

File tree

5 files changed

+79
-17
lines changed

5 files changed

+79
-17
lines changed

lib/async.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,19 @@ module.exports = {
6969
}
7070

7171
if (Immutable.Map.isMap(item)) {
72-
item.forEach(function (value, key) {
73-
loadPlugin(key, value);
74-
});
72+
loadPlugin(item.get("module"), item.get("options"));
7573
}
7674
});
7775

7876
function loadPlugin (name, opts) {
7977
try {
8078
opts = opts ? opts.toJS() : {};
81-
opts.moduleName = name;
82-
bs.registerPlugin(require(name), opts);
83-
79+
if (_.isString(name)) {
80+
opts.moduleName = name;
81+
bs.registerPlugin(require(name), opts);
82+
} else {
83+
bs.registerPlugin(name.toJS(), opts);
84+
}
8485
} catch (e) {
8586
if (e.code === "MODULE_NOT_FOUND") {
8687
return bs.logger.setOnce("useLevelPrefixes", true).error("Plugin {yellow:`%s`} was not found, did you forget to {cyan:npm install} it?", name);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
},
6161
"devDependencies": {
6262
"browser-sync-spa": "^1.0.2",
63+
"bs-html-injector": "^2.0.1",
6364
"bs-snippet-injector": "^2.0.1",
6465
"chai": "^2.1.0",
6566
"chalk": "^1.0.0",

test/specs/plugins/user.plugins.inline.enabled.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ describe("Plugins: Setting the default state (false) if given in options", funct
1414

1515
var config = {
1616
logLevel: "silent",
17-
plugins: [{
18-
"bs-snippet-injector": {
19-
enabled: false,
20-
file: ""
17+
plugins: [
18+
{
19+
module: "bs-snippet-injector",
20+
options: {
21+
enabled: false,
22+
file: ""
23+
}
2124
}
22-
}]
25+
]
2326
};
2427

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

4548
var config = {
4649
logLevel: "silent",
47-
plugins: [{
48-
"bs-snippet-injector": {
49-
enabled: true
50+
plugins: [
51+
{
52+
module: "bs-snippet-injector",
53+
options: {
54+
enabled: true
55+
}
5056
}
51-
}]
57+
]
5258
};
5359

5460
instance = browserSync(config, done).instance;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
3+
var browserSync = require("../../../");
4+
5+
var assert = require("chai").assert;
6+
7+
describe("Plugins: Retrieving user plugins when given inline as object", function () {
8+
9+
var instance;
10+
var PLUGIN_NAME = "Test Plugin";
11+
12+
before(function (done) {
13+
14+
browserSync.reset();
15+
16+
var config = {
17+
logLevel: "silent",
18+
plugins: [
19+
{
20+
module: {
21+
plugin: function () {
22+
done();
23+
},
24+
"plugin:name": PLUGIN_NAME
25+
},
26+
options: {
27+
files: "*.html"
28+
}
29+
}
30+
]
31+
};
32+
33+
instance = browserSync(config).instance;
34+
});
35+
after(function () {
36+
instance.cleanup();
37+
});
38+
it("Should access to only the user-specified plugins", function (done) {
39+
console.log(instance.getUserPlugins());
40+
assert.equal(instance.getUserPlugins().length, 1);
41+
done();
42+
});
43+
it("Should have access to only the user-specified plugins", function (done) {
44+
var plugin = instance.getUserPlugins()[0];
45+
assert.equal(plugin.name, PLUGIN_NAME);
46+
done();
47+
});
48+
it("should have access to user provided opts", function (done) {
49+
var plugin = instance.getUserPlugins()[0];
50+
assert.equal(plugin.opts.files, "*.html");
51+
done();
52+
});
53+
});

test/specs/plugins/user.plugins.inline.options.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ describe("Plugins: Retrieving user plugins when given inline with options", func
1818
logLevel: "silent",
1919
plugins: [
2020
{
21-
"bs-snippet-injector": {
21+
module: "bs-snippet-injector",
22+
options: {
2223
files: "*.html"
2324
}
2425
}
@@ -34,7 +35,7 @@ describe("Plugins: Retrieving user plugins when given inline with options", func
3435
assert.equal(instance.getUserPlugins().length, 1);
3536
done();
3637
});
37-
it("Should access to only the user-specified plugins", function (done) {
38+
it("Should have access to only the user-specified plugins", function (done) {
3839
var plugin = instance.getUserPlugins()[0];
3940
assert.equal(plugin.name, PLUGIN_NAME);
4041
done();

0 commit comments

Comments
 (0)