Include code cache for preload bundle

Co-authored-by: Scott Nonnenberg <scott@signal.org>
This commit is contained in:
Fedor Indutny 2024-09-08 14:09:57 -07:00 committed by GitHub
parent 3c9332449f
commit 695f64a55a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 230 additions and 11 deletions

View file

@ -0,0 +1,12 @@
<!-- Copyright 2014 Signal Messenger, LLC -->
<!-- SPDX-License-Identifier: AGPL-3.0-only -->
<!doctype html>
<html>
<head>
<title>Generating cache...</title>
</head>
<body>
Generating cache...
</body>
</html>

View file

@ -0,0 +1,29 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { Module } from 'node:module';
import { readFile, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { Script } from 'node:vm';
import { ipcRenderer } from 'electron';
ipcRenderer.on('compile', async () => {
try {
const sourceFile = join(__dirname, '..', '..', 'preload.bundle.js');
const outFile = sourceFile.replace(/\.js$/, '');
const source = await readFile(sourceFile, 'utf8');
const script = new Script(Module.wrap(source), {
filename: 'preload.bundle.js',
produceCachedData: true,
});
if (!script.cachedDataProduced || !script.cachedData) {
throw new Error('Cached data not produced');
}
await writeFile(`${outFile}.cache`, script.cachedData);
await ipcRenderer.invoke('done');
} catch (error) {
await ipcRenderer.invoke('error', error);
}
});

View file

@ -0,0 +1,35 @@
// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { pathToFileURL } from 'node:url';
import { join } from 'node:path';
import { app, BrowserWindow, ipcMain } from 'electron';
app.on('ready', async () => {
ipcMain.handle('done', () => {
app.quit();
});
ipcMain.handle('error', (_event, err) => {
console.error(err);
process.exit(1);
});
const window = new BrowserWindow({
show: false,
webPreferences: {
devTools: true,
nodeIntegration: false,
sandbox: false,
contextIsolation: true,
preload: join(__dirname, 'generate-preload-cache.preload.js'),
},
});
await window.loadURL(
pathToFileURL(join(__dirname, 'generate-preload-cache.html')).toString()
);
window.webContents.openDevTools();
window.webContents.send('compile', process.argv[2], process.argv[3]);
});

View file

@ -15,14 +15,14 @@ let archive: string;
let exe: string;
if (process.platform === 'darwin') {
archive = join(
'mac',
'mac-arm64',
`${packageJson.productName}.app`,
'Contents',
'Resources',
'app.asar'
);
exe = join(
'mac',
'mac-arm64',
`${packageJson.productName}.app`,
'Contents',
'MacOS',