generated from flyntwp/flynt
-
Notifications
You must be signed in to change notification settings - Fork 8
/
bs-server.js
61 lines (55 loc) · 1.8 KB
/
bs-server.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
/*
|--------------------------------------------------------------------------
| Browser-sync config file
|--------------------------------------------------------------------------
|
| For up-to-date information about the options:
| http://www.browsersync.io/docs/options/
|
| There are more options than you see here, these are just the ones that are
| set internally. See the website for more info.
|
|
*/
const config = require('./build-config')
const webpack = require('webpack')
const webpackDevMiddleware = require('webpack-dev-middleware')
/**
* Require ./webpack.config.js and make a bundler from it
*/
const webpackConfig = require('./webpack.config')
const bundler = webpack(webpackConfig)
const browserSync = require('browser-sync').create()
const crypto = require('crypto')
const fileHashes = []
bundler.plugin('done', function (bundles) {
bundles.stats.forEach(function (stats, i) {
fileHashes[i] = fileHashes[i] || {}
checkAssets(stats, fileHashes[i])
})
})
function checkAssets (stats, bundleHashes) {
try {
const changedFiles = Object.keys(stats.compilation.assets)
.filter(name => {
const asset = stats.compilation.assets[name]
const md5Hash = crypto.createHash('md5')
const hash = md5Hash.update(asset.children ? asset.children[0]._value : asset.source()).digest('hex')
if (bundleHashes[name] !== hash) {
bundleHashes[name] = hash
return true
} else {
return false
}
})
browserSync.reload(changedFiles.map(name => `dist/${name}`))
} catch (e) {}
}
browserSync.init(Object.assign({
middleware: [
webpackDevMiddleware(bundler, Object.assign({
publicPath: webpackConfig[0].output.publicPath,
logLevel: 'silent'
}, config.webpackDevMiddleware))
]
}, config.browserSync))