-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowserSync.js
106 lines (87 loc) · 2.21 KB
/
browserSync.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
/**
* Conditionally set up BrowserSync.
*
* Only run BrowserSync if config.browserSync.live = true.
*/
/**
* External dependencies
*/
import browserSync from 'browser-sync';
/**
* Internal dependencies
*/
import {
paths,
checkExistence,
} from './constants';
import {
sslFound,
httpsEnabled,
} from './helper/log';
import { getMainConfig } from './utils';
/** Creates a BrowserSync instance. */
export const server = browserSync.create();
/** The user configuration. */
const config = getMainConfig();
/**
* Gets SSL certificate or key file path.
*
* @param {string} type The data to get.
* @return {string} The file path.
*/
const getSSLFile = ( type ) => config?.dev?.browserSync[ type ] ?? paths.browserSync.cert;
/**
* Initializes the BrowserSync server conditionally.
*
* @param {function} done Function to call when async processes finish.
*/
export const serve = ( done ) => {
// Bail early if not serving via BrowserSync.
if ( ! config.dev.browserSync.live ) {
done();
}
const serverConfig = {
proxy: config.dev.browserSync.proxyURL,
port: config.dev.browserSync.bypassPort,
liveReload: true,
https: false,
};
// Only setup HTTPS certificates if HTTPS is enabled.
if ( config.dev.browserSync.https ) {
// Use a custom path key/cert if defined, otherwise use the default path.
const cert = getSSLFile( 'certPath' );
const key = getSSLFile( 'keyPath' );
// Ensure the key/cert files exist.
const certFound = checkExistence( cert );
const keyFound = checkExistence( key );
// Let the user know if we found a certificate.
sslFound( certFound, 'certificate', cert );
// Let the user know if we found a key.
sslFound( keyFound, 'key', key );
// Only enable HTTPS if there is a cert and a key.
if ( certFound && keyFound ) {
httpsEnabled();
serverConfig.https = { key, cert };
}
}
// Start the BrowserSync server.
server.init( serverConfig );
done();
};
/**
* Reloads the live site.
*
* @param {function} done Function to call when async processes finish.
*/
export const reload = ( done ) => {
if ( config?.dev?.browserSync?.live ) {
if ( server.paused ) {
server.resume();
}
server.reload();
} else {
server.pause();
}
done();
};