@witchcraft/nuxt-electron 0.3.0 → 0.4.0

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.
Files changed (71) hide show
  1. package/README.md +7 -4
  2. package/dist/module.json +1 -1
  3. package/dist/module.mjs +35 -28
  4. package/dist/runtime/electron/index.d.ts +3 -20
  5. package/dist/runtime/electron/index.js +0 -17
  6. package/dist/runtime/electron/{createProxiedProtocolHandler.d.ts → main/createProxiedProtocolHandler.d.ts} +8 -2
  7. package/dist/runtime/electron/{createProxiedProtocolHandler.js → main/createProxiedProtocolHandler.js} +6 -4
  8. package/dist/runtime/electron/{createWindowControlsApiHandler.js → main/createWindowControlsApiHandler.js} +1 -1
  9. package/dist/runtime/electron/{getPaths.js → main/getPaths.js} +1 -1
  10. package/dist/runtime/electron/{handleApi.d.ts → main/handleApi.d.ts} +1 -1
  11. package/dist/runtime/electron/main/index.d.ts +11 -0
  12. package/dist/runtime/electron/main/index.js +11 -0
  13. package/dist/runtime/electron/{useNuxtRuntimeConfig.js → main/useNuxtRuntimeConfig.js} +1 -1
  14. package/dist/runtime/electron/{createApi.d.ts → preload/createApi.d.ts} +4 -3
  15. package/dist/runtime/electron/preload/index.d.ts +3 -0
  16. package/dist/runtime/electron/preload/index.js +3 -0
  17. package/dist/runtime/electron/{createWindowControlsApi.d.ts → renderer/createWindowControlsApi.d.ts} +1 -1
  18. package/dist/runtime/electron/renderer/index.d.ts +3 -0
  19. package/dist/runtime/electron/renderer/index.js +3 -0
  20. package/dist/runtime/electron/{promisifyApi.d.ts → renderer/promisifyApi.d.ts} +1 -1
  21. package/package.json +23 -7
  22. package/src/module.ts +37 -30
  23. package/src/runtime/electron/index.ts +3 -21
  24. package/src/runtime/electron/{createProxiedProtocolHandler.ts → main/createProxiedProtocolHandler.ts} +17 -5
  25. package/src/runtime/electron/{createWindowControlsApiHandler.ts → main/createWindowControlsApiHandler.ts} +3 -2
  26. package/src/runtime/electron/{getPaths.ts → main/getPaths.ts} +1 -1
  27. package/src/runtime/electron/{handleApi.ts → main/handleApi.ts} +1 -1
  28. package/src/runtime/electron/main/index.ts +11 -0
  29. package/src/runtime/electron/{useNuxtRuntimeConfig.ts → main/useNuxtRuntimeConfig.ts} +1 -1
  30. package/src/runtime/electron/{createApi.ts → preload/createApi.ts} +4 -3
  31. package/src/runtime/electron/preload/index.ts +3 -0
  32. package/src/runtime/electron/{createWindowControlsApi.ts → renderer/createWindowControlsApi.ts} +2 -1
  33. package/src/runtime/electron/renderer/index.ts +3 -0
  34. package/src/runtime/electron/{promisifyApi.ts → renderer/promisifyApi.ts} +1 -1
  35. package/dist/runtime/electron/createPriviledgedProtocolScheme.d.ts +0 -25
  36. package/dist/runtime/electron/createPriviledgedProtocolScheme.js +0 -16
  37. package/src/runtime/electron/createPriviledgedProtocolScheme.ts +0 -40
  38. /package/dist/runtime/electron/{createBroadcaster.d.ts → main/createBroadcaster.d.ts} +0 -0
  39. /package/dist/runtime/electron/{createBroadcaster.js → main/createBroadcaster.js} +0 -0
  40. /package/dist/runtime/electron/{createPrivilegedProtocolScheme.d.ts → main/createPrivilegedProtocolScheme.d.ts} +0 -0
  41. /package/dist/runtime/electron/{createPrivilegedProtocolScheme.js → main/createPrivilegedProtocolScheme.js} +0 -0
  42. /package/dist/runtime/electron/{createWindowControlsApiHandler.d.ts → main/createWindowControlsApiHandler.d.ts} +0 -0
  43. /package/dist/runtime/electron/{getEventWindow.d.ts → main/getEventWindow.d.ts} +0 -0
  44. /package/dist/runtime/electron/{getEventWindow.js → main/getEventWindow.js} +0 -0
  45. /package/dist/runtime/electron/{getPaths.d.ts → main/getPaths.d.ts} +0 -0
  46. /package/dist/runtime/electron/{handleApi.js → main/handleApi.js} +0 -0
  47. /package/dist/runtime/electron/{promisifyReply.d.ts → main/promisifyReply.d.ts} +0 -0
  48. /package/dist/runtime/electron/{promisifyReply.js → main/promisifyReply.js} +0 -0
  49. /package/dist/runtime/electron/{registerDevtoolsShortcuts.d.ts → main/registerDevtoolsShortcuts.d.ts} +0 -0
  50. /package/dist/runtime/electron/{registerDevtoolsShortcuts.js → main/registerDevtoolsShortcuts.js} +0 -0
  51. /package/dist/runtime/electron/{useDevDataDir.d.ts → main/useDevDataDir.d.ts} +0 -0
  52. /package/dist/runtime/electron/{useDevDataDir.js → main/useDevDataDir.js} +0 -0
  53. /package/dist/runtime/electron/{useNuxtRuntimeConfig.d.ts → main/useNuxtRuntimeConfig.d.ts} +0 -0
  54. /package/dist/runtime/electron/{createApi.js → preload/createApi.js} +0 -0
  55. /package/dist/runtime/electron/{createBroadcastHandlers.d.ts → preload/createBroadcastHandlers.d.ts} +0 -0
  56. /package/dist/runtime/electron/{createBroadcastHandlers.js → preload/createBroadcastHandlers.js} +0 -0
  57. /package/dist/runtime/electron/{getPreloadMeta.d.ts → preload/getPreloadMeta.d.ts} +0 -0
  58. /package/dist/runtime/electron/{getPreloadMeta.js → preload/getPreloadMeta.js} +0 -0
  59. /package/dist/runtime/electron/{apiBuilder.d.ts → renderer/apiBuilder.d.ts} +0 -0
  60. /package/dist/runtime/electron/{apiBuilder.js → renderer/apiBuilder.js} +0 -0
  61. /package/dist/runtime/electron/{createWindowControlsApi.js → renderer/createWindowControlsApi.js} +0 -0
  62. /package/dist/runtime/electron/{promisifyApi.js → renderer/promisifyApi.js} +0 -0
  63. /package/src/runtime/electron/{createBroadcaster.ts → main/createBroadcaster.ts} +0 -0
  64. /package/src/runtime/electron/{createPrivilegedProtocolScheme.ts → main/createPrivilegedProtocolScheme.ts} +0 -0
  65. /package/src/runtime/electron/{getEventWindow.ts → main/getEventWindow.ts} +0 -0
  66. /package/src/runtime/electron/{promisifyReply.ts → main/promisifyReply.ts} +0 -0
  67. /package/src/runtime/electron/{registerDevtoolsShortcuts.ts → main/registerDevtoolsShortcuts.ts} +0 -0
  68. /package/src/runtime/electron/{useDevDataDir.ts → main/useDevDataDir.ts} +0 -0
  69. /package/src/runtime/electron/{createBroadcastHandlers.ts → preload/createBroadcastHandlers.ts} +0 -0
  70. /package/src/runtime/electron/{getPreloadMeta.ts → preload/getPreloadMeta.ts} +0 -0
  71. /package/src/runtime/electron/{apiBuilder.ts → renderer/apiBuilder.ts} +0 -0
