-
Notifications
You must be signed in to change notification settings - Fork 4
/
_start.js
90 lines (79 loc) · 2.43 KB
/
_start.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* eslint default-case:0 */
global.watch = true;
const path = require('path');
const fs = require('fs-extra');
const browserSync = require('browser-sync').create();
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const htmlInjector = require('bs-html-injector');
const webpackConfig = require('./webpack.config');
const bundler = webpack(webpackConfig);
// ===========================================================================
// CONFIG
// ===========================================================================
const PATHS = webpackConfig.data.PATHS;
const PROXY_TARGET = webpackConfig.data.PROXY_TARGET;
const bsOptions = {
files: [
{
// scss|js managed by webpack
match: [ 'src/**/*.!(scss|js)' ],
// manually sync everything else
fn: synchronize,
},
],
proxy: {
// proxy local WP install
target: PROXY_TARGET,
middleware: [
// converts browsersync into a webpack-dev-server
webpackDevMiddleware(bundler, {
publicPath: webpackConfig.output.publicPath,
noInfo: true,
}),
// hot update js && css
webpackHotMiddleware(bundler),
],
},
//this gets annoying
open: false,
};
// setup html injector, only compare differences within outer most div (#page)
// otherwise, it would replace the webpack HMR scripts
browserSync.use(htmlInjector, { restrictions: [ '#page' ] });
// ===========================================================================
// RUN
// ===========================================================================
// clean -> ensure 'style.css' -> run browsersync
fs.emptyDir(PATHS.build(), () => (
fs.ensureFile(PATHS.build('style.css'), () => (
browserSync.init(bsOptions)
))
)
);
// ===========================================================================
// UTILS
// ===========================================================================
/**
* Handle file events, sync updates.
*/
function synchronize(event, file) {
// copy/remove file
switch (event) {
case 'add':
fs.copy(file, getDest(file));
break;
case 'change':
fs.copy(file, getDest(file));
break;
case 'unlink':
fs.remove(getDest(file));
break;
case 'unlinkDir':
fs.remove(getDest(file));
break;
}
// activate html injector
if (file.endsWith('php')) {htmlInjector();}
}