This repository was archived by the owner on Oct 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathlive-reload.js
More file actions
115 lines (95 loc) · 3.14 KB
/
live-reload.js
File metadata and controls
115 lines (95 loc) · 3.14 KB
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
107
108
109
110
111
112
113
114
115
import FileChangedCache from './file-change-cache';
import {watchPath} from './pathwatcher-rx';
import {Observable} from 'rxjs/Observable';
import './custom-operators';
import 'rxjs/add/observable/defer';
import 'rxjs/add/observable/empty';
import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/timeout';
let BrowserWindow;
if (process.type === 'browser') {
BrowserWindow = require('electron').BrowserWindow;
}
function reloadAllWindows() {
let ret = BrowserWindow.getAllWindows().map(wnd => {
if (!wnd.isVisible()) return Promise.resolve(true);
return new Promise((res) => {
wnd.webContents.reloadIgnoringCache();
wnd.once('ready-to-show', () => res(true));
});
});
return Promise.all(ret);
}
function triggerHMRInRenderers() {
BrowserWindow.getAllWindows().forEach((window) => {
window.webContents.send('__electron-compile__HMR');
});
return Promise.resolve(true);
}
function triggerAssetReloadInRenderers(filePath) {
BrowserWindow.getAllWindows().forEach((window) => {
window.webContents.send('__electron-compile__stylesheet_reload', filePath);
});
return Promise.resolve(true);
}
const defaultOptions = {
'strategy': {
'text/html': 'naive',
'text/tsx': 'react-hmr',
'text/jsx': 'react-hmr',
'application/javascript': 'react-hmr',
'text/stylus': 'hot-stylesheets',
'text/sass': 'hot-stylesheets',
'text/scss': 'hot-stylesheets',
'text/css' : 'hot-stylesheets'
}
}
function setupWatchHMR(filePath) {
watchPath(filePath).subscribe(() => triggerHMRInRenderers())
}
function setWatchHotAssets(filePath) {
watchPath(filePath).subscribe(() => triggerAssetReloadInRenderers(filePath))
}
function setupWatchNaive(filePath) {
watchPath(filePath).subscribe(() => reloadAllWindows())
}
export function enableLiveReload(options=defaultOptions) {
let { strategy } = options;
if (process.type !== 'browser' || !global.globalCompilerHost) throw new Error("Call this from the browser process, right after initializing electron-compile");
// Just to handle the old case
let oldsyntax = false
if (typeof strategy === 'string') {
oldsyntax = true
}
// Enable the methods described in the reload strategy
for (let mime of Object.keys(strategy)) {
switch(oldsyntax ? strategy : strategy[mime]) {
case 'react-hmr':
global.__electron_compile_hmr_enabled__ = true;
break;
case 'hot-stylesheets':
global.__electron_compile_stylesheet_reload_enabled__ = true;
break;
}
}
// Find all the files compiled by electron-compile and setup watchers
let filesWeCareAbout = global.globalCompilerHost.listenToCompileEvents()
.filter(x => !FileChangedCache.isInNodeModules(x.filePath))
.subscribe(x => {
switch(oldsyntax ? strategy : strategy[x.mimeType]) {
case 'react-hmr':
setupWatchHMR(x.filePath)
break;
case 'hot-stylesheets':
setWatchHotAssets(x.filePath)
break;
case 'naive':
default:
setupWatchNaive(x.filePath)
}
});
}