-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
66 lines (54 loc) · 2.11 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
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
var glob = require('glob');
var bs = require('browser-sync');
var path = require('path');
var CachingWriter = require('broccoli-caching-writer');
function BrowserSyncWatcher(inputTrees, options) {
if (!(this instanceof BrowserSyncWatcher)) {
return new BrowserSyncWatcher(inputTrees, options);
}
// the options should override that in the CachingWrite if passed through option
this.onlyReturnUpdatedTrees = true;
CachingWriter.call(this, inputTrees, options);
options = options || {};
this.options = options;
this.port = (options.port > 0 ? options.port : 4200 );
this.bsInstance = bs.create();
var bsOptions = options.browserSync || {};
bsOptions.proxy = 'http://localhost:' + this.port;
this.bsInstance.init(bsOptions);
this.reload = function (files) {
this.bsInstance.reload(files);
};
}
BrowserSyncWatcher.prototype = Object.create(CachingWriter.prototype);
BrowserSyncWatcher.prototype.constructor = BrowserSyncWatcher;
// browser-sync can only inject css files at the moment
BrowserSyncWatcher.prototype.extensions = ['css'];
// TODO(markus): write custom caching functionality that either returns
// all the files that have been updated, or better: It returns all updated
// files with given extensions if those are the only files that have been updated.
// otherwise it will return an empty array.
BrowserSyncWatcher.prototype.build = function() {
var files = [];
for (var i = 0; i < this.inputPaths.length; i++) {
var tmpFiles = glob.sync(path.join(this.inputPaths[i], '/**/*'), { nodir: true });
for (var j = 0; j < tmpFiles.length; j++) {
var fileEnding = '.' + this.extensions[0];
// if a filename does not end with the specified fileEnding then reload
// the whole page and exit.
if (tmpFiles[j].indexOf(fileEnding, this.length - fileEnding.length) === -1) {
this.reload();
return;
}
}
files = files.concat(tmpFiles);
}
// if we have any css file then inject these.
if (files.length > 0) {
console.log('injecting css', files);
this.reload(files);
}
return;
};
module.exports = BrowserSyncWatcher;