@torpor/adapter-electron 1.1.8 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/dev-main.mjs +36 -3
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -1
- package/dist/main.mjs +33 -2
- package/dist/preload.cjs +9 -1
- package/dist/runtime.d.mts +1 -1
- package/dist/{types-Ce9F9DiP.d.mts → types-BhDc3oQ0.d.mts} +12 -1
- package/dist/types-BhDc3oQ0.d.mts.map +1 -0
- package/package.json +3 -3
- package/dist/types-Ce9F9DiP.d.mts.map +0 -1
package/README.md
CHANGED
|
@@ -66,6 +66,7 @@ site.adapter = electron({
|
|
|
66
66
|
partition: "persist:torpor", // session partition (cookies/web storage)
|
|
67
67
|
host: "127.0.0.1", // loopback host for the server
|
|
68
68
|
port: 0, // 0 = ephemeral; pin a port to keep web storage stable
|
|
69
|
+
theme: "system", // "system" (default), "light" or "dark"
|
|
69
70
|
window: { width: 1280, height: 800, title: "Flub" },
|
|
70
71
|
dev: true, // set false to skip the desktop window in dev
|
|
71
72
|
});
|
|
@@ -75,6 +76,23 @@ Cookies persist across launches regardless of the port. Web storage
|
|
|
75
76
|
(`localStorage`, `IndexedDB`) is origin-scoped, so pin `port` if the app relies
|
|
76
77
|
on it staying put.
|
|
77
78
|
|
|
79
|
+
## Native theme
|
|
80
|
+
|
|
81
|
+
The `theme` option sets Electron's `nativeTheme.themeSource` before the window
|
|
82
|
+
is created, so the title bar and window chrome follow the app's light/dark
|
|
83
|
+
mode, and picks a matching window background color (no white flash in dark
|
|
84
|
+
mode). An explicit `window.backgroundColor` always wins.
|
|
85
|
+
|
|
86
|
+
For runtime changes -- say a light/dark toggle inside the app -- call the
|
|
87
|
+
preload's `setNativeTheme` from the renderer:
|
|
88
|
+
|
|
89
|
+
```javascript
|
|
90
|
+
await window.electronAPI?.setNativeTheme("dark");
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
This changes the native chrome only; styling the app's own content is the
|
|
94
|
+
renderer's job (e.g. a `data-theme` attribute on `<html>`).
|
|
95
|
+
|
|
78
96
|
## Packaging
|
|
79
97
|
|
|
80
98
|
The emitted `dist/electron` app is a normal Electron app; package it with
|
package/dist/dev-main.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// The dev counterpart of main.mjs: opens a window against the Vite dev server
|
|
2
2
|
// (which the framework's Node dev plugin serves) instead of a built output, so
|
|
3
3
|
// HMR works. Launched by the adapter's dev plugin with TORPOR_ELECTRON_URL set.
|
|
4
|
-
import { app, BrowserWindow, session, shell } from "electron";
|
|
4
|
+
import { app, BrowserWindow, dialog, ipcMain, nativeTheme, session, shell } from "electron";
|
|
5
5
|
import path from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
7
|
|
|
@@ -11,14 +11,22 @@ const config = process.env.TORPOR_ELECTRON_CONFIG
|
|
|
11
11
|
? JSON.parse(process.env.TORPOR_ELECTRON_CONFIG)
|
|
12
12
|
: {};
|
|
13
13
|
const partition = config.partition ?? "persist:torpor";
|
|
14
|
+
const theme = config.theme ?? "system";
|
|
14
15
|
const origin = new URL(url).origin;
|
|
15
16
|
|
|
16
17
|
function createWindow() {
|
|
18
|
+
// Follow the app's theme before the window is created, and pick a
|
|
19
|
+
// background color that matches it: avoids a white flash in dark mode
|
|
20
|
+
// before the renderer's stylesheet applies
|
|
21
|
+
nativeTheme.themeSource = theme;
|
|
22
|
+
const backgroundColor =
|
|
23
|
+
config.window?.backgroundColor ?? (nativeTheme.shouldUseDarkColors ? "#000000" : "#ffffff");
|
|
24
|
+
|
|
17
25
|
const win = new BrowserWindow({
|
|
18
26
|
width: 1100,
|
|
19
27
|
height: 800,
|
|
20
28
|
show: false,
|
|
21
|
-
backgroundColor
|
|
29
|
+
backgroundColor,
|
|
22
30
|
...config.window,
|
|
23
31
|
webPreferences: {
|
|
24
32
|
preload: path.join(dir, "preload.cjs"),
|
|
@@ -35,6 +43,21 @@ function createWindow() {
|
|
|
35
43
|
return { action: "deny" };
|
|
36
44
|
});
|
|
37
45
|
|
|
46
|
+
// A renderer `beforeunload` handler that prevents unload would otherwise
|
|
47
|
+
// leave the window permanently unclosable: Electron suppresses Chromium's
|
|
48
|
+
// "leave site?" prompt, so ask about it here instead
|
|
49
|
+
win.webContents.on("will-prevent-unload", (event) => {
|
|
50
|
+
const choice = dialog.showMessageBoxSync(win, {
|
|
51
|
+
type: "warning",
|
|
52
|
+
buttons: ["Discard changes", "Cancel"],
|
|
53
|
+
defaultId: 1,
|
|
54
|
+
cancelId: 1,
|
|
55
|
+
message: "Unsaved Changes",
|
|
56
|
+
detail: "There are unsaved changes on this page. What would you like to do with them?",
|
|
57
|
+
});
|
|
58
|
+
if (choice === 0) event.preventDefault(); // allow the unload
|
|
59
|
+
});
|
|
60
|
+
|
|
38
61
|
win.once("ready-to-show", () => win.show());
|
|
39
62
|
void loadWithRetry(win);
|
|
40
63
|
}
|
|
@@ -54,7 +77,17 @@ async function loadWithRetry(win) {
|
|
|
54
77
|
|
|
55
78
|
app
|
|
56
79
|
.whenReady()
|
|
57
|
-
.then(
|
|
80
|
+
.then(() => {
|
|
81
|
+
// Runtime theme changes from the renderer
|
|
82
|
+
// (`window.electronAPI.setNativeTheme(mode)` in the preload)
|
|
83
|
+
ipcMain.handle("torpor:set-native-theme", (_event, mode) => {
|
|
84
|
+
if (mode === "system" || mode === "light" || mode === "dark") {
|
|
85
|
+
nativeTheme.themeSource = mode;
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return createWindow();
|
|
90
|
+
})
|
|
58
91
|
.catch((error) => {
|
|
59
92
|
console.error("[torpor] Failed to start:", error);
|
|
60
93
|
app.quit();
|
package/dist/index.d.mts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -89,6 +89,7 @@ function launch(site, url, options) {
|
|
|
89
89
|
const entry = path.join(adapterFolder, "dev-main.mjs");
|
|
90
90
|
const config = {
|
|
91
91
|
partition: options.partition ?? "persist:torpor",
|
|
92
|
+
theme: options.theme ?? "system",
|
|
92
93
|
window: options.window ?? {}
|
|
93
94
|
};
|
|
94
95
|
const child = spawn(electronBinary(site.root), [entry], {
|
|
@@ -124,6 +125,7 @@ async function postbuild(site, options) {
|
|
|
124
125
|
host: options.host ?? "127.0.0.1",
|
|
125
126
|
port: options.port ?? 0,
|
|
126
127
|
partition: options.partition ?? "persist:torpor",
|
|
128
|
+
theme: options.theme ?? "system",
|
|
127
129
|
window: options.window ?? {}
|
|
128
130
|
};
|
|
129
131
|
await promises.writeFile(path.join(outputFolder, "electron.json"), JSON.stringify(config, null, " "));
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":["fs"],"sources":["../src/electronBinary.ts","../src/sourceMode.ts","../src/electronDev.ts","../src/postbuild.ts","../src/adapter.ts"],"sourcesContent":["import { createRequire } from \"node:module\";\nimport path from \"node:path\";\n\n/**\n * Resolves the Electron executable to run, preferring the copy installed in\n * the site's node_modules (`require(\"electron\")` resolves to the binary path)\n * and falling back to `electron` on PATH. The package may not be installed --\n * e.g. a CI build that only builds the site -- in which case launching is\n * skipped and callers report how to install it.\n */\nexport default function electronBinary(siteRoot: string): string {\n\ttry {\n\t\tconst require = createRequire(path.join(siteRoot, \"package.json\"));\n\t\tconst resolved: unknown = require(\"electron\");\n\t\treturn typeof resolved === \"string\" ? resolved : \"electron\";\n\t} catch {\n\t\treturn \"electron\";\n\t}\n}\n","import { realpathSync } from \"node:fs\";\nimport path from \"node:path\";\n\n// Mirrors the entry-path logic in @torpor/build's `utils/entryPaths.ts` (kept\n// local so the adapter stays compatible with older @torpor/build installs).\n\n/**\n * Whether the framework is symlinked into the app from outside node_modules\n * (workspace links and `link:` overrides), meaning tb runs it from source via\n * the `development` export conditions. Registry installs always run dist.\n */\nexport default function detectSourceMode(siteRoot: string): boolean {\n\tif (process.env.TORPOR_SOURCE_DEV) return true;\n\tconst packages = [\"@torpor/build\", \"@torpor/view\"];\n\treturn packages.every((pkg) => {\n\t\ttry {\n\t\t\tconst dist = path.resolve(siteRoot, \"node_modules\", pkg, \"dist/index.mjs\");\n\t\t\tconst real = realpathSync(dist);\n\t\t\treturn !real.includes(\"node_modules\");\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t});\n}\n","import { type Site } from \"@torpor/build\";\nimport { nodeDev } from \"@torpor/build/node-dev\";\nimport { spawn, type ChildProcess } from \"node:child_process\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport type { Plugin, ViteDevServer } from \"vite\";\nimport electronBinary from \"./electronBinary\";\nimport detectSourceMode from \"./sourceMode\";\nimport type { ElectronOptions } from \"./types\";\n\n/**\n * Dev support: the framework's default Node dev runtime (which serves SSR off\n * the Vite dev server) plus a launcher that opens an Electron window pointed at\n * it. Without `nodeDev`, providing `Adapter.dev` would replace the Node\n * runtime entirely, which Electron doesn't need -- it runs on Node.\n */\nexport default function electronDev(site: Site, options: ElectronOptions): Plugin[] {\n\treturn [nodeDev(site, detectSourceMode(site.root)), electronLauncher(site, options)];\n}\n\nfunction electronLauncher(site: Site, options: ElectronOptions): Plugin {\n\tlet child: ChildProcess | undefined;\n\tlet quitting = false;\n\n\tconst stopChild = (): void => {\n\t\tchild?.kill();\n\t\tchild = undefined;\n\t};\n\n\treturn {\n\t\tname: \"torpor-electron-dev\",\n\t\tconfigureServer(server: ViteDevServer) {\n\t\t\tif (options.dev === false || process.env.TORPOR_ELECTRON === \"0\") return;\n\n\t\t\tconst start = (): void => {\n\t\t\t\tconst url = devServerUrl(server);\n\t\t\t\tif (!url) return;\n\t\t\t\tstopChild();\n\t\t\t\tquitting = false;\n\n\t\t\t\tconst proc = launch(site, url, options);\n\t\t\t\tchild = proc;\n\n\t\t\t\t// When the user quits the app (Cmd+Q, or closing the window),\n\t\t\t\t// stop the dev server too, so `tb --dev` exits with the desktop\n\t\t\t\t// app. A failed launch emits \"error\", not \"exit\", so browser-only\n\t\t\t\t// dev is unaffected\n\t\t\t\tproc.once(\"exit\", () => {\n\t\t\t\t\tif (quitting) return;\n\t\t\t\t\tquitting = true;\n\t\t\t\t\tvoid server.close();\n\t\t\t\t});\n\t\t\t};\n\n\t\t\tconst httpServer = server.httpServer;\n\t\t\tif (httpServer?.listening) {\n\t\t\t\tstart();\n\t\t\t} else {\n\t\t\t\thttpServer?.once(\"listening\", start);\n\t\t\t}\n\n\t\t\t// Vite is shutting down (Ctrl+C, a restart, or close() from the\n\t\t\t// handler above): kill the window without re-closing the server\n\t\t\thttpServer?.once(\"close\", () => {\n\t\t\t\tquitting = true;\n\t\t\t\tstopChild();\n\t\t\t});\n\n\t\t\tprocess.once(\"exit\", stopChild);\n\t\t},\n\t};\n}\n\nfunction launch(site: Site, url: string, options: ElectronOptions): ChildProcess {\n\tconst adapterFolder = path.dirname(fileURLToPath(import.meta.url));\n\tconst entry = path.join(adapterFolder, \"dev-main.mjs\");\n\n\tconst config = {\n\t\tpartition: options.partition ?? \"persist:torpor\",\n\t\twindow: options.window ?? {},\n\t};\n\n\tconst child = spawn(electronBinary(site.root), [entry], {\n\t\tcwd: site.root,\n\t\tstdio: \"inherit\",\n\t\tenv: {\n\t\t\t...process.env,\n\t\t\tTORPOR_ELECTRON_URL: url,\n\t\t\tTORPOR_ELECTRON_CONFIG: JSON.stringify(config),\n\t\t},\n\t});\n\n\tchild.on(\"error\", (error: NodeJS.ErrnoException) => {\n\t\tif (error.code === \"ENOENT\") {\n\t\t\tconsole.warn(\n\t\t\t\t\"[torpor] Electron is not installed, so dev is running in the browser only. \" +\n\t\t\t\t\t\"Install it with `npm install -D electron` to open a desktop window.\",\n\t\t\t);\n\t\t} else {\n\t\t\tconsole.warn(`[torpor] Could not launch Electron: ${error.message}`);\n\t\t}\n\t});\n\n\treturn child;\n}\n\nfunction devServerUrl(server: ViteDevServer): string | undefined {\n\tconst local = server.resolvedUrls?.local?.[0];\n\tif (local) return local;\n\n\tconst address = server.httpServer?.address();\n\tif (address && typeof address === \"object\") {\n\t\tconst protocol = server.config.server.https ? \"https\" : \"http\";\n\t\tconst host =\n\t\t\taddress.address === \"::\" || address.address === \"0.0.0.0\" ? \"localhost\" : address.address;\n\t\treturn `${protocol}://${host}:${address.port}`;\n\t}\n\n\treturn undefined;\n}\n","import type { Site } from \"@torpor/build\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport type { ElectronOptions } from \"./types\";\n\n// The Electron main and preload scripts, copied verbatim from the adapter's\n// dist into the site's dist/electron output\nconst RUNTIME_FILES = [\"main.mjs\", \"preload.cjs\"];\n\nexport default async function postbuild(site: Site, options: ElectronOptions): Promise<void> {\n\tconst sourceFolder = path.dirname(fileURLToPath(import.meta.url));\n\tconst outputFolder = path.join(site.outputFolder, \"electron\");\n\n\tawait fs.mkdir(outputFolder, { recursive: true });\n\tfor (const file of RUNTIME_FILES) {\n\t\tawait fs.copyFile(path.join(sourceFolder, file), path.join(outputFolder, file));\n\t}\n\n\tconst config = {\n\t\thost: options.host ?? \"127.0.0.1\",\n\t\tport: options.port ?? 0,\n\t\tpartition: options.partition ?? \"persist:torpor\",\n\t\twindow: options.window ?? {},\n\t};\n\tawait fs.writeFile(path.join(outputFolder, \"electron.json\"), JSON.stringify(config, null, \"\\t\"));\n\n\t// A minimal Electron app manifest. Run it with `electron dist/electron`\n\t// (see the adapter README for packaging with electron-builder/forge)\n\tconst { name, version } = await sitePackage(site.root);\n\tawait fs.writeFile(\n\t\tpath.join(outputFolder, \"package.json\"),\n\t\tJSON.stringify(\n\t\t\t{\n\t\t\t\tname: `${name}-electron`,\n\t\t\t\tproductName: name,\n\t\t\t\tversion,\n\t\t\t\tprivate: true,\n\t\t\t\ttype: \"module\",\n\t\t\t\tmain: \"main.mjs\",\n\t\t\t\tscripts: { start: \"electron .\" },\n\t\t\t},\n\t\t\tnull,\n\t\t\t\"\\t\",\n\t\t),\n\t);\n}\n\nasync function sitePackage(root: string): Promise<{ name: string; version: string }> {\n\ttry {\n\t\tconst pkg = JSON.parse(await fs.readFile(path.join(root, \"package.json\"), \"utf-8\"));\n\t\tconst name = typeof pkg.name === \"string\" ? pkg.name.replace(/^@[^/]+\\//, \"\") : \"torpor-app\";\n\t\tconst version = typeof pkg.version === \"string\" ? pkg.version : \"1.0.0\";\n\t\treturn { name, version };\n\t} catch {\n\t\treturn { name: \"torpor-app\", version: \"1.0.0\" };\n\t}\n}\n","import { type Adapter, type Site } from \"@torpor/build\";\nimport { spawn } from \"node:child_process\";\nimport { existsSync } from \"node:fs\";\nimport path from \"node:path\";\nimport electronBinary from \"./electronBinary\";\nimport electronDev from \"./electronDev\";\nimport postbuild from \"./postbuild\";\nimport type { ElectronOptions } from \"./types\";\n\n/**\n * A @torpor/build adapter for desktop apps.\n *\n * Builds emit an Electron app into `dist/electron` (a main process that serves\n * the site over a loopback HTTP server, a context-isolated preload, and a\n * manifest); preview launches it, and dev opens a window against the Vite dev\n * server. Requests run in the Electron main process on Node, so cookies,\n * sessions and redirects behave exactly as on a normal Node server.\n *\n * ```ts\n * import { electron } from \"@torpor/adapter-electron\";\n *\n * site.adapter = electron();\n * ```\n */\nexport default function electron(options: ElectronOptions = {}): Adapter {\n\treturn {\n\t\tdev: (site: Site) => electronDev(site, options),\n\t\tpostbuild: (site: Site) => postbuild(site, options),\n\t\tserve: (_server, site: Site) => serve(site),\n\t};\n}\n\nfunction serve(site: Site): void {\n\tconst appFolder = path.join(site.outputFolder, \"electron\");\n\tif (!existsSync(path.join(appFolder, \"main.mjs\"))) {\n\t\tthrow new Error(\n\t\t\t\"Electron output not found. Run a build first (dist/electron/main.mjs is missing).\",\n\t\t);\n\t}\n\n\tconst child = spawn(electronBinary(site.root), [appFolder], {\n\t\tcwd: site.root,\n\t\tstdio: \"inherit\",\n\t});\n\n\tchild.on(\"error\", (error: NodeJS.ErrnoException) => {\n\t\tif (error.code === \"ENOENT\") {\n\t\t\tconsole.error(\n\t\t\t\t\"[torpor] Electron is not installed. Install it with `npm install -D electron`.\",\n\t\t\t);\n\t\t\tprocess.exit(1);\n\t\t}\n\t\tthrow error;\n\t});\n\n\tchild.on(\"exit\", (code) => process.exit(code ?? 0));\n}\n"],"mappings":";;;;;;;;;;;;;;AAUA,SAAwB,eAAe,UAA0B;CAChE,IAAI;EAEH,MAAM,WADU,cAAc,KAAK,KAAK,UAAU,cAAc,CAChC,CAAC,CAAC,UAAU;EAC5C,OAAO,OAAO,aAAa,WAAW,WAAW;CAClD,QAAQ;EACP,OAAO;CACR;AACD;;;;;;;;ACPA,SAAwB,iBAAiB,UAA2B;CACnE,IAAI,QAAQ,IAAI,mBAAmB,OAAO;CAE1C,OAAO,CADW,iBAAiB,cACrB,CAAC,CAAC,OAAO,QAAQ;EAC9B,IAAI;GACH,MAAM,OAAO,KAAK,QAAQ,UAAU,gBAAgB,KAAK,gBAAgB;GAEzE,OAAO,CADM,aAAa,IACf,CAAC,CAAC,SAAS,cAAc;EACrC,QAAQ;GACP,OAAO;EACR;CACD,CAAC;AACF;;;;;;;;;ACPA,SAAwB,YAAY,MAAY,SAAoC;CACnF,OAAO,CAAC,QAAQ,MAAM,iBAAiB,KAAK,IAAI,CAAC,GAAG,iBAAiB,MAAM,OAAO,CAAC;AACpF;AAEA,SAAS,iBAAiB,MAAY,SAAkC;CACvE,IAAI;CACJ,IAAI,WAAW;CAEf,MAAM,kBAAwB;EAC7B,OAAO,KAAK;EACZ,QAAQ,KAAA;CACT;CAEA,OAAO;EACN,MAAM;EACN,gBAAgB,QAAuB;GACtC,IAAI,QAAQ,QAAQ,SAAS,QAAQ,IAAI,oBAAoB,KAAK;GAElE,MAAM,cAAoB;IACzB,MAAM,MAAM,aAAa,MAAM;IAC/B,IAAI,CAAC,KAAK;IACV,UAAU;IACV,WAAW;IAEX,MAAM,OAAO,OAAO,MAAM,KAAK,OAAO;IACtC,QAAQ;IAMR,KAAK,KAAK,cAAc;KACvB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AEA,SAAS,MAAM,MAAkB;CAChC,MAAM,YAAY,KAAK,KAAK,KAAK,cAAc,UAAU;CACzD,IAAI,CAAC,WAAW,KAAK,KAAK,WAAW,UAAU,CAAC,GAC/C,MAAM,IAAI,MACT,mFACD;CAGD,MAAM,QAAQ,MAAM,eAAe,KAAK,IAAI,GAAG,CAAC,SAAS,GAAG;EAC3D,KAAK,KAAK;EACV,OAAO;CACR,CAAC;CAED,MAAM,GAAG,UAAU,UAAiC;EACnD,IAAI,MAAM,SAAS,UAAU;GAC5B,QAAQ,MACP,gFACD;GACA,QAAQ,KAAK,CAAC;EACf;EACA,MAAM;CACP,CAAC;CAED,MAAM,GAAG,SAAS,SAAS,QAAQ,KAAK,QAAQ,CAAC,CAAC;AACnD"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":["fs"],"sources":["../src/electronBinary.ts","../src/sourceMode.ts","../src/electronDev.ts","../src/postbuild.ts","../src/adapter.ts"],"sourcesContent":["import { createRequire } from \"node:module\";\nimport path from \"node:path\";\n\n/**\n * Resolves the Electron executable to run, preferring the copy installed in\n * the site's node_modules (`require(\"electron\")` resolves to the binary path)\n * and falling back to `electron` on PATH. The package may not be installed --\n * e.g. a CI build that only builds the site -- in which case launching is\n * skipped and callers report how to install it.\n */\nexport default function electronBinary(siteRoot: string): string {\n\ttry {\n\t\tconst require = createRequire(path.join(siteRoot, \"package.json\"));\n\t\tconst resolved: unknown = require(\"electron\");\n\t\treturn typeof resolved === \"string\" ? resolved : \"electron\";\n\t} catch {\n\t\treturn \"electron\";\n\t}\n}\n","import { realpathSync } from \"node:fs\";\nimport path from \"node:path\";\n\n// Mirrors the entry-path logic in @torpor/build's `utils/entryPaths.ts` (kept\n// local so the adapter stays compatible with older @torpor/build installs).\n\n/**\n * Whether the framework is symlinked into the app from outside node_modules\n * (workspace links and `link:` overrides), meaning tb runs it from source via\n * the `development` export conditions. Registry installs always run dist.\n */\nexport default function detectSourceMode(siteRoot: string): boolean {\n\tif (process.env.TORPOR_SOURCE_DEV) return true;\n\tconst packages = [\"@torpor/build\", \"@torpor/view\"];\n\treturn packages.every((pkg) => {\n\t\ttry {\n\t\t\tconst dist = path.resolve(siteRoot, \"node_modules\", pkg, \"dist/index.mjs\");\n\t\t\tconst real = realpathSync(dist);\n\t\t\treturn !real.includes(\"node_modules\");\n\t\t} catch {\n\t\t\treturn false;\n\t\t}\n\t});\n}\n","import { type Site } from \"@torpor/build\";\nimport { nodeDev } from \"@torpor/build/node-dev\";\nimport { spawn, type ChildProcess } from \"node:child_process\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport type { Plugin, ViteDevServer } from \"vite\";\nimport electronBinary from \"./electronBinary\";\nimport detectSourceMode from \"./sourceMode\";\nimport type { ElectronOptions } from \"./types\";\n\n/**\n * Dev support: the framework's default Node dev runtime (which serves SSR off\n * the Vite dev server) plus a launcher that opens an Electron window pointed at\n * it. Without `nodeDev`, providing `Adapter.dev` would replace the Node\n * runtime entirely, which Electron doesn't need -- it runs on Node.\n */\nexport default function electronDev(site: Site, options: ElectronOptions): Plugin[] {\n\treturn [nodeDev(site, detectSourceMode(site.root)), electronLauncher(site, options)];\n}\n\nfunction electronLauncher(site: Site, options: ElectronOptions): Plugin {\n\tlet child: ChildProcess | undefined;\n\tlet quitting = false;\n\n\tconst stopChild = (): void => {\n\t\tchild?.kill();\n\t\tchild = undefined;\n\t};\n\n\treturn {\n\t\tname: \"torpor-electron-dev\",\n\t\tconfigureServer(server: ViteDevServer) {\n\t\t\tif (options.dev === false || process.env.TORPOR_ELECTRON === \"0\") return;\n\n\t\t\tconst start = (): void => {\n\t\t\t\tconst url = devServerUrl(server);\n\t\t\t\tif (!url) return;\n\t\t\t\tstopChild();\n\t\t\t\tquitting = false;\n\n\t\t\t\tconst proc = launch(site, url, options);\n\t\t\t\tchild = proc;\n\n\t\t\t\t// When the user quits the app (Cmd+Q, or closing the window),\n\t\t\t\t// stop the dev server too, so `tb --dev` exits with the desktop\n\t\t\t\t// app. A failed launch emits \"error\", not \"exit\", so browser-only\n\t\t\t\t// dev is unaffected\n\t\t\t\tproc.once(\"exit\", () => {\n\t\t\t\t\tif (quitting) return;\n\t\t\t\t\tquitting = true;\n\t\t\t\t\tvoid server.close();\n\t\t\t\t});\n\t\t\t};\n\n\t\t\tconst httpServer = server.httpServer;\n\t\t\tif (httpServer?.listening) {\n\t\t\t\tstart();\n\t\t\t} else {\n\t\t\t\thttpServer?.once(\"listening\", start);\n\t\t\t}\n\n\t\t\t// Vite is shutting down (Ctrl+C, a restart, or close() from the\n\t\t\t// handler above): kill the window without re-closing the server\n\t\t\thttpServer?.once(\"close\", () => {\n\t\t\t\tquitting = true;\n\t\t\t\tstopChild();\n\t\t\t});\n\n\t\t\tprocess.once(\"exit\", stopChild);\n\t\t},\n\t};\n}\n\nfunction launch(site: Site, url: string, options: ElectronOptions): ChildProcess {\n\tconst adapterFolder = path.dirname(fileURLToPath(import.meta.url));\n\tconst entry = path.join(adapterFolder, \"dev-main.mjs\");\n\n\tconst config = {\n\t\tpartition: options.partition ?? \"persist:torpor\",\n\t\ttheme: options.theme ?? \"system\",\n\t\twindow: options.window ?? {},\n\t};\n\n\tconst child = spawn(electronBinary(site.root), [entry], {\n\t\tcwd: site.root,\n\t\tstdio: \"inherit\",\n\t\tenv: {\n\t\t\t...process.env,\n\t\t\tTORPOR_ELECTRON_URL: url,\n\t\t\tTORPOR_ELECTRON_CONFIG: JSON.stringify(config),\n\t\t},\n\t});\n\n\tchild.on(\"error\", (error: NodeJS.ErrnoException) => {\n\t\tif (error.code === \"ENOENT\") {\n\t\t\tconsole.warn(\n\t\t\t\t\"[torpor] Electron is not installed, so dev is running in the browser only. \" +\n\t\t\t\t\t\"Install it with `npm install -D electron` to open a desktop window.\",\n\t\t\t);\n\t\t} else {\n\t\t\tconsole.warn(`[torpor] Could not launch Electron: ${error.message}`);\n\t\t}\n\t});\n\n\treturn child;\n}\n\nfunction devServerUrl(server: ViteDevServer): string | undefined {\n\tconst local = server.resolvedUrls?.local?.[0];\n\tif (local) return local;\n\n\tconst address = server.httpServer?.address();\n\tif (address && typeof address === \"object\") {\n\t\tconst protocol = server.config.server.https ? \"https\" : \"http\";\n\t\tconst host =\n\t\t\taddress.address === \"::\" || address.address === \"0.0.0.0\" ? \"localhost\" : address.address;\n\t\treturn `${protocol}://${host}:${address.port}`;\n\t}\n\n\treturn undefined;\n}\n","import type { Site } from \"@torpor/build\";\nimport { promises as fs } from \"node:fs\";\nimport path from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport type { ElectronOptions } from \"./types\";\n\n// The Electron main and preload scripts, copied verbatim from the adapter's\n// dist into the site's dist/electron output\nconst RUNTIME_FILES = [\"main.mjs\", \"preload.cjs\"];\n\nexport default async function postbuild(site: Site, options: ElectronOptions): Promise<void> {\n\tconst sourceFolder = path.dirname(fileURLToPath(import.meta.url));\n\tconst outputFolder = path.join(site.outputFolder, \"electron\");\n\n\tawait fs.mkdir(outputFolder, { recursive: true });\n\tfor (const file of RUNTIME_FILES) {\n\t\tawait fs.copyFile(path.join(sourceFolder, file), path.join(outputFolder, file));\n\t}\n\n\tconst config = {\n\t\thost: options.host ?? \"127.0.0.1\",\n\t\tport: options.port ?? 0,\n\t\tpartition: options.partition ?? \"persist:torpor\",\n\t\ttheme: options.theme ?? \"system\",\n\t\twindow: options.window ?? {},\n\t};\n\tawait fs.writeFile(path.join(outputFolder, \"electron.json\"), JSON.stringify(config, null, \"\\t\"));\n\n\t// A minimal Electron app manifest. Run it with `electron dist/electron`\n\t// (see the adapter README for packaging with electron-builder/forge)\n\tconst { name, version } = await sitePackage(site.root);\n\tawait fs.writeFile(\n\t\tpath.join(outputFolder, \"package.json\"),\n\t\tJSON.stringify(\n\t\t\t{\n\t\t\t\tname: `${name}-electron`,\n\t\t\t\tproductName: name,\n\t\t\t\tversion,\n\t\t\t\tprivate: true,\n\t\t\t\ttype: \"module\",\n\t\t\t\tmain: \"main.mjs\",\n\t\t\t\tscripts: { start: \"electron .\" },\n\t\t\t},\n\t\t\tnull,\n\t\t\t\"\\t\",\n\t\t),\n\t);\n}\n\nasync function sitePackage(root: string): Promise<{ name: string; version: string }> {\n\ttry {\n\t\tconst pkg = JSON.parse(await fs.readFile(path.join(root, \"package.json\"), \"utf-8\"));\n\t\tconst name = typeof pkg.name === \"string\" ? pkg.name.replace(/^@[^/]+\\//, \"\") : \"torpor-app\";\n\t\tconst version = typeof pkg.version === \"string\" ? pkg.version : \"1.0.0\";\n\t\treturn { name, version };\n\t} catch {\n\t\treturn { name: \"torpor-app\", version: \"1.0.0\" };\n\t}\n}\n","import { type Adapter, type Site } from \"@torpor/build\";\nimport { spawn } from \"node:child_process\";\nimport { existsSync } from \"node:fs\";\nimport path from \"node:path\";\nimport electronBinary from \"./electronBinary\";\nimport electronDev from \"./electronDev\";\nimport postbuild from \"./postbuild\";\nimport type { ElectronOptions } from \"./types\";\n\n/**\n * A @torpor/build adapter for desktop apps.\n *\n * Builds emit an Electron app into `dist/electron` (a main process that serves\n * the site over a loopback HTTP server, a context-isolated preload, and a\n * manifest); preview launches it, and dev opens a window against the Vite dev\n * server. Requests run in the Electron main process on Node, so cookies,\n * sessions and redirects behave exactly as on a normal Node server.\n *\n * ```ts\n * import { electron } from \"@torpor/adapter-electron\";\n *\n * site.adapter = electron();\n * ```\n */\nexport default function electron(options: ElectronOptions = {}): Adapter {\n\treturn {\n\t\tdev: (site: Site) => electronDev(site, options),\n\t\tpostbuild: (site: Site) => postbuild(site, options),\n\t\tserve: (_server, site: Site) => serve(site),\n\t};\n}\n\nfunction serve(site: Site): void {\n\tconst appFolder = path.join(site.outputFolder, \"electron\");\n\tif (!existsSync(path.join(appFolder, \"main.mjs\"))) {\n\t\tthrow new Error(\n\t\t\t\"Electron output not found. Run a build first (dist/electron/main.mjs is missing).\",\n\t\t);\n\t}\n\n\tconst child = spawn(electronBinary(site.root), [appFolder], {\n\t\tcwd: site.root,\n\t\tstdio: \"inherit\",\n\t});\n\n\tchild.on(\"error\", (error: NodeJS.ErrnoException) => {\n\t\tif (error.code === \"ENOENT\") {\n\t\t\tconsole.error(\n\t\t\t\t\"[torpor] Electron is not installed. Install it with `npm install -D electron`.\",\n\t\t\t);\n\t\t\tprocess.exit(1);\n\t\t}\n\t\tthrow error;\n\t});\n\n\tchild.on(\"exit\", (code) => process.exit(code ?? 0));\n}\n"],"mappings":";;;;;;;;;;;;;;AAUA,SAAwB,eAAe,UAA0B;CAChE,IAAI;EAEH,MAAM,WADU,cAAc,KAAK,KAAK,UAAU,cAAc,CAChC,CAAC,CAAC,UAAU;EAC5C,OAAO,OAAO,aAAa,WAAW,WAAW;CAClD,QAAQ;EACP,OAAO;CACR;AACD;;;;;;;;ACPA,SAAwB,iBAAiB,UAA2B;CACnE,IAAI,QAAQ,IAAI,mBAAmB,OAAO;CAE1C,OAAO,CADW,iBAAiB,cACrB,CAAC,CAAC,OAAO,QAAQ;EAC9B,IAAI;GACH,MAAM,OAAO,KAAK,QAAQ,UAAU,gBAAgB,KAAK,gBAAgB;GAEzE,OAAO,CADM,aAAa,IACf,CAAC,CAAC,SAAS,cAAc;EACrC,QAAQ;GACP,OAAO;EACR;CACD,CAAC;AACF;;;;;;;;;ACPA,SAAwB,YAAY,MAAY,SAAoC;CACnF,OAAO,CAAC,QAAQ,MAAM,iBAAiB,KAAK,IAAI,CAAC,GAAG,iBAAiB,MAAM,OAAO,CAAC;AACpF;AAEA,SAAS,iBAAiB,MAAY,SAAkC;CACvE,IAAI;CACJ,IAAI,WAAW;CAEf,MAAM,kBAAwB;EAC7B,OAAO,KAAK;EACZ,QAAQ,KAAA;CACT;CAEA,OAAO;EACN,MAAM;EACN,gBAAgB,QAAuB;GACtC,IAAI,QAAQ,QAAQ,SAAS,QAAQ,IAAI,oBAAoB,KAAK;GAElE,MAAM,cAAoB;IACzB,MAAM,MAAM,aAAa,MAAM;IAC/B,IAAI,CAAC,KAAK;IACV,UAAU;IACV,WAAW;IAEX,MAAM,OAAO,OAAO,MAAM,KAAK,OAAO;IAC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EAC9C,YAAY,SAAe,UAAU,MAAM,OAAO;EAClD,QAAQ,SAAS,SAAe,MAAM,IAAI;CAC3C;AACD;AAEA,SAAS,MAAM,MAAkB;CAChC,MAAM,YAAY,KAAK,KAAK,KAAK,cAAc,UAAU;CACzD,IAAI,CAAC,WAAW,KAAK,KAAK,WAAW,UAAU,CAAC,GAC/C,MAAM,IAAI,MACT,mFACD;CAGD,MAAM,QAAQ,MAAM,eAAe,KAAK,IAAI,GAAG,CAAC,SAAS,GAAG;EAC3D,KAAK,KAAK;EACV,OAAO;CACR,CAAC;CAED,MAAM,GAAG,UAAU,UAAiC;EACnD,IAAI,MAAM,SAAS,UAAU;GAC5B,QAAQ,MACP,gFACD;GACA,QAAQ,KAAK,CAAC;EACf;EACA,MAAM;CACP,CAAC;CAED,MAAM,GAAG,SAAS,SAAS,QAAQ,KAAK,QAAQ,CAAC,CAAC;AACnD"}
|
package/dist/main.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// It serves the built site over a loopback HTTP server and shows it in a
|
|
3
3
|
// BrowserWindow. Requests run here, in the main process, so cookies, sessions
|
|
4
4
|
// and redirects behave exactly as on a Node server.
|
|
5
|
-
import { app, BrowserWindow, session, shell } from "electron";
|
|
5
|
+
import { app, BrowserWindow, dialog, ipcMain, nativeTheme, session, shell } from "electron";
|
|
6
6
|
import { readFileSync } from "node:fs";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { fileURLToPath } from "node:url";
|
|
@@ -14,16 +14,24 @@ const dir = path.dirname(fileURLToPath(import.meta.url));
|
|
|
14
14
|
const output = path.resolve(dir, "..");
|
|
15
15
|
const config = JSON.parse(readFileSync(path.join(dir, "electron.json"), "utf-8"));
|
|
16
16
|
const partition = config.partition ?? "persist:torpor";
|
|
17
|
+
const theme = config.theme ?? "system";
|
|
17
18
|
|
|
18
19
|
let server;
|
|
19
20
|
let origin = "";
|
|
20
21
|
|
|
21
22
|
async function createWindow() {
|
|
23
|
+
// Follow the app's theme before the window is created, and pick a
|
|
24
|
+
// background color that matches it: avoids a white flash in dark mode
|
|
25
|
+
// before the renderer's stylesheet applies
|
|
26
|
+
nativeTheme.themeSource = theme;
|
|
27
|
+
const backgroundColor =
|
|
28
|
+
config.window?.backgroundColor ?? (nativeTheme.shouldUseDarkColors ? "#000000" : "#ffffff");
|
|
29
|
+
|
|
22
30
|
const win = new BrowserWindow({
|
|
23
31
|
width: 1100,
|
|
24
32
|
height: 800,
|
|
25
33
|
show: false,
|
|
26
|
-
backgroundColor
|
|
34
|
+
backgroundColor,
|
|
27
35
|
...config.window,
|
|
28
36
|
webPreferences: {
|
|
29
37
|
preload: path.join(dir, "preload.cjs"),
|
|
@@ -40,6 +48,21 @@ async function createWindow() {
|
|
|
40
48
|
return { action: "deny" };
|
|
41
49
|
});
|
|
42
50
|
|
|
51
|
+
// A renderer `beforeunload` handler that prevents unload would otherwise
|
|
52
|
+
// leave the window permanently unclosable: Electron suppresses Chromium's
|
|
53
|
+
// "leave site?" prompt, so ask about it here instead
|
|
54
|
+
win.webContents.on("will-prevent-unload", (event) => {
|
|
55
|
+
const choice = dialog.showMessageBoxSync(win, {
|
|
56
|
+
type: "warning",
|
|
57
|
+
buttons: ["Discard changes", "Cancel"],
|
|
58
|
+
defaultId: 1,
|
|
59
|
+
cancelId: 1,
|
|
60
|
+
message: "Unsaved Changes",
|
|
61
|
+
detail: "There are unsaved changes on this page. What would you like to do with them?",
|
|
62
|
+
});
|
|
63
|
+
if (choice === 0) event.preventDefault(); // allow the unload
|
|
64
|
+
});
|
|
65
|
+
|
|
43
66
|
win.once("ready-to-show", () => win.show());
|
|
44
67
|
await win.loadURL(`${server.url}/`);
|
|
45
68
|
}
|
|
@@ -47,6 +70,14 @@ async function createWindow() {
|
|
|
47
70
|
app
|
|
48
71
|
.whenReady()
|
|
49
72
|
.then(async () => {
|
|
73
|
+
// Runtime theme changes from the renderer
|
|
74
|
+
// (`window.electronAPI.setNativeTheme(mode)` in the preload)
|
|
75
|
+
ipcMain.handle("torpor:set-native-theme", (_event, mode) => {
|
|
76
|
+
if (mode === "system" || mode === "light" || mode === "dark") {
|
|
77
|
+
nativeTheme.themeSource = mode;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
50
81
|
server = await startServer(output, { host: config.host, port: config.port });
|
|
51
82
|
origin = server.url;
|
|
52
83
|
await createWindow();
|
package/dist/preload.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Runs in an isolated world before the page loads. Keep this surface as small
|
|
2
2
|
// as possible, and never expose Node or Electron modules to the renderer.
|
|
3
|
-
const { contextBridge } = require("electron");
|
|
3
|
+
const { contextBridge, ipcRenderer } = require("electron");
|
|
4
4
|
|
|
5
5
|
contextBridge.exposeInMainWorld("electronAPI", {
|
|
6
6
|
platform: process.platform,
|
|
@@ -9,4 +9,12 @@ contextBridge.exposeInMainWorld("electronAPI", {
|
|
|
9
9
|
electron: process.versions.electron,
|
|
10
10
|
node: process.versions.node,
|
|
11
11
|
},
|
|
12
|
+
/**
|
|
13
|
+
* Sets the native theme mode: `"system"` (follow the OS), `"light"` or
|
|
14
|
+
* `"dark"`. Affects the window chrome (title bar, dialogs); styling the
|
|
15
|
+
* app's own content stays the renderer's job.
|
|
16
|
+
*/
|
|
17
|
+
setNativeTheme(mode) {
|
|
18
|
+
return ipcRenderer.invoke("torpor:set-native-theme", mode);
|
|
19
|
+
},
|
|
12
20
|
});
|
package/dist/runtime.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as StartServerOptions, r as RunningServer } from "./types-
|
|
1
|
+
import { i as StartServerOptions, r as RunningServer } from "./types-BhDc3oQ0.mjs";
|
|
2
2
|
//#region src/runtime.d.ts
|
|
3
3
|
/**
|
|
4
4
|
* Loads a `.env` file from the site root into `process.env`, when the runtime's
|
|
@@ -23,6 +23,17 @@ interface ElectronWindowOptions {
|
|
|
23
23
|
transparent?: boolean;
|
|
24
24
|
}
|
|
25
25
|
interface ElectronOptions {
|
|
26
|
+
/**
|
|
27
|
+
* The native theme mode the window starts in: `"system"` (follow the OS,
|
|
28
|
+
* the default), `"light"` or `"dark"`. Sets Electron's `nativeTheme.themeSource`
|
|
29
|
+
* before the window is created, so the title bar and window chrome follow
|
|
30
|
+
* the app's light/dark mode, and picks a matching background color so there
|
|
31
|
+
* is no white flash before the renderer's stylesheet applies.
|
|
32
|
+
*
|
|
33
|
+
* For runtime changes (e.g. a light/dark toggle in the app), call
|
|
34
|
+
* `window.electronAPI?.setNativeTheme(mode)` from the renderer.
|
|
35
|
+
*/
|
|
36
|
+
theme?: "system" | "light" | "dark";
|
|
26
37
|
/**
|
|
27
38
|
* Options merged into every `new BrowserWindow(...)`. The adapter's own
|
|
28
39
|
* secure defaults (`contextIsolation`, `sandbox`, no `nodeIntegration`)
|
|
@@ -72,4 +83,4 @@ interface RunningServer {
|
|
|
72
83
|
}
|
|
73
84
|
//#endregion
|
|
74
85
|
export { StartServerOptions as i, ElectronWindowOptions as n, RunningServer as r, ElectronOptions as t };
|
|
75
|
-
//# sourceMappingURL=types-
|
|
86
|
+
//# sourceMappingURL=types-BhDc3oQ0.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-BhDc3oQ0.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;UAMiB;EAChB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;UAGgB;;;;;;;;;;;EAWhB;;;;;;EAMA,SAAS;;;;;;;;EAQT;;;;;EAKA;;;;;;;;;;EAUA;;;;;EAKA;;UAGgB;EAChB;EACA;;UAGgB;;EAEhB;EACA;EACA;;EAEA,aAAa"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@torpor/adapter-electron",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "A @torpor/build adapter for building desktop apps with Electron",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -23,8 +23,8 @@
|
|
|
23
23
|
"author": "",
|
|
24
24
|
"license": "ISC",
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@torpor/adapter-node": "^1.1.
|
|
27
|
-
"@torpor/build": "^1.8.
|
|
26
|
+
"@torpor/adapter-node": "^1.1.9",
|
|
27
|
+
"@torpor/build": "^1.8.3",
|
|
28
28
|
"vite": "npm:@voidzero-dev/vite-plus-core@1.2.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types-Ce9F9DiP.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;;;;;;UAMiB;EAChB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;UAGgB;;;;;;EAMhB,SAAS;;;;;;;;EAQT;;;;;EAKA;;;;;;;;;;EAUA;;;;;EAKA;;UAGgB;EAChB;EACA;;UAGgB;;EAEhB;EACA;EACA;;EAEA,aAAa"}
|