diff options
author | 2021-12-30 00:26:01 +0100 | |
---|---|---|
committer | 2021-12-30 02:24:28 +0100 | |
commit | 61fd13c55f5e69a9d8b32dd0d74b08870783bcce (patch) | |
tree | 4f3f97b1629f3c262bea076b596bc7245ccbc0bd /packages/main/src/index.ts | |
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/index.ts')
-rw-r--r-- | packages/main/src/index.ts | 38 |
1 files changed, 21 insertions, 17 deletions
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 | }); |