1
|
|
|
'use strict'; |
2
|
|
|
|
3
|
|
|
const webpack = require('webpack'); |
4
|
|
|
const webpackDevMiddleware = require('webpack-dev-middleware'); |
5
|
|
|
const webpackHotMiddleware = require('webpack-hot-middleware'); |
6
|
|
|
const htmlInjector = require('bs-html-injector'); |
7
|
|
|
const webpackConfig = require('../../config/webpack.config'); |
8
|
|
|
const bundler = webpack(webpackConfig); |
9
|
|
|
|
10
|
|
|
module.exports = function (gulp, paths, plugins, options) { |
11
|
|
|
return function () { |
12
|
|
|
// setup html injector, only compare differences within outer most div (#page) |
13
|
|
|
// otherwise, it will replace the webpack HMR scripts |
14
|
|
|
plugins.browserSync.use(htmlInjector, {restrictions: ['#content']}); |
15
|
|
|
|
16
|
|
|
return plugins.browserSync.init({ |
17
|
|
|
files: [{ |
18
|
|
|
// js managed by webpack |
19
|
|
|
// optionally exclude other managed assets: images, fonts, etc |
20
|
|
|
match: [paths.SRC_PATH + '**/*.!(js)'], |
21
|
|
|
|
22
|
|
|
// manually sync everything else |
23
|
|
|
fn: synchronize, |
24
|
|
|
}], |
25
|
|
|
//browsersync with a php server |
26
|
|
|
proxy: { |
27
|
|
|
target: options.url, |
28
|
|
|
middleware: [ |
29
|
|
|
|
30
|
|
|
// converts browsersync into a webpack-dev-server |
31
|
|
|
webpackDevMiddleware(bundler, { |
32
|
|
|
publicPath: webpackConfig.output.publicPath, |
33
|
|
|
noInfo: true, |
34
|
|
|
hot: true, |
35
|
|
|
watchOptions: {aggregateTimeout: 10}, |
36
|
|
|
stats: {colors: true} |
37
|
|
|
}), |
38
|
|
|
|
39
|
|
|
// hot update js && css |
40
|
|
|
webpackHotMiddleware(bundler), |
41
|
|
|
], |
42
|
|
|
}, |
43
|
|
|
notify: options.notifications |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
}; |
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
function synchronize(event, file) { |
50
|
|
|
// copy/remove file |
51
|
|
|
// if you keep assets in your src/sass folder, that might need flattened, depending on your build |
52
|
|
|
|
53
|
|
|
// activate html injector |
54
|
|
|
if (file.endsWith('php')) { |
55
|
|
|
htmlInjector(); |
56
|
|
|
} |
57
|
|
|
} |