summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorValentin Popov <info@valentineus.link>2019-08-20 11:38:39 +0300
committerValentin Popov <info@valentineus.link>2019-08-20 11:38:39 +0300
commit419f48c18d84ba3516124571ad8134bf304e65ab (patch)
treeae09d4cd491815061d84c61ea6b62bf89b098ff4 /src
parent809a5efb9db4284b0124c75fbaec34d3dd22e7bc (diff)
downloadelectron-hot-reload-419f48c18d84ba3516124571ad8134bf304e65ab.tar.xz
electron-hot-reload-419f48c18d84ba3516124571ad8134bf304e65ab.zip
Added Renderer reloader
Signed-off-by: Valentin Popov <info@valentineus.link>
Diffstat (limited to 'src')
-rw-r--r--src/rendererReloader.ts47
1 files changed, 45 insertions, 2 deletions
diff --git a/src/rendererReloader.ts b/src/rendererReloader.ts
index 63178cf..36f485d 100644
--- a/src/rendererReloader.ts
+++ b/src/rendererReloader.ts
@@ -1,3 +1,46 @@
-type TRendererReloader = (paths: string[], ignored?: string[], handler?: (path: string) => void) => void;
+import chokidar from "chokidar";
+// tslint:disable-next-line: no-implicit-dependencies
+import { BrowserWindow } from "electron";
-export const rendererReloader: TRendererReloader = (paths, ignored, handler) => { };
+type TRendererReloader = (
+ paths: string | string[],
+ ignored?: RegExp | RegExp[],
+ handler?: (error: Error | undefined, path: string | undefined) => void,
+ options?: chokidar.WatchOptions,
+) => void;
+
+const ignoredDefault: RegExp = /(node_modules|bower_components)/;
+
+export const rendererReloader: TRendererReloader = (
+ paths: string | string[],
+ ignored?: RegExp | RegExp[],
+ handler?: (error: Error | undefined, path: string | undefined) => void,
+ options?: chokidar.WatchOptions,
+): void => {
+ const ignoredPaths: RegExp[] = [ignoredDefault];
+
+ if (typeof ignored === "object") {
+ ignoredPaths.concat(ignored);
+ }
+
+ const watcher: chokidar.FSWatcher = chokidar.watch(paths, {
+ ignored: ignoredPaths,
+ ...options,
+ });
+
+ watcher.on("error", (error: Error) => {
+ if (typeof handler === "function") {
+ handler(error, undefined);
+ }
+ });
+
+ watcher.on("change", (path: string) => {
+ if (typeof handler === "function") {
+ handler(undefined, path);
+ }
+
+ BrowserWindow.getAllWindows().forEach((window: BrowserWindow) => {
+ window.webContents.reloadIgnoringCache();
+ });
+ });
+};