Skip to content

Commit c2e2d85

Browse files
committed
refactor: trying to upgrade electron-next to support esm
1 parent 98bd513 commit c2e2d85

File tree

3 files changed

+60
-28
lines changed

3 files changed

+60
-28
lines changed
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* This module enables hot-reloading with Nextjs during development.
3+
* The project `electron-next` was originally developed by Leo Lamprecht.
4+
* https://github.com/leo/electron-next
5+
*
6+
* That project went dormant in 2018.
7+
* As part of my effort to migrate to ESM, I've forked that module here.
8+
*/
9+
10+
import { app } from 'electron';
11+
import * as http from 'node:http';
12+
import type { NextServer, NextServerOptions } from 'next/dist/server/next.js';
13+
import { runInBackground } from '../../common/async/run-in-background.js';
14+
import { sleep } from '../../common/async/sleep.js';
15+
16+
export const serve = async (options: {
17+
/**
18+
* Path to the renderer directory, the root of your nextjs web app.
19+
*/
20+
rendererPath: string;
21+
/**
22+
* The port to serve the renderer on.
23+
*/
24+
port: number;
25+
}): Promise<void> => {
26+
const { rendererPath, port = 3000 } = options;
27+
28+
// Dynamically import nextjs to avoid bundling it with the app
29+
// when webpack compiles and tree-shakes the project.
30+
const { default: nextjs } = await import('next');
31+
32+
const createNextServer = nextjs as unknown as (
33+
options: NextServerOptions
34+
) => NextServer;
35+
36+
const nextServer = createNextServer({ dev: true, dir: rendererPath });
37+
38+
const requestHandler = nextServer.getRequestHandler();
39+
40+
// Build the renderer code and watch the files.
41+
await nextServer.prepare();
42+
43+
console.log('nextjs server is ready');
44+
await sleep(30_000);
45+
46+
// Create a new native HTTP server to support hot code reloading.
47+
const httpServer = http.createServer(
48+
(req: http.IncomingMessage, res: http.ServerResponse) => {
49+
runInBackground(async () => {
50+
await requestHandler(req, res);
51+
});
52+
}
53+
);
54+
55+
httpServer.listen(port, () => {
56+
// Make sure to stop the server when the app closes.
57+
// Otherwise it keeps running on its own.
58+
app.on('before-quit', () => httpServer.close());
59+
});
60+
};

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@
7878
"dotenv-flow": "^4.1.0",
7979
"electron": "^28.2.3",
8080
"electron-extension-installer": "^1.2.0",
81-
"electron-is-dev": "^3.0.1",
8281
"electron-log": "^5.1.1",
83-
"electron-next": "^3.1.5",
8482
"electron-serve": "^1.3.0",
8583
"fs-extra": "^11.2.0",
8684
"lodash-es": "^4.17.21",

yarn.lock

-26
Original file line numberDiff line numberDiff line change
@@ -3482,13 +3482,6 @@ __metadata:
34823482
languageName: node
34833483
linkType: hard
34843484

3485-
"app-root-path@npm:>=2.0.1":
3486-
version: 3.1.0
3487-
resolution: "app-root-path@npm:3.1.0"
3488-
checksum: 10c0/4a0fd976de1bffcdb18a5e1f8050091f15d0780e0582bca99aaa9d52de71f0e08e5185355fcffc781180bfb898499e787a2f5ed79b9c448b942b31dc947acaa9
3489-
languageName: node
3490-
linkType: hard
3491-
34923485
"aproba@npm:^1.0.3 || ^2.0.0, aproba@npm:^2.0.0":
34933486
version: 2.0.0
34943487
resolution: "aproba@npm:2.0.0"
@@ -5199,30 +5192,13 @@ __metadata:
51995192
languageName: node
52005193
linkType: hard
52015194

5202-
"electron-is-dev@npm:>=0.3.0, electron-is-dev@npm:^3.0.1":
5203-
version: 3.0.1
5204-
resolution: "electron-is-dev@npm:3.0.1"
5205-
checksum: 10c0/80d37d61d44b8b7af0af90eab622b09e0f165afbc2560297c760356efa93fcb54dbbd13d4d6522da6474e261d630b76b804c86385564a1f3062d28fc59c340bd
5206-
languageName: node
5207-
linkType: hard
5208-
52095195
"electron-log@npm:^5.1.1":
52105196
version: 5.1.1
52115197
resolution: "electron-log@npm:5.1.1"
52125198
checksum: 10c0/4ca427404d7a660df69146a4d16fcc266f0fa8d1da665b226cda50b8608f7ef032c43bef1330125b692835582afca4e2123602840b3216735c586dd49c16d03c
52135199
languageName: node
52145200
linkType: hard
52155201

5216-
"electron-next@npm:^3.1.5":
5217-
version: 3.1.5
5218-
resolution: "electron-next@npm:3.1.5"
5219-
dependencies:
5220-
app-root-path: "npm:>=2.0.1"
5221-
electron-is-dev: "npm:>=0.3.0"
5222-
checksum: 10c0/70aaf01b383a49a1fcdd7f258c93be5236a187b869b5ea5da4a906906ebad26be33514e21ef09a0a740760ad59fde78963e5c0391973220eeb68f0dc34b416d2
5223-
languageName: node
5224-
linkType: hard
5225-
52265202
"electron-publish@npm:24.13.1":
52275203
version: 24.13.1
52285204
resolution: "electron-publish@npm:24.13.1"
@@ -10050,9 +10026,7 @@ __metadata:
1005010026
electron: "npm:^28.2.3"
1005110027
electron-builder: "npm:^24.13.1"
1005210028
electron-extension-installer: "npm:^1.2.0"
10053-
electron-is-dev: "npm:^3.0.1"
1005410029
electron-log: "npm:^5.1.1"
10055-
electron-next: "npm:^3.1.5"
1005610030
electron-serve: "npm:^1.3.0"
1005710031
eslint: "npm:^8.56.0"
1005810032
eslint-config-next: "npm:^14.1.0"

0 commit comments

Comments
 (0)