Skip to content

Commit 0d2988a

Browse files
committed
latest PR code refactored; support for node 3 and below dropped
1 parent ec64500 commit 0d2988a

File tree

1 file changed

+47
-56
lines changed

1 file changed

+47
-56
lines changed

index.js

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,67 @@
1-
var _ = require('lodash');
2-
var browserSync = require('browser-sync');
1+
const { extend, get, isFunction } = require('lodash')
2+
const browserSync = require('browser-sync')
33

44
function BrowserSyncPlugin(browserSyncOptions, pluginOptions) {
5-
var self = this;
6-
7-
var defaultPluginOptions = {
5+
const defaultPluginOptions = {
86
reload: true,
97
name: 'bs-webpack-plugin',
10-
callback: undefined
11-
};
8+
callback: undefined,
9+
injectCss: false
10+
}
1211

13-
self.browserSyncOptions = _.extend({}, browserSyncOptions);
14-
self.options = _.extend({}, defaultPluginOptions, pluginOptions);
12+
this.browserSyncOptions = extend({}, browserSyncOptions)
13+
this.options = extend({}, defaultPluginOptions, pluginOptions)
1514

16-
self.browserSync = browserSync.create(self.options.name);
17-
self.isWebpackWatching = false;
18-
self.isBrowserSyncRunning = false;
15+
this.browserSync = browserSync.create(this.options.name)
16+
this.isWebpackWatching = false
17+
this.isBrowserSyncRunning = false
1918
}
2019

21-
BrowserSyncPlugin.prototype.apply = function (compiler) {
22-
var self = this;
23-
24-
compiler.plugin('watch-run', function (watching, callback) {
25-
self.isWebpackWatching = true;
26-
callback(null, null);
27-
});
20+
function isCssOnlyEmission(stats) {
21+
const assetsStatsMapping = get(stats, 'compilation.assets', {})
22+
const assetsNames = Object.keys(assetsStatsMapping)
2823

29-
compiler.plugin('compilation', function () {
30-
if (self.isBrowserSyncRunning && self.browserSyncOptions.notify) {
31-
self.browserSync.notify('Rebuilding...');
32-
}
33-
});
24+
return (
25+
assetsNames
26+
.map(assetName => ({ name: assetName, wasEmitted: get(assetsStatsMapping, [assetName, 'emitted'], false) }))
27+
.filter(asset => asset.wasEmitted)
28+
.every(asset => asset.name.includes('.css'))
29+
)
30+
}
3431

35-
compiler.plugin('done', function (stats) {
32+
BrowserSyncPlugin.prototype.apply = function (compiler) {
33+
compiler.plugin('watch-run', (watching, callback) => {
34+
this.isWebpackWatching = true
35+
callback(null, null)
36+
})
3637

37-
// assets contains all the compiled assets
38-
var assets = _.keys(stats.compilation.assets),
39-
isCSS = _(assets)
40-
// organize the assets for cleaner use
41-
.map(function(asset){
42-
return {
43-
name: asset,
44-
emitted: stats.compilation.assets[asset].emitted
45-
}
46-
})
47-
// remove asset files that have not been emitted
48-
.filter(function(asset){ return asset.emitted })
49-
// true if all assets contain .css, false for anything else (.js, .img, etc)
50-
.every(function(asset){
51-
return asset.name.match('.css') !== null;
52-
});
38+
compiler.plugin('compilation', () => {
39+
if (this.isBrowserSyncRunning && this.browserSyncOptions.notify) {
40+
this.browserSync.notify('Rebuilding...')
41+
}
42+
})
5343

54-
if (self.isWebpackWatching) {
55-
if (self.isBrowserSyncRunning) {
56-
if (self.options.reload) {
57-
if (isCSS)
58-
// inject css if all compiled assets are css
59-
self.browserSync.reload('*.css');
60-
else
61-
self.browserSync.reload();
44+
compiler.plugin('done', stats => {
45+
if (this.isWebpackWatching) {
46+
if (this.isBrowserSyncRunning) {
47+
if (this.options.reload) {
48+
if (this.options.injectCss && isCssOnlyEmission(stats)) {
49+
this.browserSync.reload('*.css')
50+
} else {
51+
this.browserSync.reload()
52+
}
6253
}
6354
} else {
64-
if (_.isFunction(self.options.callback)) {
65-
self.browserSync.init(self.browserSyncOptions, self.options.callback);
55+
if (isFunction(this.options.callback)) {
56+
this.browserSync.init(this.browserSyncOptions, this.options.callback)
6657
} else {
67-
self.browserSync.init(self.browserSyncOptions);
58+
this.browserSync.init(this.browserSyncOptions)
6859
}
6960

70-
self.isBrowserSyncRunning = true;
61+
this.isBrowserSyncRunning = true
7162
}
7263
}
73-
});
74-
};
64+
})
65+
}
7566

76-
module.exports = BrowserSyncPlugin;
67+
module.exports = BrowserSyncPlugin

0 commit comments

Comments
 (0)