bs-server.js   A
last analyzed

Complexity

Total Complexity 8
Complexity/F 1.6

Size

Lines of Code 48
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 33
c 0
b 0
f 0
dl 0
loc 48
rs 10
mnd 3
bc 3
fnc 5
bpm 0.6
cpm 1.6
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
B ➔ checkAssets 0 17 8
1
/*
2
 |--------------------------------------------------------------------------
3
 | Browser-sync config file
4
 |--------------------------------------------------------------------------
5
 |
6
 | For up-to-date information about the options:
7
 |   http://www.browsersync.io/docs/options/
8
 |
9
 | There are more options than you see here, these are just the ones that are
10
 | set internally. See the website for more info.
11
 |
12
 |
13
 */
14
const config = require('./build-config')
15
16
const webpack = require('webpack')
17
const webpackDevMiddleware = require('webpack-dev-middleware')
18
19
/**
20
 * Require ./webpack.config.js and make a bundler from it
21
 */
22
const webpackConfig = require('./webpack.config')
23
const bundler = webpack(webpackConfig)
24
25
const browserSync = require('browser-sync').create()
26
27
const crypto = require('crypto')
28
const fileHashes = []
29
bundler.plugin('done', function (bundles) {
30
  bundles.stats.forEach(function (stats, i) {
31
    fileHashes[i] = fileHashes[i] || {}
32
    checkAssets(stats, fileHashes[i])
33
  })
34
})
35
36
function checkAssets (stats, bundleHashes) {
37
  try {
38
    const changedFiles = Object.keys(stats.compilation.assets)
39
      .filter(name => {
40
        const asset = stats.compilation.assets[name]
41
        const md5Hash = crypto.createHash('md5')
42
        const hash = md5Hash.update(asset.children ? asset.children[0]._value : asset.source()).digest('hex')
43
        if (bundleHashes[name] !== hash) {
44
          bundleHashes[name] = hash
45
          return true
46
        } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
47
          return false
48
        }
49
      })
50
    browserSync.reload(changedFiles.map(name => `dist/${name}`))
51
  } catch (e) {}
0 ignored issues
show
Coding Style Comprehensibility Best Practice introduced by
Empty catch clauses should be used with caution; consider adding a comment why this is needed.
Loading history...
52
}
53
54
browserSync.init(Object.assign({
55
  middleware: [
56
    webpackDevMiddleware(bundler, Object.assign({
57
      publicPath: webpackConfig[0].output.publicPath,
58
      logLevel: 'silent'
59
    }, config.webpackDevMiddleware))
60
  ]
61
}, config.browserSync))
62