Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.

Commit 6a8960d

Browse files
committed
support reload on change workflow again
1 parent 408dec4 commit 6a8960d

File tree

7 files changed

+78
-45
lines changed

7 files changed

+78
-45
lines changed

src/main.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,52 @@
11
import type { RequestHandler, ServerBuild } from "@remix-run/server-runtime"
22
import { createRequestHandler } from "@remix-run/server-runtime"
33
import { app, protocol } from "electron"
4+
import { asAbsolutePath } from "./as-absolute-path"
45
import type { AssetFile } from "./asset-files"
56
import { collectAssetFiles, serveAsset } from "./asset-files"
67
import "./browser-globals"
78

8-
const defaultMode = app.isPackaged
9-
? "production"
10-
: process.env.NODE_ENV === "production"
11-
? "production"
12-
: "development"
9+
const defaultMode = app.isPackaged ? "production" : process.env.NODE_ENV
1310

1411
export type GetLoadContextFunction = (
1512
request: Electron.ProtocolRequest,
1613
) => unknown
1714

1815
export type InitRemixOptions = {
19-
serverBuild: ServerBuild
20-
mode?: "development" | "production"
16+
/**
17+
* The path to the server build, or the server build itself.
18+
*/
19+
serverBuild: ServerBuild | string
20+
21+
/**
22+
* The mode to run the app in, either development or production
23+
* @default app.isPackaged ? "production" : process.env.NODE_ENV
24+
*/
25+
mode?: string
26+
27+
/**
28+
* The path where static assets are served from.
29+
* @default "public"
30+
*/
2131
publicFolder?: string
32+
33+
/**
34+
* A function to provide a `context` object to your loaders.
35+
*/
2236
getLoadContext?: GetLoadContextFunction
2337
}
2438

2539
export async function initRemix({
26-
serverBuild,
40+
serverBuild: serverBuildOption,
2741
mode = defaultMode,
2842
publicFolder = "public",
2943
getLoadContext,
3044
}: InitRemixOptions) {
45+
let serverBuild: ServerBuild =
46+
typeof serverBuildOption === "string"
47+
? require(serverBuildOption)
48+
: serverBuildOption
49+
3150
let [assetFiles] = await Promise.all([
3251
collectAssetFiles(publicFolder),
3352
app.whenReady(),
@@ -39,6 +58,11 @@ export async function initRemix({
3958
assetFiles = await collectAssetFiles(publicFolder)
4059
}
4160

61+
if (mode === "development" && typeof serverBuildOption === "string") {
62+
purgeRequireCache(asAbsolutePath(serverBuildOption))
63+
serverBuild = require(serverBuildOption)
64+
}
65+
4266
const context = await getLoadContext?.(request)
4367
const requestHandler = createRequestHandler(serverBuild, {}, mode)
4468

@@ -103,3 +127,11 @@ async function serveRemixResponse(
103127
statusCode: response.status,
104128
}
105129
}
130+
131+
function purgeRequireCache(prefix: string) {
132+
for (const key in require.cache) {
133+
if (key.startsWith(prefix)) {
134+
delete require.cache[key]
135+
}
136+
}
137+
}

template/desktop/main.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const { initRemix } = require("remix-electron")
2+
const { app, BrowserWindow, dialog } = require("electron")
3+
const { join } = require("node:path")
4+
5+
/** @type {BrowserWindow | undefined} */
6+
let win
7+
8+
/** @param {string} url */
9+
async function createWindow(url) {
10+
win = new BrowserWindow({ show: false })
11+
await win.loadURL(url)
12+
win.show()
13+
}
14+
15+
app.on("ready", async () => {
16+
try {
17+
const url = await initRemix({ serverBuild: join(__dirname, "build") })
18+
await createWindow(url)
19+
} catch (error) {
20+
dialog.showErrorBox("Error", getErrorStack(error))
21+
console.error(error)
22+
}
23+
})
24+
25+
/** @param {unknown} error */
26+
function getErrorStack(error) {
27+
return error instanceof Error ? error.stack || error.message : String(error)
28+
}

template/desktop/main.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

template/nodemon.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://json.schemastore.org/nodemon.json",
33
"exec": "electron",
4-
"watch": ["desktop/build"],
5-
"delay": 0.5
4+
"watch": ["desktop"],
5+
"ignore": ["desktop/build"]
66
}

template/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
"description": "",
44
"version": "0.0.0",
55
"private": true,
6-
"main": "desktop/build/index.js",
6+
"main": "desktop/main.js",
77
"scripts": {
88
"prepare": "remix setup node",
99
"clean": "del-cli dist desktop/build public/build .cache",
1010
"dev": "npm run clean && cross-env NODE_ENV=development npm-run-all --parallel --print-label --race dev:*",
1111
"dev:remix": "remix watch",
12-
"dev:nodemon": "wait-on file:desktop/build/index.js && nodemon .",
12+
"dev:nodemon": "wait-on file:desktop/main.js && nodemon .",
1313
"build": "npm run clean && remix build && electron-builder",
1414
"start": "cross-env NODE_ENV=production electron ."
1515
},

template/remix.config.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ module.exports = {
55
appDirectory: "app",
66
assetsBuildDirectory: "public/build",
77
publicPath: "/build/",
8-
server: "desktop/main.ts",
98
serverBuildPath: "desktop/build/index.js",
109
devServerPort: 8002,
11-
ignoredRouteFiles: [".*"],
1210
}

template/tsconfig.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],
32
"compilerOptions": {
43
"lib": ["DOM", "DOM.Iterable", "ES2019"],
54
"isolatedModules": true,
@@ -9,12 +8,13 @@
98
"resolveJsonModule": true,
109
"target": "ES2019",
1110
"strict": true,
12-
"baseUrl": ".",
1311
"paths": {
1412
"~/*": ["./app/*"]
1513
},
16-
17-
// Remix takes care of building everything in `remix build`.
18-
"noEmit": true
19-
}
14+
"noEmit": true,
15+
"allowJs": true,
16+
"checkJs": true,
17+
"skipLibCheck": true
18+
},
19+
"exclude": ["desktop/build", "public/build", ".cache"]
2020
}

0 commit comments

Comments
 (0)