Files
inkling/src/main/logger.ts
altair823 7b129fec9f chore(build): switch main+preload to CJS for Electron 41 module hook
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) <noreply@anthropic.com>
2026-04-25 12:32:21 +09:00

30 lines
1.1 KiB
TypeScript

import log from 'electron-log/main.js';
import electron from 'electron';
const { app } = electron;
import { join } from 'node:path';
import { createHash } from 'node:crypto';
let initialized = false;
export function initLogger(): void {
if (initialized) return;
const logDir = join(app.getPath('userData'), 'Inkling', 'logs');
log.transports.file.resolvePathFn = () =>
join(logDir, `main-${new Date().toISOString().slice(0, 10)}.log`);
log.transports.file.maxSize = 5 * 1024 * 1024;
log.transports.file.level = 'info';
log.transports.console.level = process.env.INKLING_DEBUG === '1' ? 'debug' : 'info';
initialized = true;
}
export function hashPrefix(text: string): string {
return createHash('sha256').update(text).digest('hex').slice(0, 8);
}
export const logger = {
info: (msg: string, meta?: Record<string, unknown>) => log.info(msg, meta ?? {}),
warn: (msg: string, meta?: Record<string, unknown>) => log.warn(msg, meta ?? {}),
error: (msg: string, meta?: Record<string, unknown>) => log.error(msg, meta ?? {}),
debug: (msg: string, meta?: Record<string, unknown>) => log.debug(msg, meta ?? {})
};