-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
53 lines (45 loc) · 1.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var _ = require('lodash');
var bs = require('browser-sync').create();
var debug = require('debug')('metalsmith-browser-sync');
var PLUGIN_NAME = 'browser-sync';
function browserSyncPlugin(options, callback){
function plugin(files, metalsmith, done) {
// first time through remove ourselves, since we will be continously building
// and we only need one browser-sync server active
metalsmith.plugins.forEach(function(plugin, index){
if(plugin._pluginName === PLUGIN_NAME){
metalsmith.plugins.splice(index, 1);
}
});
//default options
var bsOptions = {
server : "build",
files : ["src/**/*.md", "templates/**/*.hbs"]
};
_.merge(bsOptions, options, function merger(a, b){
//Always use the array given if there is one
if(_.isArray(a)){
return b;
}
});
function rebuild() {
if (!bs.paused) {
bs.pause();
metalsmith.build(function(err) {
var buildMessage = err ? err : 'Build successful';
debug(buildMessage);
bs.resume();
bs.reload();
});
}
}
var watched = bsOptions.files;
delete bsOptions.files;
bs.watch(watched, { ignoreInitial: true }).on('all', rebuild);
bs.init(bsOptions, callback || _.noop);
done();
}
plugin._pluginName = PLUGIN_NAME;
return plugin;
}
module.exports = browserSyncPlugin;