@witchcraft/nuxt-electron 0.0.12 → 0.2.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.
- package/README.md +160 -73
- package/dist/module.json +1 -1
- package/dist/module.mjs +33 -13
- package/dist/runtime/electron/createPriviledgedProtocolScheme.d.ts +25 -0
- package/dist/runtime/electron/createPriviledgedProtocolScheme.js +16 -0
- package/dist/runtime/electron/createPrivilegedProtocolScheme.d.ts +25 -0
- package/dist/runtime/electron/createPrivilegedProtocolScheme.js +16 -0
- package/dist/runtime/electron/createProxiedProtocolHandler.d.ts +31 -0
- package/dist/runtime/electron/createProxiedProtocolHandler.js +158 -0
- package/dist/runtime/electron/getPaths.d.ts +7 -15
- package/dist/runtime/electron/getPaths.js +18 -12
- package/dist/runtime/electron/index.d.ts +2 -1
- package/dist/runtime/electron/index.js +2 -1
- package/genDevDesktop.js +50 -3
- package/package.json +8 -6
- package/src/module.ts +42 -16
- package/src/runtime/electron/createPriviledgedProtocolScheme.ts +40 -0
- package/src/runtime/electron/createPrivilegedProtocolScheme.ts +40 -0
- package/src/runtime/electron/createProxiedProtocolHandler.ts +229 -0
- package/src/runtime/electron/getPaths.ts +32 -27
- package/src/runtime/electron/index.ts +2 -1
- package/dist/runtime/electron/createNuxtFileProtocolHandler.d.ts +0 -14
- package/dist/runtime/electron/createNuxtFileProtocolHandler.js +0 -20
- package/src/runtime/electron/createNuxtFileProtocolHandler.ts +0 -42
package/README.md
CHANGED
|
@@ -11,9 +11,10 @@
|
|
|
11
11
|
|
|
12
12
|
- :zap: Auto reloads/restarts electron on changes to the main/renderer code or nuxt server restarts.
|
|
13
13
|
- :rocket: Api calls are proxied to the server.
|
|
14
|
-
- :start:
|
|
14
|
+
- :start: Rendering strategy isn't changed. Does not require static builds or changing baseURL/buildAssetsDir.
|
|
15
15
|
- :scissors: Trims server and non-electron routes from the electron bundle.
|
|
16
16
|
- :open_file_folder: Modifies directory structure for easier multi-platform builds.
|
|
17
|
+
- :snowflake: Nix Support - Playground contains an example flake for reproducible development and builds.
|
|
17
18
|
- :hammer_and_wrench: Helpful Tools/Composables
|
|
18
19
|
- `isElectron`
|
|
19
20
|
- Electron Only
|
|
@@ -29,16 +30,43 @@
|
|
|
29
30
|
|
|
30
31
|
# Playground
|
|
31
32
|
|
|
32
|
-
There is a playground
|
|
33
|
+
There is a playground with a comprehensive example, but it only works locally due to electron.
|
|
34
|
+
|
|
35
|
+
To try it:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
git clone https://github.com/witchcraftjs/nuxt-electron.git
|
|
39
|
+
pnpm i
|
|
40
|
+
cd nuxt-electron/playground
|
|
41
|
+
pnpm build && pnpm build:electron:no-pack
|
|
42
|
+
pnpm preview:electron:dev
|
|
43
|
+
```
|
|
44
|
+
You can also do:
|
|
45
|
+
```bash
|
|
46
|
+
pnpm build
|
|
47
|
+
pnpm build:electron:pack
|
|
48
|
+
# in one tab
|
|
49
|
+
pnpm preview
|
|
50
|
+
# in another tab
|
|
51
|
+
OVERRIDE_PUBLIC_SERVER_URL=http://localhost:3000 ./.dist/electron/release/linux-unpacked/your-app-name
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
If you're using nix the above should work in the dev shell. You can also do `nix run` but warning, this is like just building and doing `pnpm launch:electron` (see below).
|
|
33
55
|
|
|
34
56
|
## Install
|
|
35
57
|
```bash
|
|
36
58
|
pnpx nuxi module add @witchcraft/nuxt-electron
|
|
37
59
|
|
|
38
60
|
```
|
|
39
|
-
|
|
61
|
+
### Components
|
|
62
|
+
|
|
63
|
+
If using any of the provided components, they rely on the @witchcraft/ui module.
|
|
40
64
|
|
|
41
|
-
If
|
|
65
|
+
If not, ignore it.
|
|
66
|
+
|
|
67
|
+
This module installs it to use it's css config and twMerge.
|
|
68
|
+
|
|
69
|
+
All you need to do is add the electron module to your tailwind css as a source:
|
|
42
70
|
```css [assets/css/tailwind.css]
|
|
43
71
|
@source "../../../.nuxt/witchcraft-electron.css";
|
|
44
72
|
```
|
|
@@ -47,22 +75,41 @@ If using the components, you should add the library to your tailwind css as a so
|
|
|
47
75
|
|
|
48
76
|
A directory structure like the following is suggested:
|
|
49
77
|
```
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
app
|
|
78
|
+
[project root]
|
|
79
|
+
├── .dist/ (I prefer .dist over dist so it stays hidden and at the top)/
|
|
80
|
+
│ └── [platform]/
|
|
81
|
+
│ ├── .output/ (nuxt output)
|
|
82
|
+
│ ├── release/
|
|
83
|
+
│ │ └── ${productName}_${version}.${ext}
|
|
84
|
+
│ └── build/ (for any intermediate builds like electron's)
|
|
85
|
+
├── app/ - nuxt code
|
|
86
|
+
└── app-electron/ - contains all the main/renderer code
|
|
58
87
|
```
|
|
88
|
+
The module sets it up like this when building electron, but not for the regular build. You should set that to go elsewhere if you're using it (though it's not required).
|
|
89
|
+
|
|
90
|
+
```ts [nuxt.config.ts]
|
|
91
|
+
export default defineNuxtConfig({
|
|
92
|
+
nitro: {
|
|
93
|
+
output: {
|
|
94
|
+
dir: ".dist/web/.output",
|
|
95
|
+
serverDir: ".dist/web/.output/server",
|
|
96
|
+
publicDir: ".dist/web/.output/public"
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
59
102
|
Usage of nuxt 4's new directory structure is recommended.
|
|
60
103
|
|
|
61
104
|
For whatever electron builder you want to use, you must point it at the correct directories.
|
|
62
105
|
|
|
63
106
|
For `electron-builder` with the default directories the module uses and to have all the artifacts in one folder, add:
|
|
64
107
|
|
|
65
|
-
|
|
108
|
+
|
|
109
|
+
<details>
|
|
110
|
+
<summary>Electron Builder Config Example</summary>
|
|
111
|
+
|
|
112
|
+
```json
|
|
66
113
|
{
|
|
67
114
|
"directories": {
|
|
68
115
|
"output": ".dist/electron/release"
|
|
@@ -85,103 +132,122 @@ For `electron-builder` with the default directories the module uses and to have
|
|
|
85
132
|
},
|
|
86
133
|
}
|
|
87
134
|
```
|
|
135
|
+
</details>
|
|
88
136
|
|
|
89
137
|
Add the following to the package.json:
|
|
90
|
-
|
|
138
|
+
|
|
139
|
+
<details>
|
|
140
|
+
<summary>Package.json</summary>
|
|
141
|
+
|
|
142
|
+
```json
|
|
91
143
|
// package.json
|
|
92
144
|
{
|
|
93
145
|
"main": ".dist/electron/build/main.cjs",
|
|
94
146
|
"scripts": {
|
|
95
147
|
"dev": "nuxi dev",
|
|
96
148
|
"build": "nuxi build",
|
|
97
|
-
"preview": "nuxt preview .dist/web",
|
|
149
|
+
"preview": "LOG_LEVEL=trace nuxt preview .dist/web",
|
|
98
150
|
"======= electron": "=======",
|
|
99
|
-
"dev:electron": "AUTO_OPEN=electron
|
|
100
|
-
// write a dev desktop file for linux, see below
|
|
101
|
-
"gen:dev:electron:desktop": "node node_modules/@witchcraft/nuxt-electron/genDevDesktop.js YOURAPPNAMEK",
|
|
102
|
-
// for the desktop file
|
|
151
|
+
"dev:electron": "AUTO_OPEN=electron pnpm dev",
|
|
103
152
|
"launch:electron": "electron .",
|
|
104
|
-
"
|
|
105
|
-
"build:electron
|
|
106
|
-
"build:electron:pack": "electron-builder",
|
|
107
|
-
"
|
|
153
|
+
"launch:electron:dev": "LOG_LEVEL=trace OVERRIDE_PUBLIC_SERVER_URL=http://localhost:3000 electron .",
|
|
154
|
+
"build:electron": "BUILD_ELECTRON=true pnpm build",
|
|
155
|
+
"build:electron:pack": "APP_VERSION=0.0.0 electron-builder",
|
|
156
|
+
"build:electron:no-pack": "APP_VERSION=0.0.0 SKIP_ELECTRON_PACK=true BUILD_ELECTRON=true nuxi build",
|
|
157
|
+
// write a dev desktop file for linux, see below
|
|
158
|
+
"preview:electron:dev": "concurrently --kill-others \"pnpm preview\" \"sleep 2 && pnpm launch:electron:dev\"",
|
|
159
|
+
"gen:dev:electron:desktop": "node node_modules/@witchcraft/nuxt-electron/genDevDesktop.js YOURAPPNAME",
|
|
108
160
|
}
|
|
109
161
|
}
|
|
110
162
|
```
|
|
163
|
+
</details>
|
|
164
|
+
|
|
165
|
+
#### To Develop
|
|
111
166
|
|
|
167
|
+
Run `pnpm dev:electron`. This will both launch nuxt and open electron.
|
|
168
|
+
|
|
169
|
+
Alternatively, run the server and electron seperately:
|
|
170
|
+
|
|
171
|
+
Run `pnpm dev` to start the nuxt dev server. The dev version of the app will be written to `.dist/electron/build/main.cjs`.
|
|
172
|
+
|
|
173
|
+
In a seperate terminal run `pnpm launch:electron` to start the electron app (this will do `electron .` which will run the configured `main` property, aka `.dist/electron/build/main.cjs`).
|
|
174
|
+
|
|
175
|
+
##### Notes
|
|
112
176
|
By default the module will not open electron. You must set `process.env.AUTO_OPEN` to include the string `electron` or set `autoOpen `in the options to true, hence the seperate `dev:electron` script.
|
|
113
177
|
|
|
114
178
|
The idea is if you use other platform modules as well, you'd do `AUTO_OPEN=electron,android`, etc. for each module you wanted to actually have auto open.
|
|
115
179
|
|
|
116
|
-
|
|
180
|
+
#### To Build
|
|
117
181
|
|
|
118
|
-
|
|
119
|
-
{
|
|
120
|
-
"scripts": {
|
|
121
|
-
"preview:electron:dev": "concurrrently --kill-others \" npm run dev\" \"PUBLIC_SERVER_URL=http://localhost:3000 electron .\"",
|
|
122
|
-
"preview:electron:prod": "electron .\""
|
|
182
|
+
Build the regular nuxt app with `pnpm build` then build the electron app with `pnpm build:electron` or `pnpm build:electron:no-pack` (if you just want to test, you can skip the packing).
|
|
123
183
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
184
|
+
In this case, the build written to `.dist/electron/build/main.cjs` is the **production** build.
|
|
185
|
+
|
|
186
|
+
To run the built production server and the production app proxied to this server, use `pnpm preview:electron:dev`.
|
|
187
|
+
|
|
188
|
+
Alternatively...
|
|
189
|
+
|
|
190
|
+
You can run `pnpm launch:electron` to launch the production build in nearly exactly as a user would.
|
|
191
|
+
|
|
192
|
+
CAREFUL though, **this will proxy api requests to the real server**.
|
|
193
|
+
|
|
194
|
+
If you want to test against the local server build, run it with `pnpm preview` then run the app with `pnpm launch:electron:dev`.
|
|
195
|
+
|
|
196
|
+
If you use the example code, it allows `OVERRIDE_PUBLIC_SERVER_URL` which allows the app to override which server the app proxies to and that's what the script is setting to allow this.
|
|
127
197
|
|
|
128
|
-
|
|
198
|
+
You can handle this different if you want and *not* allow overriding the server url. Up to you. See below.
|
|
199
|
+
|
|
200
|
+
### Files
|
|
129
201
|
|
|
130
202
|
In main to get the correct paths during build and dev, use the `getPaths` helper.
|
|
131
203
|
|
|
132
204
|
To get the dev user data dir, use the `useDevDataDir` helper.
|
|
133
205
|
|
|
134
|
-
We also need to create the nuxt `
|
|
135
|
-
|
|
136
|
-
```ts
|
|
137
|
-
// main.ts
|
|
206
|
+
We also need to create the nuxt `app://` protocol handler for every window and configure the proxies for server calls.
|
|
138
207
|
|
|
139
|
-
import { getPaths, useDevDataDir, createNuxtFileProtocolHandler } from "@witchcraft/nuxt-electron/electron"
|
|
140
208
|
|
|
141
|
-
|
|
142
|
-
const userDataDir = useDevDataDir() ?? app.getPath("userData")
|
|
209
|
+
See full example in [main.ts](https://github.com/witchcraftjs/nuxt-electron/blob/master/playground/app-electron/main.ts).
|
|
143
210
|
|
|
144
|
-
// when creating a window later
|
|
145
|
-
const win = new BrowserWindow({
|
|
146
|
-
title: "...",
|
|
147
|
-
webPreferences: {
|
|
148
|
-
preload: paths.preloadPath
|
|
149
|
-
}
|
|
150
|
-
})
|
|
151
211
|
|
|
152
|
-
|
|
153
|
-
const proxies = {
|
|
154
|
-
"/api": paths.publicServerUrl,
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
// for every session partition
|
|
158
|
-
createNuxtFileProtocolHandler(win.webContents.session, paths.nuxtDir, proxies)
|
|
159
|
-
await win.loadURL(paths.windowUrl)
|
|
160
|
-
```
|
|
212
|
+
For the nuxt config, here's the minimum you need, the one in the playground contains additional options for testing and debugging:
|
|
161
213
|
|
|
214
|
+
<details>
|
|
215
|
+
<summary>nuxt.config.ts</summary>
|
|
216
|
+
|
|
162
217
|
```ts [nuxt.config.ts]
|
|
163
218
|
export default defineNuxtConfig({
|
|
164
219
|
modules: [
|
|
165
220
|
"@witchcraft/nuxt-electron",
|
|
221
|
+
/** Optional */
|
|
222
|
+
"@witchcraft/ui",
|
|
223
|
+
/** Optional */
|
|
224
|
+
"@witchcraft/nuxt-logger",
|
|
166
225
|
],
|
|
226
|
+
dir: ".dist/web/.output",
|
|
227
|
+
serverDir: ".dist/web/.output/server",
|
|
228
|
+
publicDir: ".dist/web/.output/public"
|
|
229
|
+
},
|
|
230
|
+
},
|
|
167
231
|
electron: {
|
|
168
232
|
additionalElectronVariables: {
|
|
169
|
-
//
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
233
|
+
// this will hardcode `process.env.PUBLIC_SERVER_URL` to the server url in production
|
|
234
|
+
// this means getPaths().publicServerUrl will always return your site url (see getPaths) unless you allow getPaths an override (see it for details)
|
|
235
|
+
publicServerUrl: process.env.NODE_ENV === "production"
|
|
236
|
+
// note the quotes for strings! this is a literal replacement that happens
|
|
237
|
+
// you also cannot access process.env dynamically if you want this to work (e.g. process.env[name])
|
|
238
|
+
? `"https://yoursite.com"`
|
|
239
|
+
: `undefined`
|
|
240
|
+
},
|
|
241
|
+
// the module will set this to pre-render
|
|
242
|
+
// additionalRoutes: ["/other-page-prerendered"]
|
|
174
243
|
}
|
|
175
244
|
})
|
|
176
245
|
```
|
|
177
|
-
|
|
178
|
-
A full example, including usage with `@witchcraft/nuxt-logger` is available in the playground.
|
|
179
|
-
|
|
246
|
+
</details>
|
|
180
247
|
|
|
181
248
|
**NOTE: The proxies only work for api calls. They do not work for pages.**
|
|
182
249
|
|
|
183
|
-
Proxying server only page routes seems possible but complicated because each route's resource calls must also be proxied. I don't think it's worth the pain. It's easier to make sure the electron app never
|
|
184
|
-
|
|
250
|
+
Proxying server only page routes seems possible but complicated because each route's resource calls must also be proxied. I don't think it's worth the pain. It's easier to make sure the electron app never access server only routes.
|
|
185
251
|
|
|
186
252
|
### Runtime Config
|
|
187
253
|
|
|
@@ -212,12 +278,19 @@ An isomorphic logger is also available for electron, see [@witchcraft/nuxt-logge
|
|
|
212
278
|
|
|
213
279
|
A script is provided for use with electron-builder to generate a dev desktop file for linux.
|
|
214
280
|
|
|
281
|
+
This is useful for when registering deep links in the app as these require a desktop file to work on linux.
|
|
282
|
+
|
|
215
283
|
It will create a desktop file named `dev-YOURAPPNAME.desktop`, put it in `~/.local/share/applications/` and re-install it with `xdg-desktop-menu un/install`.
|
|
216
284
|
|
|
217
|
-
The desktop's exec is set to run bash, cd into the project dir and run `
|
|
285
|
+
The desktop's exec is set to run bash, cd into the project dir and run `pnpm launch:electron`.
|
|
218
286
|
|
|
219
|
-
|
|
287
|
+
You can pass a second parameter to the script to use a different package.json script.
|
|
220
288
|
|
|
289
|
+
And a third parameter pointing to your config if it's not in one of the searched locations:
|
|
290
|
+
|
|
291
|
+
- `electron-builder-config.js`
|
|
292
|
+
- `electron-builder.json5`
|
|
293
|
+
- `electron-builder.json`
|
|
221
294
|
|
|
222
295
|
## Misc Notes
|
|
223
296
|
|
|
@@ -240,17 +313,31 @@ First for production only changes, we run the nuxt config with a different env v
|
|
|
240
313
|
|
|
241
314
|
Then we use a custom protocol to proxy requests and api calls.
|
|
242
315
|
|
|
243
|
-
##### Custom `
|
|
316
|
+
##### Custom `app://` Handler
|
|
317
|
+
|
|
318
|
+
Electron uses the `file://` protocol by default to load all scripts/assets/etc.
|
|
319
|
+
|
|
320
|
+
Loading from files does not work well with the default nuxt config so we use use a custom protocol handler to intercept these requests and correctly route them so that we don't have to be changing nuxt's baseURL and buildAssetsDir.
|
|
321
|
+
|
|
322
|
+
It is recommended to use a custom protocol instead of `file://`, so the example uses `app://`.
|
|
323
|
+
|
|
324
|
+
Additionally to have as close to regular browser behavior, the host is set to `bundle`.
|
|
325
|
+
|
|
326
|
+
So `getPaths` returns `app://bundle/path/to/file` for `windowUrl` in production.
|
|
327
|
+
|
|
328
|
+
This is because using a `standard` protocol schema, paths like `/path/to/file` are resolved relative to the host as they are in the browser.
|
|
244
329
|
|
|
245
|
-
|
|
330
|
+
Internally the protocol handler does the following:
|
|
246
331
|
|
|
247
|
-
|
|
332
|
+
- Checks the request is safe (does not try to escape the path given).
|
|
333
|
+
- Reroutes proxy requests to the correct url.
|
|
334
|
+
- Checks if the request exists as a file, if not, attempts to find the nested `index.html` file (e.g. `/some/path` will correctly load `/some/path/index.html`).
|
|
248
335
|
|
|
249
336
|
##### Electron Route
|
|
250
337
|
In electron we want to point to a different route, `/{electronRoute}`, so we can trim other routes from the bundle.
|
|
251
338
|
|
|
252
339
|
To do this we prerender the `/{electronRoute}` route and point electron to `/{electronRoute}/index.html`.
|
|
253
|
-
- This used to require changing baseURL and buildAssetsDir to "./" and "/" respectively, but with the
|
|
340
|
+
- This used to require changing baseURL and buildAssetsDir to "./" and "/" respectively, but with the custom `app://` handler it should just work.
|
|
254
341
|
|
|
255
342
|
We then need to remove unwanted routes from the bundle which are included regardless of whether they're used. So for the web app, remove electron routes, and for the electron app, remove web app routes. This is done through a nuxt hook in the nuxt config on production builds only.
|
|
256
343
|
|
|
@@ -266,7 +353,7 @@ A redirect from a page (e.i. `if (process.client && isElectron) { await navigate
|
|
|
266
353
|
|
|
267
354
|
Building for electron requires lots of changes to the config. We can't just build for web then copy. So this module reroutes the output when building the web app (and reroutes it differently when building for electron (see directory sturcture above).
|
|
268
355
|
|
|
269
|
-
The
|
|
356
|
+
The reason for the nested `.output` is so it doesn't overwrite the default one. We can also do `nuxt preview .dist/platform` though it's often not of much help.
|
|
270
357
|
|
|
271
358
|
To build for electron you must set `process.env.BUILD_ELECTRON` to true, to do the configuration required to make the final output actually work with electron.
|
|
272
359
|
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { crop } from '@alanscodelog/utils/crop';
|
|
2
2
|
import { run } from '@alanscodelog/utils/run';
|
|
3
3
|
import { defineNuxtModule, useLogger, createResolver, addComponentsDir, addTemplate, extendRouteRules, addImportsDir } from '@nuxt/kit';
|
|
4
|
-
import { nuxtRemoveUneededPages,
|
|
4
|
+
import { nuxtRemoveUneededPages, nuxtRerouteOutputTo, createConstantCaseVariables } from '@witchcraft/nuxt-utils/utils';
|
|
5
5
|
import { defu } from 'defu';
|
|
6
6
|
import fs from 'node:fs/promises';
|
|
7
7
|
import path from 'node:path';
|
|
@@ -90,7 +90,7 @@ const module$1 = defineNuxtModule({
|
|
|
90
90
|
logger.warn(`Missing electron scripts: ${[hasMainScript ? "" : "main.ts", hasPreloadScript ? "" : "preload.ts"].join(", ")}. Skipping electron build.`);
|
|
91
91
|
}
|
|
92
92
|
const isElectronBuild = process.env.BUILD_ELECTRON === "true" && hasScripts;
|
|
93
|
-
const skipElectronPack = process.env.SKIP_ELECTRON_PACK === "true";
|
|
93
|
+
const skipElectronPack = !isElectronBuild || process.env.SKIP_ELECTRON_PACK === "true";
|
|
94
94
|
const autoOpen = !!(options.autoOpen && hasScripts && isDev);
|
|
95
95
|
const useWatch = nuxt.options.dev;
|
|
96
96
|
const devUserDataDir = options.devUserDataDir && await resolvePath(options.devUserDataDir, nuxt.options.alias);
|
|
@@ -159,11 +159,11 @@ const module$1 = defineNuxtModule({
|
|
|
159
159
|
);
|
|
160
160
|
const additionalElectronVariables = defu(
|
|
161
161
|
options.additionalElectronVariables,
|
|
162
|
-
nuxt.options.electron.additionalElectronVariables
|
|
162
|
+
nuxt.options.electron === false ? {} : nuxt.options.electron.additionalElectronVariables
|
|
163
163
|
);
|
|
164
164
|
const additionalViteDefinesToCopy = [
|
|
165
165
|
...options.additionalViteDefinesToCopy,
|
|
166
|
-
...nuxt.options.electron.additionalViteDefinesToCopy ?? []
|
|
166
|
+
...nuxt.options.electron === false ? [] : nuxt.options.electron.additionalViteDefinesToCopy ?? []
|
|
167
167
|
];
|
|
168
168
|
const copyFromVite = [
|
|
169
169
|
"__NUXT_VERSION__",
|
|
@@ -183,9 +183,10 @@ const module$1 = defineNuxtModule({
|
|
|
183
183
|
// on windows the backslashes in the paths must be double escaped
|
|
184
184
|
electronRoute: electronRoute.replaceAll("\\", "\\\\"),
|
|
185
185
|
electronProdUrl: electronProdUrl.replaceAll("\\", "\\\\"),
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
186
|
+
// all paths muxt be made relative to the build dir where electron will launch from in production
|
|
187
|
+
electronNuxtDir: path.relative(electronBuildDir, relativeElectronDir).replaceAll("\\", "\\\\"),
|
|
188
|
+
electronNuxtPublicDir: path.relative(electronBuildDir, electronNuxtPublicDir).replaceAll("\\", "\\\\"),
|
|
189
|
+
electronBuildDir: path.relative(electronBuildDir, electronBuildDir).replaceAll("\\", "\\\\"),
|
|
189
190
|
// ...options.additionalElectronVariables,
|
|
190
191
|
// nuxt's runtimeConfig cannot be used in electron's main since it's built seperately
|
|
191
192
|
// also we must stringify ourselves since escaped double quotes are not preserved in the final output :/
|
|
@@ -328,11 +329,21 @@ const module$1 = defineNuxtModule({
|
|
|
328
329
|
}
|
|
329
330
|
});
|
|
330
331
|
nuxtRemoveUneededPages(nuxt, ["/", electronRoute, ...options.additionalRoutes]);
|
|
331
|
-
|
|
332
|
-
nuxt.options.router =
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
);
|
|
332
|
+
nuxt.options.router.options ??= {};
|
|
333
|
+
nuxt.options.router.options.hashMode = false;
|
|
334
|
+
extendRouteRules(electronRoute + "/**", {
|
|
335
|
+
prerender: true
|
|
336
|
+
}, { override: true });
|
|
337
|
+
for (const route of options.additionalRoutes) {
|
|
338
|
+
extendRouteRules(route, {
|
|
339
|
+
prerender: true
|
|
340
|
+
}, { override: true });
|
|
341
|
+
}
|
|
342
|
+
nuxt.hook("prerender:routes", ({ routes }) => {
|
|
343
|
+
for (const route of ["/404.html"]) {
|
|
344
|
+
routes.add(route);
|
|
345
|
+
}
|
|
346
|
+
});
|
|
336
347
|
nuxtRerouteOutputTo(nuxt, electronNuxtDir);
|
|
337
348
|
nuxt.hook("close", async () => {
|
|
338
349
|
logger.info(`Building Electron`);
|
|
@@ -361,7 +372,16 @@ const module$1 = defineNuxtModule({
|
|
|
361
372
|
} else {
|
|
362
373
|
logger.info(`Skipping Electron Build`);
|
|
363
374
|
}
|
|
364
|
-
|
|
375
|
+
const nuxtOutputDir = nuxt.options.nitro?.output?.dir;
|
|
376
|
+
if (nuxtOutputDir === void 0 || nuxtOutputDir === ".output") {
|
|
377
|
+
logger.warn(crop`Nitro output dir is not set or set to the default, it's suggested you set it to the following when using nuxt-electron:
|
|
378
|
+
nitro: {
|
|
379
|
+
output: ".dist/web/.output",
|
|
380
|
+
serverDir: ".dist/web/.output/server"
|
|
381
|
+
publicDir: ".dist/web/.output/public"
|
|
382
|
+
}
|
|
383
|
+
.`);
|
|
384
|
+
}
|
|
365
385
|
}
|
|
366
386
|
addImportsDir(resolve("runtime/utils"));
|
|
367
387
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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;
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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;
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Protocol } from "electron";
|
|
2
|
+
export declare function createProxiedProtocolHandler(
|
|
3
|
+
/** `protocol` or `session.protocol` depending on if you're using paritions with your windows or not, regular `protocol` only registers the handler for the default parition. */
|
|
4
|
+
protocol: Protocol, protocolName: string | undefined, basePath: string,
|
|
5
|
+
/**
|
|
6
|
+
* If any route starts with one of these keys, it will get rerouted to the given value.
|
|
7
|
+
*
|
|
8
|
+
* At least `/api` should be added for a basic nuxt app to work correctly.
|
|
9
|
+
*
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* routeProxies: {
|
|
13
|
+
* "/api": "http://localhost:3000/api",
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
routeProxies: Record<string, string>, { logger, errorPage }?: {
|
|
18
|
+
/**
|
|
19
|
+
* Optional logger. It's suggested you not pass this unless you're traceging in dev mode as a lot of requests can be made.
|
|
20
|
+
*/
|
|
21
|
+
logger?: {
|
|
22
|
+
trace: (...args: any[]) => void;
|
|
23
|
+
error: (...args: any[]) => void;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The path to the error page. Defaults to "/404.html".
|
|
27
|
+
*
|
|
28
|
+
* 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.
|
|
29
|
+
*/
|
|
30
|
+
errorPage?: string;
|
|
31
|
+
}): void;
|