Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 68 additions & 37 deletions src/live-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,6 @@ import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/timeout';

export function enableLiveReload(options={}) {
let { strategy } = options;

if (process.type !== 'browser' || !global.globalCompilerHost) throw new Error("Call this from the browser process, right after initializing electron-compile");

switch(strategy) {
case 'react-hmr':
enableReactHMR();
break;
case 'naive':
default:
enableLiveReloadNaive();
}
}

let BrowserWindow;
if (process.type === 'browser') {
BrowserWindow = require('electron').BrowserWindow;
Expand All @@ -47,38 +32,84 @@ function reloadAllWindows() {
return Promise.all(ret);
}

function enableLiveReloadNaive() {
let filesWeCareAbout = global.globalCompilerHost.listenToCompileEvents()
.filter(x => !FileChangedCache.isInNodeModules(x.filePath));

let weShouldReload = filesWeCareAbout
.mergeMap(x => watchPath(x.filePath).map(() => x))
.guaranteedThrottle(1*1000);
function triggerHMRInRenderers() {
BrowserWindow.getAllWindows().forEach((window) => {
window.webContents.send('__electron-compile__HMR');
});

return weShouldReload
.switchMap(() => Observable.defer(() => Observable.fromPromise(reloadAllWindows()).timeout(5*1000).catch(() => Observable.empty())))
.subscribe(() => console.log("Reloaded all windows!"));
return Promise.resolve(true);
}

function triggerHMRInRenderers() {
function triggerAssetReloadInRenderers(filePath) {
BrowserWindow.getAllWindows().forEach((window) => {
window.webContents.send('__electron-compile__HMR');
window.webContents.send('__electron-compile__stylesheet_reload', filePath);
});

return Promise.resolve(true);
}

function enableReactHMR() {
global.__electron_compile_hmr_enabled__ = 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'
}
}

let filesWeCareAbout = global.globalCompilerHost.listenToCompileEvents()
.filter(x => !FileChangedCache.isInNodeModules(x.filePath));
function setupWatchHMR(filePath) {
watchPath(filePath).subscribe(() => triggerHMRInRenderers())
}

let weShouldReload = filesWeCareAbout
.mergeMap(x => watchPath(x.filePath).map(() => x))
.guaranteedThrottle(1*1000);
function setWatchHotAssets(filePath) {
watchPath(filePath).subscribe(() => triggerAssetReloadInRenderers(filePath))
}

return weShouldReload
.switchMap(() => Observable.defer(() => Observable.fromPromise(triggerHMRInRenderers()).catch(() => Observable.empty())))
.subscribe(() => console.log("HMR sent to all windows!"));
function setupWatchNaive(filePath) {
watchPath(filePath).subscribe(() => reloadAllWindows())
}

export function enableLiveReload(options=defaultOptions) {
let { strategy } = options;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotta handle the old syntax here (i.e. { strategy: 'naive' }) or else it is a breaking change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Done in the latest commit.


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)
}
});
}
15 changes: 15 additions & 0 deletions src/require-hook.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mimeTypes from '@paulcbetts/mime-types';

let HMR = false;
let stylesheetReload = false;

const d = require('debug')('electron-compile:require-hook');
let electron = null;
Expand All @@ -9,6 +10,7 @@ if (process.type === 'renderer') {
window.__hot = [];
electron = require('electron');
HMR = electron.remote.getGlobal('__electron_compile_hmr_enabled__');
stylesheetReload = electron.remote.getGlobal('__electron_compile_stylesheet_reload_enabled__');

if (HMR) {
electron.ipcRenderer.on('__electron-compile__HMR', () => {
Expand All @@ -25,6 +27,19 @@ if (process.type === 'renderer') {
window.__hot.forEach(fn => fn());
});
}

if (stylesheetReload) {
electron.ipcRenderer.on('__electron-compile__stylesheet_reload', (e, path) => {
let links = document.getElementsByTagName('link');

for (let link of links) {
let uri = link.href
if (uri.includes(path)) {
link.href = link.href; // trigger a reload for this stylesheet
}
}
});
}
}

/**
Expand Down