Completed
Push — master ( b77a16...cb9446 )
by Doğa
16s queued 11s
created

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 (stats) {
30
  try {
31
    const changedFiles = Object.keys(stats.compilation.assets)
32
      .filter(name => {
33
        const asset = stats.compilation.assets[name]
34
        const md5Hash = crypto.createHash('md5')
35
        const hash = md5Hash.update(asset.children ? asset.children[0]._value : asset.source()).digest('hex')
36
        if (fileHashes[name] !== hash) {
37
          fileHashes[name] = hash
38
          return true
39
        } else {
40
          return false
41
        }
42
      })
43
    browserSync.reload(changedFiles.map(name => `dist/${name}`))
44
  } 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...
45
})
46
47
browserSync.init(Object.assign({
48
  middleware: [
49
    webpackDevMiddleware(bundler, Object.assign({
50
      publicPath: webpackConfig[0].output.publicPath,
51
      logLevel: 'silent'
52
    }, config.webpackDevMiddleware))
53
  ]
54
}, config.browserSync))
55