aboutsummaryrefslogtreecommitdiffstats
path: root/.electron-builder.config.cjs
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2022-05-04 21:50:32 +0200
committerLibravatar Kristóf Marussy <kristof@marussy.com>2022-05-16 00:55:03 +0200
commitefc32118b6b079462255890a7cb7c2e09ae9d473 (patch)
tree7bf13f27316c5db78fc6ea38e9a550f9a97452e4 /.electron-builder.config.cjs
parentbuild: integration testing support (diff)
downloadsophie-efc32118b6b079462255890a7cb7c2e09ae9d473.tar.gz
sophie-efc32118b6b079462255890a7cb7c2e09ae9d473.tar.zst
sophie-efc32118b6b079462255890a7cb7c2e09ae9d473.zip
feat: use wayland when available
Auto-detect wayland in the yarn watch script. We use a shell script wrapper to launch sophie with wayland-specific arguments whenever appropriate. Because of this, the electron binary that ships with sophie has been renamed to sophie-bin. Signed-off-by: Kristóf Marussy <kristof@marussy.com>
Diffstat (limited to '.electron-builder.config.cjs')
-rw-r--r--.electron-builder.config.cjs98
1 files changed, 73 insertions, 25 deletions
diff --git a/.electron-builder.config.cjs b/.electron-builder.config.cjs
index efd9ef7..3eef91c 100644
--- a/.electron-builder.config.cjs
+++ b/.electron-builder.config.cjs
@@ -1,29 +1,8 @@
1const { readFile, rename, writeFile } = require('node:fs/promises');
2const path = require('node:path');
3
1const { Arch } = require('electron-builder'); 4const { Arch } = require('electron-builder');
2const { flipFuses, FuseV1Options, FuseVersion } = require('@electron/fuses'); 5const { flipFuses, FuseV1Options, FuseVersion } = require('@electron/fuses');
3const { join } = require('path');
4
5/**
6 * @type {import('electron-builder').Configuration}
7 * @see https://www.electron.build/configuration/configuration
8 */
9const config = {
10 directories: {
11 output: 'dist',
12 buildResources: 'buildResources',
13 },
14 files: [
15 'packages/main/dist/**',
16 'packages/preload/dist/**',
17 'packages/renderer/dist/**',
18 'packages/service-preload/dist/**',
19 'locales/**',
20 // Do not ship with source maps.
21 '!**/*.map',
22 ],
23 afterPack(context) {
24 return burnFuses(context);
25 },
26};
27 6
28/** 7/**
29 * Hardens the shipped electron binary by burning some electron fuses. 8 * Hardens the shipped electron binary by burning some electron fuses.
@@ -46,7 +25,7 @@ async function burnFuses(context) {
46 darwin: '.app', 25 darwin: '.app',
47 win32: '.exe', 26 win32: '.exe',
48 }[context.electronPlatformName] || ''; 27 }[context.electronPlatformName] || '';
49 const electronBinaryPath = join( 28 const electronBinaryPath = path.join(
50 context.appOutDir, 29 context.appOutDir,
51 `${context.packager.appInfo.productFilename}${ext}`, 30 `${context.packager.appInfo.productFilename}${ext}`,
52 ); 31 );
@@ -66,4 +45,73 @@ async function burnFuses(context) {
66 return flipFuses(electronBinaryPath, fuseConfig); 45 return flipFuses(electronBinaryPath, fuseConfig);
67} 46}
68 47
48/**
49 * Adds a wrapper scripts that detects in wayland is in use and enabled it in chromium.
50 *
51 * The script in `build-heleprs/detect_wayland.sh` uses the `WAYLAND_DISPLAY` environmental
52 * variable to detect whether wayland is in use.
53 *
54 * If wayland is in use, the script enables the wayland ozone backed for chromium
55 * and pipewire screen sharing. Otherwise, the x11 ozone backend will be used.
56 *
57 * @param {import('electron-builder').AfterPackContext} context The `electron-builder` context.
58 * @return {Promise<void>} The promise to add the wrapper script.
59 * @see https://stackoverflow.com/a/45537237
60 */
61async function enableWaylandAutoDetection(context) {
62 const { appOutDir, packager: { appInfo: { productName, productFilename } } } = context;
63 const electronBinaryPath = path.join(appOutDir, productFilename);
64 const newFilename = `${productFilename}-bin`;
65 const newElectronBinaryPath = path.join(appOutDir, newFilename);
66 await rename(electronBinaryPath, newElectronBinaryPath);
67 const wrapperScriptPath = path.join(__dirname, 'build-helpers/detect_wayland.sh');
68 const wrapperScriptTempate = await readFile(wrapperScriptPath, 'utf8');
69 const replacements = new Map([
70 ['PRODUCT_NAME', productName],
71 ['REAL_BINARY_NAME', newFilename],
72 ]);
73 const wrapperScript = wrapperScriptTempate.replaceAll(
74 /\{\{([^}]+)\}\}/g,
75 (_match, /** @type {string} */ variable) => {
76 const replacement = replacements.get(variable);
77 if (replacement === undefined) {
78 throw new Error(`Unknown variable: ${variable}`);
79 }
80 return replacement;
81 },
82 );
83 await writeFile(electronBinaryPath, wrapperScript, {
84 encoding: 'utf8',
85 mode: 0o755,
86 });
87}
88
89/**
90 * @type {import('electron-builder').Configuration}
91 * @see https://www.electron.build/configuration/configuration
92 */
93const config = {
94 directories: {
95 output: 'dist',
96 buildResources: 'buildResources',
97 },
98 files: [
99 'packages/main/dist/**',
100 'packages/preload/dist/**',
101 'packages/renderer/dist/**',
102 'packages/service-preload/dist/**',
103 'locales/**',
104 // Do not ship with source maps.
105 '!**/*.map',
106 ],
107 afterPack(context) {
108 return burnFuses(context);
109 },
110 async afterSign(context) {
111 if (context.electronPlatformName === 'linux') {
112 await enableWaylandAutoDetection(context);
113 }
114 }
115};
116
69module.exports = config; 117module.exports = config;