Issues (76)

bs-server.js (1 issue)

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 {
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