aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorValentin Popov <info@valentineus.link>2019-08-22 00:12:17 +0300
committerGitHub <noreply@github.com>2019-08-22 00:12:17 +0300
commit6d5cb2611be73e4c3b0787ac00fe28e4c243417e (patch)
tree5b65999c40a916d0e4bb5f878b30cee236045e94 /src
parent03dd1ac6af558886acae1296832459827ade54c3 (diff)
parent8147b911dec725f50fb79e22dd149e13eca02d81 (diff)
downloadelectron-hot-reload-6d5cb2611be73e4c3b0787ac00fe28e4c243417e.tar.xz
electron-hot-reload-6d5cb2611be73e4c3b0787ac00fe28e4c243417e.zip
Merge pull request #1 from valentineus/develop
Develop
Diffstat (limited to 'src')
-rw-r--r--src/index.ts7
-rw-r--r--src/mainReloader.ts45
-rw-r--r--src/rendererReloader.ts48
3 files changed, 100 insertions, 0 deletions
diff --git a/src/index.ts b/src/index.ts
new file mode 100644
index 0000000..82df114
--- /dev/null
+++ b/src/index.ts
@@ -0,0 +1,7 @@
+import { mainReloader } from "./mainReloader";
+import { rendererReloader } from "./rendererReloader";
+
+export {
+ mainReloader,
+ rendererReloader,
+};
diff --git a/src/mainReloader.ts b/src/mainReloader.ts
new file mode 100644
index 0000000..5a9a71b
--- /dev/null
+++ b/src/mainReloader.ts
@@ -0,0 +1,45 @@
+import chokidar from "chokidar";
+// tslint:disable-next-line: no-implicit-dependencies
+import { app } from "electron";
+
+type TMainReloader = (
+ 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 mainReloader: TMainReloader = (
+ 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);
+ }
+
+ app.relaunch();
+ app.exit();
+ });
+};
diff --git a/src/rendererReloader.ts b/src/rendererReloader.ts
new file mode 100644
index 0000000..213dad0
--- /dev/null
+++ b/src/rendererReloader.ts
@@ -0,0 +1,48 @@
+import chokidar from "chokidar";
+// tslint:disable-next-line: no-implicit-dependencies
+import { BrowserWindow } from "electron";
+
+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);
+ }
+
+ const windows: BrowserWindow[] = BrowserWindow.getAllWindows();
+
+ windows.forEach((window: BrowserWindow) => {
+ window.webContents.reloadIgnoringCache();
+ });
+ });
+};