build(app): migrate to electron-vite (main/preload/renderer), keep electron-builder (Phase 5d-3a)

Restructure app into src/{main,preload,renderer}; electron.vite.config.ts builds
all three (each main module its own input so CJS requires + the indexer worker
resolve; better-sqlite3 externalized; Vue plugin for renderer). main/index.js
paths updated for the out/ layout + ELECTRON_RENDERER_URL. Still JS/CJS — TS+ESM
and core consumption are the next stages. Verified: npm run dev launches clean;
electron-vite build succeeds; root suite 119/119.
This commit is contained in:
tommy0103
2026-07-09 10:20:18 +08:00
parent e04c07de05
commit 80c125a572
69 changed files with 572 additions and 78 deletions
+60
View File
@@ -0,0 +1,60 @@
const path = require('path');
const { Worker } = require('worker_threads');
function createWorkerBuildIndex({
workerPath = path.join(__dirname, 'indexer-worker.js'),
WorkerImpl = Worker,
} = {}) {
let worker = null;
let nextId = 1;
const pending = new Map();
const rejectPending = (error) => {
for (const { reject } of pending.values()) reject(error);
pending.clear();
};
const ensureWorker = () => {
if (worker) return worker;
worker = new WorkerImpl(workerPath);
worker.on('message', (message) => {
const current = pending.get(message.id);
if (!current) return;
pending.delete(message.id);
if (message.error) {
const error = new Error(message.error.message);
error.stack = message.error.stack;
current.reject(error);
} else {
current.resolve(message.result);
}
});
worker.on('error', (error) => {
rejectPending(error);
worker = null;
});
worker.on('exit', (code) => {
if (pending.size) rejectPending(new Error(`Indexer worker exited with code ${code}`));
worker = null;
});
return worker;
};
const buildIndex = (args = {}) => new Promise((resolve, reject) => {
const id = nextId++;
pending.set(id, { resolve, reject });
ensureWorker().postMessage({ id, args });
});
const stop = () => {
const current = worker;
worker = null;
const termination = current?.terminate ? Promise.resolve(current.terminate()) : Promise.resolve();
rejectPending(new Error('Indexer worker stopped'));
return termination;
};
return { buildIndex, stop };
}
module.exports = { createWorkerBuildIndex };