refactor(app): migrate to ESM; preload emitted as CJS for sandbox (Phase 5d-3b)

App source (main/preload/worker/renderer) -> ESM; app is now type: module;
__dirname via import.meta.url; worker spawned with type module. Preload is built
as CJS (electron-vite output format) because the sandboxed renderer does not
support ESM preload; main loads ../preload/index.js. Removed dead imports
(nativeImage, readline) and the obsolete scripts/dev.js.

Tests: 4 app tests require->import; app-main-settings rewritten with node:test
mock.module + dynamic import (replacing CJS Module._load mocking); test script
adds --experimental-test-module-mocks. electron-vite build clean, 119/119, and
npm run dev verified: app launches, preload bridges IPC, data loads.
This commit is contained in:
tommy0103
2026-07-09 11:05:00 +08:00
parent 80c125a572
commit 905c10789a
16 changed files with 313 additions and 555 deletions
+7 -4
View File
@@ -1,5 +1,8 @@
const path = require('path');
const { Worker } = require('worker_threads');
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { Worker } from 'node:worker_threads';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
function createWorkerBuildIndex({
workerPath = path.join(__dirname, 'indexer-worker.js'),
@@ -16,7 +19,7 @@ function createWorkerBuildIndex({
const ensureWorker = () => {
if (worker) return worker;
worker = new WorkerImpl(workerPath);
worker = new WorkerImpl(workerPath, { type: 'module' });
worker.on('message', (message) => {
const current = pending.get(message.id);
if (!current) return;
@@ -57,4 +60,4 @@ function createWorkerBuildIndex({
return { buildIndex, stop };
}
module.exports = { createWorkerBuildIndex };
export { createWorkerBuildIndex };