diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-30 00:26:01 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-30 02:24:28 +0100 |
commit | 61fd13c55f5e69a9d8b32dd0d74b08870783bcce (patch) | |
tree | 4f3f97b1629f3c262bea076b596bc7245ccbc0bd /packages/main/src | |
parent | Revert "refactor: Switch back to consola for prettyness" (diff) | |
download | sophie-61fd13c55f5e69a9d8b32dd0d74b08870783bcce.tar.gz sophie-61fd13c55f5e69a9d8b32dd0d74b08870783bcce.tar.zst sophie-61fd13c55f5e69a9d8b32dd0d74b08870783bcce.zip |
build: Switch to esbuild
We will build all packages except the frontend (where vite remains in
use) with esbuild.
For some reason, the @yarnpkg/esbuild-plugin-pnp doesn't allow esbuild
to load esm modules and we fall back to commonjs for dependencies.
Hence we had to switch back to node_modules (but still rely on yarn
hardlinking for a more efficient install).
Diffstat (limited to 'packages/main/src')
-rw-r--r-- | packages/main/src/controllers/config.ts | 2 | ||||
-rw-r--r-- | packages/main/src/devTools.ts | 41 | ||||
-rw-r--r-- | packages/main/src/index.ts | 38 | ||||
-rw-r--r-- | packages/main/src/services/ConfigPersistenceService.ts | 2 | ||||
-rw-r--r-- | packages/main/src/utils/logging.ts | 4 |
5 files changed, 42 insertions, 45 deletions
diff --git a/packages/main/src/controllers/config.ts b/packages/main/src/controllers/config.ts index 7187ab4..f2467c7 100644 --- a/packages/main/src/controllers/config.ts +++ b/packages/main/src/controllers/config.ts | |||
@@ -18,7 +18,7 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { debounce } from 'lodash'; | 21 | import debounce from 'lodash-es/debounce'; |
22 | import ms from 'ms'; | 22 | import ms from 'ms'; |
23 | import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree'; | 23 | import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree'; |
24 | 24 | ||
diff --git a/packages/main/src/devTools.ts b/packages/main/src/devTools.ts index b98974a..6c25b3e 100644 --- a/packages/main/src/devTools.ts +++ b/packages/main/src/devTools.ts | |||
@@ -18,38 +18,35 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import type { App, BrowserWindow } from 'electron'; | 21 | import type { BrowserWindow } from 'electron'; |
22 | 22 | ||
23 | /** | 23 | /** |
24 | * Installs the react and redux developer tools extensions. | 24 | * Installs the react and redux developer tools extensions. |
25 | * | 25 | * |
26 | * We use the redux devtools and connect the mobx store to it with `mst-middlewares`, | 26 | * We use the redux devtools and connect the mobx store to it with `mst-middlewares`, |
27 | * because the mobx-state-tree devtools are currently unmaintained. | 27 | * because the mobx-state-tree devtools are currently unmaintained. |
28 | * | ||
29 | * @param app The electron application instance. | ||
30 | */ | 28 | */ |
31 | export function installDevToolsExtensions(app: App): void { | 29 | export async function installDevToolsExtensions(): Promise<void> { |
32 | app.whenReady().then(async () => { | 30 | const installerPackage = await import('electron-devtools-installer'); |
33 | const { | 31 | const { |
34 | default: installExtension, | 32 | default: installExtension, |
33 | REACT_DEVELOPER_TOOLS, | ||
34 | REDUX_DEVTOOLS, | ||
35 | } = installerPackage.default instanceof Function | ||
36 | ? installerPackage | ||
37 | : installerPackage.default as unknown as typeof import('electron-devtools-installer'); | ||
38 | await installExtension( | ||
39 | [ | ||
35 | REACT_DEVELOPER_TOOLS, | 40 | REACT_DEVELOPER_TOOLS, |
36 | REDUX_DEVTOOLS, | 41 | REDUX_DEVTOOLS, |
37 | } = await import('electron-devtools-installer'); | 42 | ], |
38 | installExtension( | 43 | { |
39 | [ | 44 | forceDownload: false, |
40 | REACT_DEVELOPER_TOOLS, | 45 | loadExtensionOptions: { |
41 | REDUX_DEVTOOLS, | 46 | allowFileAccess: true, |
42 | ], | ||
43 | { | ||
44 | forceDownload: false, | ||
45 | loadExtensionOptions: { | ||
46 | allowFileAccess: true, | ||
47 | }, | ||
48 | }, | 47 | }, |
49 | ); | 48 | }, |
50 | }).catch((err) => { | 49 | ); |
51 | console.error('Failed to install devtools extension', err); | ||
52 | }); | ||
53 | } | 50 | } |
54 | 51 | ||
55 | /** | 52 | /** |
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index 7c7be35..f8b6787 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts | |||
@@ -50,6 +50,9 @@ import { createMainStore } from './stores/MainStore'; | |||
50 | 50 | ||
51 | const isDevelopment = import.meta.env.MODE === 'development'; | 51 | const isDevelopment = import.meta.env.MODE === 'development'; |
52 | 52 | ||
53 | // Alwayse enable sandboxing. | ||
54 | app.enableSandbox(); | ||
55 | |||
53 | // Use alternative directory when debugging to avoid clobbering the main installation. | 56 | // Use alternative directory when debugging to avoid clobbering the main installation. |
54 | if (isDevelopment) { | 57 | if (isDevelopment) { |
55 | app.setPath('userData', `${app.getPath('userData')}-dev`); | 58 | app.setPath('userData', `${app.getPath('userData')}-dev`); |
@@ -62,9 +65,6 @@ if (!isSingleInstance) { | |||
62 | process.exit(0); | 65 | process.exit(0); |
63 | } | 66 | } |
64 | 67 | ||
65 | // Alwayse enable sandboxing. | ||
66 | app.enableSandbox(); | ||
67 | |||
68 | // Disable chromium's MPRIS integration, which is usually more annoying | 68 | // Disable chromium's MPRIS integration, which is usually more annoying |
69 | // (triggered by random sounds played by websites) than useful. | 69 | // (triggered by random sounds played by websites) than useful. |
70 | app.commandLine.appendSwitch( | 70 | app.commandLine.appendSwitch( |
@@ -90,17 +90,13 @@ function getResourceUrl(relativePath: string): string { | |||
90 | return new URL(relativePath, baseUrl).toString(); | 90 | return new URL(relativePath, baseUrl).toString(); |
91 | } | 91 | } |
92 | 92 | ||
93 | let serviceInjectRelativePath = '../../service-inject/dist/index.cjs'; | 93 | let serviceInjectRelativePath = '../../service-inject/dist/index.js'; |
94 | let serviceInjectPath = getResourcePath(serviceInjectRelativePath); | 94 | let serviceInjectPath = getResourcePath(serviceInjectRelativePath); |
95 | let serviceInject: WebSource = { | 95 | let serviceInject: WebSource = { |
96 | code: readFileSync(serviceInjectPath, 'utf8'), | 96 | code: readFileSync(serviceInjectPath, 'utf8'), |
97 | url: getResourceUrl(serviceInjectRelativePath), | 97 | url: getResourceUrl(serviceInjectRelativePath), |
98 | }; | 98 | }; |
99 | 99 | ||
100 | if (isDevelopment) { | ||
101 | installDevToolsExtensions(app); | ||
102 | } | ||
103 | |||
104 | let mainWindow: BrowserWindow | null = null; | 100 | let mainWindow: BrowserWindow | null = null; |
105 | 101 | ||
106 | const store = createMainStore(); | 102 | const store = createMainStore(); |
@@ -134,13 +130,13 @@ function shouldCancelMainWindowRequest(url: string, method: string): boolean { | |||
134 | return !normalizedUrl.startsWith(getResourceUrl(rendererBaseUrl)); | 130 | return !normalizedUrl.startsWith(getResourceUrl(rendererBaseUrl)); |
135 | } | 131 | } |
136 | 132 | ||
137 | function createWindow(): Promise<unknown> { | 133 | async function createWindow(): Promise<unknown> { |
138 | mainWindow = new BrowserWindow({ | 134 | mainWindow = new BrowserWindow({ |
139 | show: false, | 135 | show: false, |
140 | autoHideMenuBar: true, | 136 | autoHideMenuBar: true, |
141 | webPreferences: { | 137 | webPreferences: { |
142 | devTools: isDevelopment, | ||
143 | sandbox: true, | 138 | sandbox: true, |
139 | devTools: isDevelopment, | ||
144 | preload: getResourcePath('../../preload/dist/index.cjs'), | 140 | preload: getResourcePath('../../preload/dist/index.cjs'), |
145 | }, | 141 | }, |
146 | }); | 142 | }); |
@@ -159,8 +155,14 @@ function createWindow(): Promise<unknown> { | |||
159 | }) | 155 | }) |
160 | }); | 156 | }); |
161 | 157 | ||
162 | webContents.on('will-navigate', (event) => { | 158 | const pageUrl = (isDevelopment && import.meta.env.VITE_DEV_SERVER_URL !== undefined) |
163 | event.preventDefault(); | 159 | ? import.meta.env.VITE_DEV_SERVER_URL |
160 | : getResourceUrl('../renderer/dist/index.html'); | ||
161 | |||
162 | webContents.on('will-navigate', (event, url) => { | ||
163 | if (url !== pageUrl) { | ||
164 | event.preventDefault(); | ||
165 | } | ||
164 | }); | 166 | }); |
165 | 167 | ||
166 | webContents.setWindowOpenHandler(() => ({ action: 'deny' })); | 168 | webContents.setWindowOpenHandler(() => ({ action: 'deny' })); |
@@ -279,10 +281,6 @@ function createWindow(): Promise<unknown> { | |||
279 | callback({ requestHeaders }); | 281 | callback({ requestHeaders }); |
280 | }); | 282 | }); |
281 | 283 | ||
282 | const pageUrl = (isDevelopment && import.meta.env.VITE_DEV_SERVER_URL !== undefined) | ||
283 | ? import.meta.env.VITE_DEV_SERVER_URL | ||
284 | : getResourceUrl('../renderer/dist/index.html'); | ||
285 | |||
286 | return Promise.all([ | 284 | return Promise.all([ |
287 | mainWindow.loadURL(pageUrl), | 285 | mainWindow.loadURL(pageUrl), |
288 | browserView.webContents.loadURL('https://git.marussy.com/sophie/about'), | 286 | browserView.webContents.loadURL('https://git.marussy.com/sophie/about'), |
@@ -307,7 +305,13 @@ app.on('window-all-closed', () => { | |||
307 | } | 305 | } |
308 | }); | 306 | }); |
309 | 307 | ||
310 | app.whenReady().then(createWindow).catch((err) => { | 308 | app.whenReady().then(async () => { |
309 | if (isDevelopment) { | ||
310 | await installDevToolsExtensions(); | ||
311 | } | ||
312 | |||
313 | return createWindow(); | ||
314 | }).catch((err) => { | ||
311 | console.error('Failed to create window', err); | 315 | console.error('Failed to create window', err); |
312 | process.exit(1); | 316 | process.exit(1); |
313 | }); | 317 | }); |
diff --git a/packages/main/src/services/ConfigPersistenceService.ts b/packages/main/src/services/ConfigPersistenceService.ts index 1c0315f..ee5eb9f 100644 --- a/packages/main/src/services/ConfigPersistenceService.ts +++ b/packages/main/src/services/ConfigPersistenceService.ts | |||
@@ -20,7 +20,7 @@ | |||
20 | import { watch } from 'fs'; | 20 | import { watch } from 'fs'; |
21 | import { readFile, stat, writeFile } from 'fs/promises'; | 21 | import { readFile, stat, writeFile } from 'fs/promises'; |
22 | import JSON5 from 'json5'; | 22 | import JSON5 from 'json5'; |
23 | import { throttle } from 'lodash'; | 23 | import throttle from 'lodash-es/throttle'; |
24 | import { join } from 'path'; | 24 | import { join } from 'path'; |
25 | 25 | ||
26 | import type { ConfigSnapshotOut } from '../stores/Config'; | 26 | import type { ConfigSnapshotOut } from '../stores/Config'; |
diff --git a/packages/main/src/utils/logging.ts b/packages/main/src/utils/logging.ts index 9f1133f..5cb5d21 100644 --- a/packages/main/src/utils/logging.ts +++ b/packages/main/src/utils/logging.ts | |||
@@ -36,10 +36,6 @@ prefix.apply(loglevel, { | |||
36 | const lastSegment = nameSegments.pop(); | 36 | const lastSegment = nameSegments.pop(); |
37 | shortName = [...nameSegments.map((segment) => segment[0]), lastSegment].join(':'); | 37 | shortName = [...nameSegments.map((segment) => segment[0]), lastSegment].join(':'); |
38 | } | 38 | } |
39 | if (isDevelopment) { | ||
40 | // `watch.js` already appends timestamps. | ||
41 | return `${level} (${shortName})`; | ||
42 | } | ||
43 | return `[${timestamp}] ${level} (${shortName})`; | 39 | return `[${timestamp}] ${level} (${shortName})`; |
44 | }, | 40 | }, |
45 | }); | 41 | }); |