fix: 修复打包后模块无法加载的问题,改用静态模块加载

This commit is contained in:
2026-03-11 01:32:06 +08:00
parent 1fa17f7c9d
commit bbd33339a5
4 changed files with 63 additions and 54 deletions

View File

@@ -1,51 +1,59 @@
import { readdirSync, statSync } from 'fs'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import type { ApiModule } from '../infra/types.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
const moduleFactoryPattern = /^create\w+Module$/
async function discoverModules(): Promise<ApiModule[]> {
return await getStaticModules()
}
async function getStaticModules(): Promise<ApiModule[]> {
const modules: ApiModule[] = []
const entries = readdirSync(__dirname)
for (const entry of entries) {
const entryPath = join(__dirname, entry)
try {
const { createTodoModule } = await import('./todo/index.js')
modules.push(createTodoModule())
} catch (e) {
console.warn('[ModuleLoader] Failed to load todo module:', e)
}
try {
const stats = statSync(entryPath)
if (!stats.isDirectory()) {
continue
}
try {
const { createTimeTrackingModule } = await import('./time-tracking/index.js')
modules.push(createTimeTrackingModule())
} catch (e) {
console.warn('[ModuleLoader] Failed to load time-tracking module:', e)
}
const moduleIndexPath = join(entryPath, 'index.ts')
let moduleIndexStats: ReturnType<typeof statSync>
try {
moduleIndexStats = statSync(moduleIndexPath)
} catch {
continue
}
if (!moduleIndexStats.isFile()) {
continue
}
try {
const { createRecycleBinModule } = await import('./recycle-bin/index.js')
modules.push(createRecycleBinModule())
} catch (e) {
console.warn('[ModuleLoader] Failed to load recycle-bin module:', e)
}
const moduleExports = await import(`./${entry}/index.js`)
try {
const { createPyDemosModule } = await import('./pydemos/index.js')
modules.push(createPyDemosModule())
} catch (e) {
console.warn('[ModuleLoader] Failed to load pydemos module:', e)
}
for (const exportName of Object.keys(moduleExports)) {
if (moduleFactoryPattern.test(exportName)) {
const factory = moduleExports[exportName]
if (typeof factory === 'function') {
const module = factory() as ApiModule
modules.push(module)
}
}
}
} catch (error) {
console.warn(`[ModuleLoader] Failed to load module '${entry}':`, error)
}
try {
const { createDocumentParserModule } = await import('./document-parser/index.js')
modules.push(createDocumentParserModule())
} catch (e) {
console.warn('[ModuleLoader] Failed to load document-parser module:', e)
}
try {
const { createAiModule } = await import('./ai/index.js')
modules.push(createAiModule())
} catch (e) {
console.warn('[ModuleLoader] Failed to load ai module:', e)
}
try {
const { createRemoteModule } = await import('./remote/index.js')
modules.push(createRemoteModule())
} catch (e) {
console.warn('[ModuleLoader] Failed to load remote module:', e)
}
modules.sort((a, b) => {

View File

@@ -1,6 +1,6 @@
import chokidar, { FSWatcher } from 'chokidar';
import path from 'path';
import { NOTEBOOK_ROOT } from '../config/paths.js';
import { config } from '../config/index.js';
import { eventBus } from '../events/eventBus.js';
import { logger } from '../utils/logger.js';
import { toPosixPath } from '../../shared/utils/path.js';
@@ -10,16 +10,17 @@ let watcher: FSWatcher | null = null;
export const startWatcher = (): void => {
if (watcher) return;
logger.info(`Starting file watcher for: ${NOTEBOOK_ROOT}`);
const notebookRoot = config.notebookRoot;
logger.info(`Starting file watcher for: ${notebookRoot}`);
watcher = chokidar.watch(NOTEBOOK_ROOT, {
watcher = chokidar.watch(notebookRoot, {
ignored: /(^|[\/\\])\../,
persistent: true,
ignoreInitial: true,
});
const broadcast = (event: string, changedPath: string) => {
const rel = path.relative(NOTEBOOK_ROOT, changedPath);
const rel = path.relative(notebookRoot, changedPath);
if (!rel || rel.startsWith('..') || path.isAbsolute(rel)) return;
logger.info(`File event: ${event} - ${rel}`);
eventBus.broadcast({ event, path: toPosixPath(rel) });