From 7b129fec9fed3c4d9a8dabe02647dd28d0190c82 Mon Sep 17 00:00:00 2001 From: altair823 Date: Sat, 25 Apr 2026 12:32:21 +0900 Subject: [PATCH] chore(build): switch main+preload to CJS for Electron 41 module hook MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two related runtime defects surfaced when first attempting `npm run build` + Electron launch: 1. Renderer html files referenced `/src/.../main.tsx`, which vite dev resolves but vite production rollup cannot. Changed both inbox and quickcapture to `./main.tsx` (sibling-relative). 2. Electron 41's main-process module hook only injects the real `electron` API into `require('electron')` calls inside CommonJS modules. With package.json `"type": "module"` set, electron-vite emitted ESM bundles that did `import { app } from "electron"`; Node's ESM CJS-interop then loaded the on-disk index.js (which exports the binary path string) instead, leaving `app` undefined at startup. Fix: drop `"type": "module"` so electron-vite emits CJS for main + preload (renderer is unaffected — vite handles its own ESM transform regardless). Source files keep modern import syntax via the default-import + destructure pattern (`import electron from 'electron'; const { app } = electron;`) which transpiles correctly to `const { app } = require('electron')` in the CJS output. Also updated `electron-log/main` -> `electron-log/main.js` because Node ESM strict resolution requires the `.js` extension even though the package's CJS entry serves both. Verification: `npm run build` produces 47-module renderer + 1005KB main bundle without errors. `npm run typecheck` clean. Unit suite (52 tests) unaffected. Plan deviation log: Task 4 (logger), Task 30 (main wiring), and Tasks 17/18/22/30 referencing electron imports landed with named imports per plan; this commit refactors them to default+destructure without changing semantics. Co-Authored-By: Claude Opus 4.7 (1M context) --- package.json | 1 - src/main/index.ts | 3 ++- src/main/ipc/captureApi.ts | 3 ++- src/main/ipc/inboxApi.ts | 4 +++- src/main/logger.ts | 5 +++-- src/main/paths.ts | 3 ++- src/main/services/HotkeyService.ts | 3 ++- src/main/tray.ts | 3 ++- src/main/windows/inboxWindow.ts | 3 ++- src/main/windows/quickCaptureWindow.ts | 3 ++- src/preload/index.ts | 3 ++- src/renderer/inbox/index.html | 2 +- src/renderer/quickcapture/index.html | 2 +- 13 files changed, 24 insertions(+), 14 deletions(-) diff --git a/package.json b/package.json index 44e2fde..6df0088 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,6 @@ "name": "inkling", "version": "0.2.0", "private": true, - "type": "module", "main": "out/main/index.js", "scripts": { "dev": "electron-vite dev", diff --git a/src/main/index.ts b/src/main/index.ts index eb891a2..2f1851d 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,4 +1,5 @@ -import { app, BrowserWindow, Notification } from 'electron'; +import electron from 'electron'; +const { app, BrowserWindow, Notification } = electron; import '@shared/types'; import { initLogger, logger } from './logger.js'; import { resolveProfilePaths } from './paths.js'; diff --git a/src/main/ipc/captureApi.ts b/src/main/ipc/captureApi.ts index cb3b53b..3c41451 100644 --- a/src/main/ipc/captureApi.ts +++ b/src/main/ipc/captureApi.ts @@ -1,6 +1,7 @@ -import { ipcMain } from 'electron'; +import electron from 'electron'; import type { CaptureService } from '../services/CaptureService.js'; import type { BrowserWindow } from 'electron'; +const { ipcMain } = electron; export function registerCaptureApi( captureService: CaptureService, diff --git a/src/main/ipc/inboxApi.ts b/src/main/ipc/inboxApi.ts index 0fc50eb..06daa52 100644 --- a/src/main/ipc/inboxApi.ts +++ b/src/main/ipc/inboxApi.ts @@ -1,4 +1,6 @@ -import { ipcMain, BrowserWindow } from 'electron'; +import electron from 'electron'; +import type { BrowserWindow } from 'electron'; +const { ipcMain } = electron; import type { NoteRepository } from '../repository/NoteRepository.js'; import type { ContinuityService } from '../services/ContinuityService.js'; import type { CaptureService } from '../services/CaptureService.js'; diff --git a/src/main/logger.ts b/src/main/logger.ts index cdf1219..e55d0e2 100644 --- a/src/main/logger.ts +++ b/src/main/logger.ts @@ -1,5 +1,6 @@ -import log from 'electron-log/main'; -import { app } from 'electron'; +import log from 'electron-log/main.js'; +import electron from 'electron'; +const { app } = electron; import { join } from 'node:path'; import { createHash } from 'node:crypto'; diff --git a/src/main/paths.ts b/src/main/paths.ts index 033e601..9437e42 100644 --- a/src/main/paths.ts +++ b/src/main/paths.ts @@ -1,4 +1,5 @@ -import { app } from 'electron'; +import electron from 'electron'; +const { app } = electron; import { join } from 'node:path'; import { mkdirSync } from 'node:fs'; diff --git a/src/main/services/HotkeyService.ts b/src/main/services/HotkeyService.ts index a7eed4d..08c54b2 100644 --- a/src/main/services/HotkeyService.ts +++ b/src/main/services/HotkeyService.ts @@ -1,4 +1,5 @@ -import { globalShortcut } from 'electron'; +import electron from 'electron'; +const { globalShortcut } = electron; export interface HotkeyBinding { accelerator: string; diff --git a/src/main/tray.ts b/src/main/tray.ts index 5c257a0..494b437 100644 --- a/src/main/tray.ts +++ b/src/main/tray.ts @@ -1,4 +1,5 @@ -import { app, Tray, Menu, nativeImage } from 'electron'; +import electron from 'electron'; +const { app, Tray, Menu, nativeImage } = electron; let tray: Tray | null = null; diff --git a/src/main/windows/inboxWindow.ts b/src/main/windows/inboxWindow.ts index 7579c5d..976b5ad 100644 --- a/src/main/windows/inboxWindow.ts +++ b/src/main/windows/inboxWindow.ts @@ -1,4 +1,5 @@ -import { BrowserWindow, app } from 'electron'; +import electron from 'electron'; +const { BrowserWindow, app } = electron; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; diff --git a/src/main/windows/quickCaptureWindow.ts b/src/main/windows/quickCaptureWindow.ts index 624db35..18c9cca 100644 --- a/src/main/windows/quickCaptureWindow.ts +++ b/src/main/windows/quickCaptureWindow.ts @@ -1,4 +1,5 @@ -import { BrowserWindow, screen } from 'electron'; +import electron from 'electron'; +const { BrowserWindow, screen } = electron; import { join } from 'node:path'; import { fileURLToPath } from 'node:url'; diff --git a/src/preload/index.ts b/src/preload/index.ts index 7024fc6..3d9acef 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -1,4 +1,5 @@ -import { contextBridge, ipcRenderer } from 'electron'; +import electron from 'electron'; +const { contextBridge, ipcRenderer } = electron; import type { InklingApi, Note } from '@shared/types'; const api: InklingApi = { diff --git a/src/renderer/inbox/index.html b/src/renderer/inbox/index.html index 8398b0a..757ae2d 100644 --- a/src/renderer/inbox/index.html +++ b/src/renderer/inbox/index.html @@ -18,6 +18,6 @@
- + diff --git a/src/renderer/quickcapture/index.html b/src/renderer/quickcapture/index.html index f00df2f..7a4b005 100644 --- a/src/renderer/quickcapture/index.html +++ b/src/renderer/quickcapture/index.html @@ -23,6 +23,6 @@
- +