Generate preload cache from live app
This commit is contained in:
parent
695f64a55a
commit
3dba3a07f0
7 changed files with 57 additions and 69 deletions
|
@ -1,12 +0,0 @@
|
|||
<!-- 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>
|
|
@ -1,29 +0,0 @@
|
|||
// 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);
|
||||
}
|
||||
});
|
|
@ -1,35 +1,51 @@
|
|||
// Copyright 2024 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { join } from 'node:path';
|
||||
import { app, BrowserWindow, ipcMain } from 'electron';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { mkdtemp, rm } from 'node:fs/promises';
|
||||
|
||||
app.on('ready', async () => {
|
||||
ipcMain.handle('done', () => {
|
||||
app.quit();
|
||||
});
|
||||
const ROOT_DIR = join(__dirname, '..', '..');
|
||||
|
||||
ipcMain.handle('error', (_event, err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
const ELECTRON = join(
|
||||
ROOT_DIR,
|
||||
'node_modules',
|
||||
'.bin',
|
||||
process.platform === 'win32' ? 'electron.cmd' : 'electron'
|
||||
);
|
||||
|
||||
const window = new BrowserWindow({
|
||||
show: false,
|
||||
webPreferences: {
|
||||
devTools: true,
|
||||
nodeIntegration: false,
|
||||
sandbox: false,
|
||||
contextIsolation: true,
|
||||
preload: join(__dirname, 'generate-preload-cache.preload.js'),
|
||||
},
|
||||
});
|
||||
async function main(): Promise<void> {
|
||||
const storagePath = await mkdtemp(join(tmpdir(), 'signal-preload-cache-'));
|
||||
|
||||
await window.loadURL(
|
||||
pathToFileURL(join(__dirname, 'generate-preload-cache.html')).toString()
|
||||
);
|
||||
let status: number | null;
|
||||
try {
|
||||
({ status } = spawnSync(
|
||||
ELECTRON,
|
||||
['--js-args="--predictable --random-seed 1"', 'ci.js'],
|
||||
{
|
||||
cwd: ROOT_DIR,
|
||||
env: {
|
||||
...process.env,
|
||||
GENERATE_PRELOAD_CACHE: 'on',
|
||||
SIGNAL_CI_CONFIG: JSON.stringify({
|
||||
storagePath,
|
||||
}),
|
||||
},
|
||||
// Since we run `.cmd` file on Windows - use shell
|
||||
shell: process.platform === 'win32',
|
||||
}
|
||||
));
|
||||
} finally {
|
||||
await rm(storagePath, { recursive: true });
|
||||
}
|
||||
|
||||
window.webContents.openDevTools();
|
||||
window.webContents.send('compile', process.argv[2], process.argv[3]);
|
||||
if (status !== 0) {
|
||||
throw new Error(`Exit code: ${status}`);
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(error => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue