fix(electron): restore packaged desktop app loading (#1095)

* fix(electron): restore packaged desktop app loading

Rebuild and verify Electron native modules before packaging and use the Vite base URL for packaged asset preloads.

* test(preload-utils): cover non-root base urls
This commit is contained in:
Tommaso Casaburi
2026-03-17 17:04:47 +08:00
committed by GitHub
parent f812fe1d18
commit c9bd6258b2
6 changed files with 93 additions and 12 deletions
+44
View File
@@ -0,0 +1,44 @@
import { spawnSync } from 'node:child_process';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const electronBinary = require('electron');
if (typeof electronBinary !== 'string' || electronBinary.length === 0) {
throw new Error('Failed to resolve the Electron executable for native-module verification');
}
const nativeModules = ['better-sqlite3'];
const verificationScript = `
console.log('electron modules', process.versions.modules);
for (const moduleName of ${JSON.stringify(nativeModules)}) {
try {
require(moduleName);
console.log('native-ok', moduleName);
} catch (error) {
console.error('native-fail', moduleName);
console.error(error && error.stack ? error.stack : String(error));
process.exit(1);
}
}
`;
const result = spawnSync(electronBinary, ['-e', verificationScript], {
encoding: 'utf8',
env: {
...process.env,
ELECTRON_RUN_AS_NODE: '1',
},
});
if (result.stdout) {
process.stdout.write(result.stdout);
}
if (result.stderr) {
process.stderr.write(result.stderr);
}
if (result.status !== 0) {
throw new Error(`Electron native-module verification failed with exit code ${result.status ?? 'unknown'}`);
}