From b6b1dabde0f63ca0ae743aa7f4266ca892d7b5e5 Mon Sep 17 00:00:00 2001
From: Jeremy Press <jeremypress1@gmail.com>
Date: Mon, 28 Aug 2023 00:39:42 +0000
Subject: [PATCH] feat: support configurable ws port and remote development (#429)

---
 quartz/plugins/emitters/componentResources.ts |    8 +++++++-
 .gitignore                                    |    2 ++
 quartz/util/ctx.ts                            |    2 ++
 quartz/cli/handlers.js                        |    2 +-
 quartz/cli/args.js                            |   10 ++++++++++
 5 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
index fd96fec..25d07db 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,5 @@
 .obsidian
 .quartz-cache
 private/
+.replit
+replit.nix
diff --git a/quartz/cli/args.js b/quartz/cli/args.js
index 4f330cd..3543e2e 100644
--- a/quartz/cli/args.js
+++ b/quartz/cli/args.js
@@ -76,6 +76,16 @@
     default: 8080,
     describe: "port to serve Quartz on",
   },
+  wsPort: {
+    number: true,
+    default: 3001,
+    describe: "port to use for WebSocket-based hot-reload notifications",
+  },
+  remoteDevHost: {
+    string: true,
+    default: "",
+    describe: "A URL override for the websocket connection if you are not developing on localhost",
+  },
   bundleInfo: {
     boolean: true,
     default: false,
diff --git a/quartz/cli/handlers.js b/quartz/cli/handlers.js
index cba0ceb..bc3da73 100644
--- a/quartz/cli/handlers.js
+++ b/quartz/cli/handlers.js
@@ -402,7 +402,7 @@
       return serve()
     })
     server.listen(argv.port)
-    const wss = new WebSocketServer({ port: 3001 })
+    const wss = new WebSocketServer({ port: argv.wsPort })
     wss.on("connection", (ws) => connections.push(ws))
     console.log(
       chalk.cyan(
diff --git a/quartz/plugins/emitters/componentResources.ts b/quartz/plugins/emitters/componentResources.ts
index a62bc38..61409cc 100644
--- a/quartz/plugins/emitters/componentResources.ts
+++ b/quartz/plugins/emitters/componentResources.ts
@@ -107,12 +107,18 @@
         document.dispatchEvent(event)`)
   }
 
+  let wsUrl = `ws://localhost:${ctx.argv.wsPort}`
+
+  if (ctx.argv.remoteDevHost) {
+    wsUrl = `wss://${ctx.argv.remoteDevHost}:${ctx.argv.wsPort}`
+  }
+
   if (reloadScript) {
     staticResources.js.push({
       loadTime: "afterDOMReady",
       contentType: "inline",
       script: `
-          const socket = new WebSocket('ws://localhost:3001')
+          const socket = new WebSocket('${wsUrl}'')
           socket.addEventListener('message', () => document.location.reload())
         `,
     })
diff --git a/quartz/util/ctx.ts b/quartz/util/ctx.ts
index d303391..13e0bf8 100644
--- a/quartz/util/ctx.ts
+++ b/quartz/util/ctx.ts
@@ -7,6 +7,8 @@
   output: string
   serve: boolean
   port: number
+  wsPort: number
+  remoteDevHost?: string
   concurrency?: number
 }
 

--
Gitblit v1.10.0