package/README.md CHANGED
@@ -147,7 +147,8 @@ Add the following to the package.json:
147
147
  "dev:electron": "AUTO_OPEN=electron pnpm dev",
148
148
  "launch:electron": "electron .",
149
149
  "launch:electron:dev": "LOG_LEVEL=trace OVERRIDE_PUBLIC_SERVER_URL=http://localhost:3000 electron .",
150
- "build:electron": "BUILD_ELECTRON=true pnpm build",
150
+ // Use a custom env like "PRODUCTION_BUILD" to control the baking of env variables in the nuxt.config
151
+ "build:electron": "PRODUCTION_BUILD=true BUILD_ELECTRON=true pnpm build",
151
152
  "build:electron:pack": "APP_VERSION=0.0.0 electron-builder --config electron-builder-config.js",
152
153
  "build:electron:no-pack": "APP_VERSION=0.0.0 SKIP_ELECTRON_PACK=true BUILD_ELECTRON=true nuxi build",
153
154
  // write a dev desktop file for linux, see below
@@ -231,7 +232,9 @@ export default defineNuxtConfig({
231
232
  // note the quotes for strings! this is a literal replacement that happens
232
233
  // you also cannot access process.env dynamically if you want this to work (e.g. process.env[name])
233
234
  ? `"https://yoursite.com"`
234
- : `undefined`
235
+ : `undefined`,
236
+ // This bakes "process.env.BAKED_IS_PRODUCTION" for use where runtime overriding should not be allowed
237
+ bakedIsProduction: process.env.PRODUCTION_BUILD === "true" ? `"true"` : `"false"`
235
238
  },
236
239
  // the module will set this to prerender: true
237
240
  // you can override it in routeRules and create a spa without prerender if you need
@@ -247,10 +250,10 @@ Proxying server only page routes seems possible but complicated because each rou
247
250
 
248
251
  ### Runtime Config
249
252
 
250
- Anywhere in electron's renderer files you can also now use nuxt's public runtime config (only the public, you don't want to expose your server secrets to the electron app at all).
253
+ Anywhere in electron's main files you can also now use nuxt's public runtime config using the special version from the module (only the public part, you don't want to expose your server secrets to the electron app at all). Module does some magic to strinfigy it into the `ELECTRON_RUNTIME_CONFIG` env variable and read it back out:
251
254
 
252
255
  ```ts
253
- import { useNuxtRuntimeConfig } from "@witchcraft/nuxt-electron/electron"
256
+ import { useNuxtRuntimeConfig } from "@witchcraft/nuxt-electron/electron/main"
254
257
 
255
258
  const publicRuntimeConfig = useNuxtRuntimeConfig()
256
259
  ```
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "electron",
3
3
  "configKey": "electron",
4
- "version": "0.3.0",
4
+ "version": "0.4.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.3",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -256,35 +256,42 @@ const module$1 = defineNuxtModule({
256
256
  }
257
257
  }
258
258
  }] : []
259
- ].map((entry) => ({
260
- vite: {
261
- mode: process.env.NODE_ENV,
262
- ...electronViteOptions,
263
- build: {
264
- watch: useWatch ? {
265
- include: [`${srcDir}/**/*`]
266
- } : null,
267
- lib: {
268
- entry: entry.entry,
269
- formats: entry.entry.includes("preload") ? ["cjs"] : ["es"],
270
- fileName: () => entry.entry.includes("preload") ? "[name].cjs" : "[name].mjs"
271
- },
272
- ...electronViteOptions.build
273
- },
274
- plugins: [
275
- autoOpen ? {
276
- name: "plugin-start-electron",
277
- async closeBundle() {
278
- void entry.onStart?.();
259
+ ].map((entry) => {
260
+ const isPreload = entry.entry.includes("preload");
261
+ return {
262
+ vite: {
263
+ mode: process.env.NODE_ENV,
264
+ ...electronViteOptions,
265
+ build: {
266
+ watch: useWatch ? {
267
+ include: [`${srcDir}/**/*`]
268
+ } : null,
269
+ lib: {
270
+ entry: entry.entry,
271
+ formats: isPreload ? ["cjs"] : ["es"],
272
+ fileName: () => isPreload ? "[name].cjs" : "[name].mjs"
273
+ },
274
+ ...electronViteOptions.build,
275
+ rollupOptions: {
276
+ ...electronViteOptions.build?.rollupOptions,
277
+ ...isPreload ? { external: [] } : {}
279
278
  }
280
- } : void 0,
281
- // not bundle breaks preload because it tries to required from node_modules
282
- // and sandboxed windows can't do that
283
- ...!isElectronBuild && !entry.entry.includes("preload") ? [notBundle(options.notBundleOptions)] : [],
284
- ...electronViteOptions.plugins ?? []
285
- ]
286
- }
287
- }));
279
+ },
280
+ plugins: [
281
+ autoOpen ? {
282
+ name: "plugin-start-electron",
283
+ async closeBundle() {
284
+ void entry.onStart?.();
285
+ }
286
+ } : void 0,
287
+ // not bundle breaks preload because it tries to required from node_modules
288
+ // and sandboxed windows can't do that
289
+ ...!isElectronBuild && !isPreload ? [notBundle(options.notBundleOptions)] : [],
290
+ ...electronViteOptions.plugins ?? []
291
+ ]
292
+ }
293
+ };
294
+ });
288
295
  logger.debug(builds);
289
296
  const devUrl = await viteServerUrl;
290
297
  if (devUrl) {
@@ -1,20 +1,3 @@
1
- export * from "./types";
2
- export { STATIC } from "./static";
3
- export { apiBuilder } from "./apiBuilder";
4
- export { createApi } from "./createApi";
5
- export { createBroadcastHandlers } from "./createBroadcastHandlers";
6
- export { createBroadcaster } from "./createBroadcaster";
7
- export { createPrivilegedProtocolScheme } from "./createPrivilegedProtocolScheme";
8
- export { createProxiedProtocolHandler } from "./createProxiedProtocolHandler";
9
- export { createWindowControlsApi } from "./createWindowControlsApi";
10
- export { createWindowControlsApiHandler } from "./createWindowControlsApiHandler";
11
- export { getEventWindow } from "./getEventWindow";
12
- export { getPaths } from "./getPaths";
13
- export { getPreloadMeta } from "./getPreloadMeta";
14
- export { handleApi } from "./handleApi";
15
- export { mergeApi } from "./mergeApi";
16
- export { promisifyApi } from "./promisifyApi";
17
- export { promisifyReply } from "./promisifyReply";
18
- export { registerDevtoolsShortcuts } from "./registerDevtoolsShortcuts";
19
- export { useDevDataDir } from "./useDevDataDir";
20
- export { useNuxtRuntimeConfig } from "./useNuxtRuntimeConfig";
1
+ export * from "./types.js";
2
+ export { STATIC } from "./static.js";
3
+ export { mergeApi } from "./mergeApi.js";
@@ -1,20 +1,3 @@
1
1
  export * from "./types.js";
2
2
  export { STATIC } from "./static.js";
3
- export { apiBuilder } from "./apiBuilder.js";
4
- export { createApi } from "./createApi.js";
5
- export { createBroadcastHandlers } from "./createBroadcastHandlers.js";
6
- export { createBroadcaster } from "./createBroadcaster.js";
7
- export { createPrivilegedProtocolScheme } from "./createPrivilegedProtocolScheme.js";
8
- export { createProxiedProtocolHandler } from "./createProxiedProtocolHandler.js";
9
- export { createWindowControlsApi } from "./createWindowControlsApi.js";
10
- export { createWindowControlsApiHandler } from "./createWindowControlsApiHandler.js";
11
- export { getEventWindow } from "./getEventWindow.js";
12
- export { getPaths } from "./getPaths.js";
13
- export { getPreloadMeta } from "./getPreloadMeta.js";
14
- export { handleApi } from "./handleApi.js";
15
3
  export { mergeApi } from "./mergeApi.js";
16
- export { promisifyApi } from "./promisifyApi.js";
17
- export { promisifyReply } from "./promisifyReply.js";
18
- export { registerDevtoolsShortcuts } from "./registerDevtoolsShortcuts.js";
19
- export { useDevDataDir } from "./useDevDataDir.js";
20
- export { useNuxtRuntimeConfig } from "./useNuxtRuntimeConfig.js";
@@ -1,4 +1,4 @@
1
- import type { Protocol } from "electron";
1
+ import type { Protocol, Session } from "electron";
2
2
  /**
3
3
  * Stores file paths and proxy route URLs with an expiration time.
4
4
  * Expired entries are lazily deleted on read and periodically cleaned on write.
@@ -50,7 +50,7 @@ routeProxies: Record<string, string | {
50
50
  url: never;
51
51
  convertPath: never;
52
52
  ignore: true;
53
- }>, { logger, errorPage }?: {
53
+ }>, { logger, errorPage, session }?: {
54
54
  /**
55
55
  * Optional logger. It's suggested you not pass this unless you're traceging in dev mode as a lot of requests can be made.
56
56
  */
@@ -64,5 +64,11 @@ routeProxies: Record<string, string | {
64
64
  * The module forces nuxt to generate it as it's not rendered in build mode. This is only used for 404 errors. The 400 error for bad requests still just returns Bad Request for now. This might become more flexible in the future.
65
65
  */
66
66
  errorPage?: string;
67
+ /**
68
+ * The window session the requests should be made from, i.e. the same session whose `protocol` you passed first.
69
+ *
70
+ * `net.fetch` always uses the default session, so without this, cookies, cache and proxy settings of a partitioned window session are bypassed, and requests for a partitioned window go out with an empty cookie jar.
71
+ */
72
+ session?: Session;
67
73
  }): void;
68
74
  export {};
@@ -67,12 +67,14 @@ async function getFromCacheOr(cache2, key, fn, logger) {
67
67
  }
68
68
  export function createProxiedProtocolHandler(protocol, protocolName = "app", basePath, routeProxies, {
69
69
  logger,
70
- errorPage = "404.html"
70
+ errorPage = "404.html",
71
+ session
71
72
  } = {}) {
72
73
  const routeProxyKeys = keys(routeProxies);
73
74
  if (protocol.isProtocolHandled(protocolName)) {
74
75
  throw new Error(`Protocol ${protocolName} is already handled.`);
75
76
  }
77
+ const netFetch = session ? (input, init) => session.fetch(input, init) : net.fetch;
76
78
  let errorPage404;
77
79
  protocol.handle(protocolName, async (request) => {
78
80
  errorPage404 ??= await getFromCacheOr(cache, "404.html", (key) => getPathToServe(path.join(basePath, errorPage), "404.html", key, logger), logger);
@@ -111,7 +113,7 @@ export function createProxiedProtocolHandler(protocol, protocolName = "app", bas
111
113
  finalPath: cachedProxyResult
112
114
  });
113
115
  }
114
- return net.fetch(cachedProxyResult, {
116
+ return netFetch(cachedProxyResult, {
115
117
  ...pick(request, proxiedProperties)
116
118
  });
117
119
  }
@@ -165,7 +167,7 @@ export function createProxiedProtocolHandler(protocol, protocolName = "app", bas
165
167
  errorPath: pathToFileURL(errorPage404).toString()
166
168
  });
167
169
  }
168
- const res = await net.fetch(pathToFileURL(errorPage404).toString(), {
170
+ const res = await netFetch(pathToFileURL(errorPage404).toString(), {
169
171
  // see below
170
172
  bypassCustomProtocolHandlers: true
171
173
  }).catch((err) => err);
@@ -181,7 +183,7 @@ export function createProxiedProtocolHandler(protocol, protocolName = "app", bas
181
183
  originalPath
182
184
  });
183
185
  }
184
- const response = await net.fetch(finalPath, {
186
+ const response = await netFetch(finalPath, {
185
187
  // avoid infinite loop if protocolName protocol is "file"
186
188
  // or file is otherwise handled
187
189
  bypassCustomProtocolHandlers: true
@@ -1,5 +1,5 @@
1
- import { windowControlsMessageKey } from "./createWindowControlsApi.js";
2
1
  import { promisifyReply } from "./promisifyReply.js";
2
+ import { windowControlsMessageKey } from "../renderer/createWindowControlsApi.js";
3
3
  export function createWindowControlsApiHandler(cb) {
4
4
  promisifyReply(windowControlsMessageKey, async (win, action) => {
5
5
  switch (action) {
@@ -1,7 +1,7 @@
1
1
  import { unreachable } from "@alanscodelog/utils/unreachable";
2
2
  import path from "node:path";
3
3
  import { fileURLToPath } from "node:url";
4
- import { STATIC } from "./static.js";
4
+ import { STATIC } from "../static.js";
5
5
  export function getPaths(protocolName = "app", overridingEnvs = {
6
6
  windowUrl: void 0,
7
7
  publicServerUrl: void 0
@@ -1,4 +1,4 @@
1
- import type { ElectronIpcMessages } from "./types.js";
1
+ import type { ElectronIpcMessages } from "../types.js";
2
2
  /**
3
3
  * Type safe wrapper around ipcMain.handle. See {@link createApi} for more info.
4
4
  */
@@ -0,0 +1,11 @@
1
+ export { createBroadcaster } from "./createBroadcaster.js";
2
+ export { createPrivilegedProtocolScheme } from "./createPrivilegedProtocolScheme.js";
3
+ export { createProxiedProtocolHandler } from "./createProxiedProtocolHandler.js";
4
+ export { createWindowControlsApiHandler } from "./createWindowControlsApiHandler.js";
5
+ export { getEventWindow } from "./getEventWindow.js";
6
+ export { getPaths } from "./getPaths.js";
7
+ export { handleApi } from "./handleApi.js";
8
+ export { promisifyReply } from "./promisifyReply.js";
9
+ export { registerDevtoolsShortcuts } from "./registerDevtoolsShortcuts.js";
10
+ export { useDevDataDir } from "./useDevDataDir.js";
11
+ export { useNuxtRuntimeConfig } from "./useNuxtRuntimeConfig.js";
@@ -0,0 +1,11 @@
1
+ export { createBroadcaster } from "./createBroadcaster.js";
2
+ export { createPrivilegedProtocolScheme } from "./createPrivilegedProtocolScheme.js";
3
+ export { createProxiedProtocolHandler } from "./createProxiedProtocolHandler.js";
4
+ export { createWindowControlsApiHandler } from "./createWindowControlsApiHandler.js";
5
+ export { getEventWindow } from "./getEventWindow.js";
6
+ export { getPaths } from "./getPaths.js";
7
+ export { handleApi } from "./handleApi.js";
8
+ export { promisifyReply } from "./promisifyReply.js";
9
+ export { registerDevtoolsShortcuts } from "./registerDevtoolsShortcuts.js";
10
+ export { useDevDataDir } from "./useDevDataDir.js";
11
+ export { useNuxtRuntimeConfig } from "./useNuxtRuntimeConfig.js";
@@ -1,4 +1,4 @@
1
- import { STATIC } from "./static.js";
1
+ import { STATIC } from "../static.js";
2
2
  export function useNuxtRuntimeConfig() {
3
3
  return STATIC.ELECTRON_RUNTIME_CONFIG;
4
4
  }
@@ -1,11 +1,12 @@
1
- import type { ElectronIpcMessages } from "./types.js";
1
+ import type { ElectronIpcMessages } from "../types.js";
2
2
  /**
3
3
  * Type safe wrapper around ipcRenderer.invoke which also creates the api function at the proper path.
4
4
  *
5
5
  * This makes every single part of the api type safe while avoiding repetition.
6
6
  *
7
7
  * ```ts [preload.ts]
8
- * import { mergeApi, createApi } from "@witchcraft/nuxt-electron/electron"
8
+ * import { mergeApi } from "@witchcraft/nuxt-electron/electron"
9
+ * import { createApi } from "@witchcraft/nuxt-electron/electron/preload"
9
10
  *
10
11
  * declare module "@witchcraft/nuxt-electron" {
11
12
  * interface Register {
@@ -41,7 +42,7 @@ import type { ElectronIpcMessages } from "./types.js";
41
42
  * ```
42
43
  *
43
44
  * ```ts [main.ts]
44
- * import { createApi } from "@witchcraft/nuxt-electron/electron"
45
+ * import { createApi } from "@witchcraft/nuxt-electron/electron/preload"
45
46
  * ipcHandle("my.test.method", (event, arg1, arg2) => { ... })
46
47
  * ipcHandle("avoidConflict.my.test.method", (event, arg1, arg2) => { ... })
47
48
  * ```
@@ -0,0 +1,3 @@
1
+ export { createApi } from "./createApi.js";
2
+ export { createBroadcastHandlers } from "./createBroadcastHandlers.js";
3
+ export { getPreloadMeta } from "./getPreloadMeta.js";
@@ -0,0 +1,3 @@
1
+ export { createApi } from "./createApi.js";
2
+ export { createBroadcastHandlers } from "./createBroadcastHandlers.js";
3
+ export { getPreloadMeta } from "./getPreloadMeta.js";
@@ -1,5 +1,5 @@
1
1
  import type { IpcRenderer } from "electron";
2
- import type { WindowControlsApi } from "./types.js";
2
+ import type { WindowControlsApi } from "../types.js";
3
3
  /**
4
4
  * Creates the apis to control the window from the preload script / client.
5
5
  *
@@ -0,0 +1,3 @@
1
+ export { apiBuilder } from "./apiBuilder.js";
2
+ export { createWindowControlsApi } from "./createWindowControlsApi.js";
3
+ export { promisifyApi } from "./promisifyApi.js";
@@ -0,0 +1,3 @@
1
+ export { apiBuilder } from "./apiBuilder.js";
2
+ export { createWindowControlsApi } from "./createWindowControlsApi.js";
3
+ export { promisifyApi } from "./promisifyApi.js";
@@ -18,7 +18,7 @@ import type { IpcRenderer } from "electron";
18
18
  * ```
19
19
  *
20
20
  * ```ts [main.ts]
21
- * import { promisifyReply } from "@witchcraft/nuxt-electron/electron"
21
+ * import { promisifyReply } from "@witchcraft/nuxt-electron/electron/main"
22
22
  * promisifyReply<
23
23
  * ElectronApi["api"]["someApi"]
24
24
  * >(ipcRenderer, MESSAGE.UNIQUE_KEY, (win, apiParam, apiParam2) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@witchcraft/nuxt-electron",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "Nuxt module for working with electron.",
5
5
  "repository": "https://github.com/witchcraftjs/nuxt-electron",
6
6
  "license": "MIT",
@@ -15,6 +15,22 @@
15
15
  "types": "./dist/runtime/electron/index.d.ts",
16
16
  "import": "./dist/runtime/electron/index.js"
17
17
  },
18
+ "./electron/main": {
19
+ "types": "./dist/runtime/electron/main/index.d.ts",
20
+ "import": "./dist/runtime/electron/main/index.js"
21
+ },
22
+ "./electron/preload": {
23
+ "types": "./dist/runtime/electron/preload/index.d.ts",
24
+ "import": "./dist/runtime/electron/preload/index.js"
25
+ },
26
+ "./electron/renderer": {
27
+ "types": "./dist/runtime/electron/renderer/index.d.ts",
28
+ "import": "./dist/runtime/electron/renderer/index.js"
29
+ },
30
+ "./electron/*": {
31
+ "types": "./dist/runtime/electron/*.d.ts",
32
+ "import": "./dist/runtime/electron/*.js"
33
+ },
18
34
  "./genDevDesktop": {
19
35
  "import": "./genDevDesktop.js"
20
36
  }
@@ -27,15 +43,15 @@
27
43
  "dependencies": {
28
44
  "@alanscodelog/utils": "^6.3.0",
29
45
  "@nuxt/kit": "^4.3.1",
30
- "@witchcraft/nuxt-utils": "^0.3.10",
31
- "@witchcraft/ui": "^0.5.0",
46
+ "@witchcraft/nuxt-utils": "^0.3.17",
47
+ "@witchcraft/ui": "^0.5.12",
32
48
  "defu": "^6.1.4",
33
49
  "electron": "^37.10.3",
34
50
  "es-toolkit": "^1.44.0",
35
51
  "vite-plugin-electron": "^0.29.0"
36
52
  },
37
53
  "peerDependencies": {
38
- "@witchcraft/ui": "^0.5.0",
54
+ "@witchcraft/ui": "^0.5.12",
39
55
  "unplugin-icons": "^22.5.0"
40
56
  },
41
57
  "peerDependenciesMeta": {
@@ -47,9 +63,9 @@
47
63
  }
48
64
  },
49
65
  "devDependencies": {
50
- "@alanscodelog/eslint-config": "^6.3.1",
51
- "@alanscodelog/semantic-release-config": "^6.0.2",
52
- "@alanscodelog/tsconfigs": "^6.3.0",
66
+ "@alanscodelog/eslint-config": "^6.3.2",
67
+ "@alanscodelog/semantic-release-config": "^6.0.3",
68
+ "@alanscodelog/tsconfigs": "^6.3.1",
53
69
  "@nuxt/eslint-config": "^1.15.1",
54
70
  "@nuxt/module-builder": "^1.0.3",
55
71
  "@nuxt/schema": "^4.3.1",
package/src/module.ts CHANGED
@@ -427,39 +427,46 @@ export default defineNuxtModule<ModuleOptions>({
427
427
  }]
428
428
  : []
429
429
  )
430
- ].map(entry => ({
431
- vite: {
432
- mode: process.env.NODE_ENV,
433
- ...electronViteOptions,
434
- build: {
435
- watch: useWatch
436
- ? {
437
- include: [`${srcDir}/**/*`]
438
- }
439
- : null,
440
- lib: {
441
- entry: entry.entry,
442
- formats: entry.entry.includes("preload") ? ["cjs"] : ["es"],
443
- fileName: () => entry.entry.includes("preload") ? "[name].cjs" : "[name].mjs"
430
+ ].map(entry => {
431
+ const isPreload = entry.entry.includes("preload")
432
+ return {
433
+ vite: {
434
+ mode: process.env.NODE_ENV,
435
+ ...electronViteOptions,
436
+ build: {
437
+ watch: useWatch
438
+ ? {
439
+ include: [`${srcDir}/**/*`]
440
+ }
441
+ : null,
442
+ lib: {
443
+ entry: entry.entry,
444
+ formats: isPreload ? ["cjs"] : ["es"],
445
+ fileName: () => isPreload ? "[name].cjs" : "[name].mjs"
446
+ },
447
+ ...electronViteOptions.build,
448
+ rollupOptions: {
449
+ ...electronViteOptions.build?.rollupOptions,
450
+ ...(isPreload ? { external: [] } : {})
451
+ }
444
452
  },
445
- ...electronViteOptions.build
446
- },
447
- plugins: [
448
- autoOpen
449
- ? {
450
- name: "plugin-start-electron",
451
- async closeBundle() {
452
- void entry.onStart?.()
453
+ plugins: [
454
+ autoOpen
455
+ ? {
456
+ name: "plugin-start-electron",
457
+ async closeBundle() {
458
+ void entry.onStart?.()
459
+ }
453
460
  }
454
- }
455
- : undefined,
456
- // not bundle breaks preload because it tries to required from node_modules
457
- // and sandboxed windows can't do that
458
- ...(!isElectronBuild && !entry.entry.includes("preload") ? [notBundle(options.notBundleOptions)] : []),
459
- ...(electronViteOptions.plugins ?? [])
460
- ]
461
+ : undefined,
462
+ // not bundle breaks preload because it tries to required from node_modules
463
+ // and sandboxed windows can't do that
464
+ ...(!isElectronBuild && !isPreload ? [notBundle(options.notBundleOptions)] : []),
465
+ ...(electronViteOptions.plugins ?? [])
466
+ ]
467
+ }
461
468
  }
462
- }))
469
+ })
463
470
 
464
471
  logger.debug(builds)
465
472
 
@@ -1,21 +1,3 @@
1
- export * from "./types"
2
- // note adding file endings breaks build types
3
- export { STATIC } from "./static"
4
- export { apiBuilder } from "./apiBuilder"
5
- export { createApi } from "./createApi"
6
- export { createBroadcastHandlers } from "./createBroadcastHandlers"
7
- export { createBroadcaster } from "./createBroadcaster"
8
- export { createPrivilegedProtocolScheme } from "./createPrivilegedProtocolScheme"
9
- export { createProxiedProtocolHandler } from "./createProxiedProtocolHandler"
10
- export { createWindowControlsApi } from "./createWindowControlsApi"
11
- export { createWindowControlsApiHandler } from "./createWindowControlsApiHandler"
12
- export { getEventWindow } from "./getEventWindow"
13
- export { getPaths } from "./getPaths"
14
- export { getPreloadMeta } from "./getPreloadMeta"
15
- export { handleApi } from "./handleApi"
16
- export { mergeApi } from "./mergeApi"
17
- export { promisifyApi } from "./promisifyApi"
18
- export { promisifyReply } from "./promisifyReply"
19
- export { registerDevtoolsShortcuts } from "./registerDevtoolsShortcuts"
20
- export { useDevDataDir } from "./useDevDataDir"
21
- export { useNuxtRuntimeConfig } from "./useNuxtRuntimeConfig"
1
+ export * from "./types.js"
2
+ export { STATIC } from "./static.js"
3
+ export { mergeApi } from "./mergeApi.js"
@@ -1,6 +1,6 @@
1
1
  import { keys } from "@alanscodelog/utils/keys"
2
2
  import { pick } from "@alanscodelog/utils/pick"
3
- import type { Protocol } from "electron"
3
+ import type { Protocol, Session } from "electron"
4
4
  import { net } from "electron"
5
5
  import fs from "node:fs/promises"
6
6
  import path from "node:path"
@@ -146,7 +146,8 @@ export function createProxiedProtocolHandler(
146
146
  >,
147
147
  {
148
148
  logger,
149
- errorPage = "404.html"
149
+ errorPage = "404.html",
150
+ session
150
151
  }: {
151
152
  /**
152
153
  * Optional logger. It's suggested you not pass this unless you're traceging in dev mode as a lot of requests can be made.
@@ -163,12 +164,23 @@ export function createProxiedProtocolHandler(
163
164
  */
164
165
  errorPage?: string
165
166
 
167
+ /**
168
+ * The window session the requests should be made from, i.e. the same session whose `protocol` you passed first.
169
+ *
170
+ * `net.fetch` always uses the default session, so without this, cookies, cache and proxy settings of a partitioned window session are bypassed, and requests for a partitioned window go out with an empty cookie jar.
171
+ */
172
+ session?: Session
173
+
166
174
  } = {}
167
175
  ): void {
168
176
  const routeProxyKeys = keys(routeProxies)
169
177
  if (protocol.isProtocolHandled(protocolName)) {
170
178
  throw new Error(`Protocol ${protocolName} is already handled.`)
171
179
  }
180
+ // net.fetch is hardcoded to the default session, a partitioned window needs its own
181
+ const netFetch: typeof net.fetch = session
182
+ ? (input, init) => session.fetch(input as Parameters<Session["fetch"]>[0], init)
183
+ : net.fetch
172
184
 
173
185
  let errorPage404: string | undefined
174
186
  // note that while it would be nice to do protocol.isProtocolRegistered
@@ -222,7 +234,7 @@ export function createProxiedProtocolHandler(
222
234
  finalPath: cachedProxyResult
223
235
  })
224
236
  }
225
- return net.fetch(cachedProxyResult, {
237
+ return netFetch(cachedProxyResult, {
226
238
  ...pick(request, proxiedProperties)
227
239
  })
228
240
  }
@@ -276,7 +288,7 @@ export function createProxiedProtocolHandler(
276
288
  errorPath: pathToFileURL(errorPage404).toString()
277
289
  })
278
290
  }
279
- const res = await net.fetch(pathToFileURL(errorPage404).toString(), {
291
+ const res = await netFetch(pathToFileURL(errorPage404).toString(), {
280
292
  // see below
281
293
  bypassCustomProtocolHandlers: true
282
294
  })
@@ -295,7 +307,7 @@ export function createProxiedProtocolHandler(
295
307
  })
296
308
  }
297
309
 
298
- const response = await net.fetch(finalPath, {
310
+ const response = await netFetch(finalPath, {
299
311
  // avoid infinite loop if protocolName protocol is "file"
300
312
  // or file is otherwise handled
301
313
  bypassCustomProtocolHandlers: true
@@ -1,8 +1,9 @@
1
1
  import type { BrowserWindow } from "electron"
2
2
 
3
- import { windowControlsMessageKey } from "./createWindowControlsApi.js"
4
3
  import { promisifyReply } from "./promisifyReply.js"
5
- import type { WindowControlsApi } from "./types.js"
4
+
5
+ import { windowControlsMessageKey } from "../renderer/createWindowControlsApi.js"
6
+ import type { WindowControlsApi } from "../types.js"
6
7
 
7
8
  export function createWindowControlsApiHandler(
8
9
  /** Mostly for logging. Does not replace the action. */
@@ -2,7 +2,7 @@ import { unreachable } from "@alanscodelog/utils/unreachable"
2
2
  import path from "node:path"
3
3
  import { fileURLToPath } from "node:url"
4
4
 
5
- import { STATIC } from "./static.js"
5
+ import { STATIC } from "../static.js"
6
6
 
7
7
  /**
8
8
  * Calculates the correct paths for the app to run in electron.
@@ -1,6 +1,6 @@
1
1
  import { ipcMain } from "electron"
2
2
 
3
- import type { ElectronIpcMessages } from "./types.js"
3
+ import type { ElectronIpcMessages } from "../types.js"
4
4
 
5
5
  /**
6
6
  * Type safe wrapper around ipcMain.handle. See {@link createApi} for more info.
@@ -0,0 +1,11 @@
1
+ export { createBroadcaster } from "./createBroadcaster.js"
2
+ export { createPrivilegedProtocolScheme } from "./createPrivilegedProtocolScheme.js"
3
+ export { createProxiedProtocolHandler } from "./createProxiedProtocolHandler.js"
4
+ export { createWindowControlsApiHandler } from "./createWindowControlsApiHandler.js"
5
+ export { getEventWindow } from "./getEventWindow.js"
6
+ export { getPaths } from "./getPaths.js"
7
+ export { handleApi } from "./handleApi.js"
8
+ export { promisifyReply } from "./promisifyReply.js"
9
+ export { registerDevtoolsShortcuts } from "./registerDevtoolsShortcuts.js"
10
+ export { useDevDataDir } from "./useDevDataDir.js"
11
+ export { useNuxtRuntimeConfig } from "./useNuxtRuntimeConfig.js"
@@ -1,6 +1,6 @@
1
1
  import type { PublicRuntimeConfig } from "nuxt/schema"
2
2
 
3
- import { STATIC } from "./static.js"
3
+ import { STATIC } from "../static.js"
4
4
 
5
5
  export function useNuxtRuntimeConfig(): PublicRuntimeConfig {
6
6
  return STATIC.ELECTRON_RUNTIME_CONFIG
@@ -1,7 +1,7 @@
1
1
  import { set } from "@alanscodelog/utils/set"
2
2
  import { ipcRenderer } from "electron"
3
3
 
4
- import type { ElectronIpcMessages } from "./types.js"
4
+ import type { ElectronIpcMessages } from "../types.js"
5
5
 
6
6
  /**
7
7
  * Type safe wrapper around ipcRenderer.invoke which also creates the api function at the proper path.
@@ -9,7 +9,8 @@ import type { ElectronIpcMessages } from "./types.js"
9
9
  * This makes every single part of the api type safe while avoiding repetition.
10
10
  *
11
11
  * ```ts [preload.ts]
12
- * import { mergeApi, createApi } from "@witchcraft/nuxt-electron/electron"
12
+ * import { mergeApi } from "@witchcraft/nuxt-electron/electron"
13
+ * import { createApi } from "@witchcraft/nuxt-electron/electron/preload"
13
14
  *
14
15
  * declare module "@witchcraft/nuxt-electron" {
15
16
  * interface Register {
@@ -45,7 +46,7 @@ import type { ElectronIpcMessages } from "./types.js"
45
46
  * ```
46
47
  *
47
48
  * ```ts [main.ts]
48
- * import { createApi } from "@witchcraft/nuxt-electron/electron"
49
+ * import { createApi } from "@witchcraft/nuxt-electron/electron/preload"
49
50
  * ipcHandle("my.test.method", (event, arg1, arg2) => { ... })
50
51
  * ipcHandle("avoidConflict.my.test.method", (event, arg1, arg2) => { ... })
51
52
  * ```
@@ -0,0 +1,3 @@
1
+ export { createApi } from "./createApi.js"
2
+ export { createBroadcastHandlers } from "./createBroadcastHandlers.js"
3
+ export { getPreloadMeta } from "./getPreloadMeta.js"
@@ -1,7 +1,8 @@
1
1
  import type { IpcRenderer } from "electron"
2
2
 
3
3
  import { promisifyApi } from "./promisifyApi.js"
4
- import type { WindowControlsApi } from "./types.js"
4
+
5
+ import type { WindowControlsApi } from "../types.js"
5
6
 
6
7
  const key = "window-control-action"
7
8
  /**
@@ -0,0 +1,3 @@
1
+ export { apiBuilder } from "./apiBuilder.js"
2
+ export { createWindowControlsApi } from "./createWindowControlsApi.js"
3
+ export { promisifyApi } from "./promisifyApi.js"
@@ -24,7 +24,7 @@ const promiseResolveMap = new Map<string, {
24
24
  * ```
25
25
  *
26
26
  * ```ts [main.ts]
27
- * import { promisifyReply } from "@witchcraft/nuxt-electron/electron"
27
+ * import { promisifyReply } from "@witchcraft/nuxt-electron/electron/main"
28
28
  * promisifyReply<
29
29
  * ElectronApi["api"]["someApi"]
30
30
  * >(ipcRenderer, MESSAGE.UNIQUE_KEY, (win, apiParam, apiParam2) => {
@@ -1,25 +0,0 @@
1
- /**
2
- * Creates a protocol scheme to register as privileged with the following permissions (you can override them with the second parameter).
3
- *
4
- * Note this must be called before the app's ready event:
5
- *
6
- * ```ts
7
- * protocol.registerSchemesAsPrivileged([
8
- * createPrivilegedProtocolScheme("app")
9
- * })
10
- * ```
11
- *
12
- * ## Privileges
13
- *
14
- * Basically all except `bypassCSP` and `enableCORS`.
15
- *
16
- * - `standard`
17
- * - `allowServiceWorkers`
18
- * - `bypassCSP`
19
- * - `corsEnabled`
20
- * - `secure`
21
- * - `supportFetchAPI`
22
- * - `codeCache`
23
- * - `stream`
24
- */
25
- export declare function createPrivilegedProtocolScheme(protocolName: string, privileges?: Partial<Electron.CustomScheme["privileges"]>): Electron.CustomScheme;
@@ -1,16 +0,0 @@
1
- export function createPrivilegedProtocolScheme(protocolName, privileges = {}) {
2
- return {
3
- scheme: protocolName,
4
- privileges: {
5
- bypassCSP: false,
6
- corsEnabled: false,
7
- stream: false,
8
- standard: true,
9
- secure: true,
10
- supportFetchAPI: true,
11
- allowServiceWorkers: true,
12
- codeCache: true,
13
- ...privileges
14
- }
15
- };
16
- }
@@ -1,40 +0,0 @@
1
- /**
2
- * Creates a protocol scheme to register as privileged with the following permissions (you can override them with the second parameter).
3
- *
4
- * Note this must be called before the app's ready event:
5
- *
6
- * ```ts
7
- * protocol.registerSchemesAsPrivileged([
8
- * createPrivilegedProtocolScheme("app")
9
- * })
10
- * ```
11
- *
12
- * ## Privileges
13
- *
14
- * Basically all except `bypassCSP` and `enableCORS`.
15
- *
16
- * - `standard`
17
- * - `allowServiceWorkers`
18
- * - `bypassCSP`
19
- * - `corsEnabled`
20
- * - `secure`
21
- * - `supportFetchAPI`
22
- * - `codeCache`
23
- * - `stream`
24
- */
25
- export function createPrivilegedProtocolScheme(protocolName: string, privileges: Partial<Electron.CustomScheme["privileges"]> = {}): Electron.CustomScheme {
26
- return {
27
- scheme: protocolName,
28
- privileges: {
29
- bypassCSP: false,
30
- corsEnabled: false,
31
- stream: false,
32
- standard: true,
33
- secure: true,
34
- supportFetchAPI: true,
35
- allowServiceWorkers: true,
36
- codeCache: true,
37
- ...privileges
38
- }
39
- }
40
- }