From ced52bdf420b5f2423799fe45a03ba53a1a7c803 Mon Sep 17 00:00:00 2001 From: ByteYue Date: Fri, 27 Mar 2026 15:44:49 +0800 Subject: [PATCH] fix(plugin): prevent raw .ts import crash when esbuild transpilation fails (#500) When a TS plugin is installed but esbuild is unavailable or transpilation fails silently, the plugin discovery would attempt to import() the raw .ts file, causing 'Unknown file extension .ts' on production Node.js. Changes: - discovery.ts: Skip raw .ts import when no compiled .js exists; show an actionable warning guiding the user to re-transpile or install esbuild - plugin.ts: Upgrade esbuild-not-found from debug to warn level; log the outer catch error instead of silently swallowing it Closes #500 --- src/discovery.ts | 11 +++++++---- src/plugin.ts | 9 ++++++--- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/discovery.ts b/src/discovery.ts index 15ce9af6..32301a53 100644 --- a/src/discovery.ts +++ b/src/discovery.ts @@ -218,11 +218,14 @@ async function discoverPluginDir(dir: string, site: string): Promise { file.endsWith('.ts') && !file.endsWith('.d.ts') && !file.endsWith('.test.ts') ) { const jsFile = file.replace(/\.ts$/, '.js'); + // Prefer compiled .js — skip the .ts source file if (fileSet.has(jsFile)) return; - if (!(await isCliModule(filePath))) return; - await import(pathToFileURL(filePath).href).catch((err) => { - log.warn(`Plugin ${site}/${file}: ${getErrorMessage(err)}`); - }); + // No compiled .js found — cannot import raw .ts in production Node.js. + // This typically means esbuild transpilation failed during plugin install. + log.warn( + `Plugin ${site}/${file}: no compiled .js found. ` + + `Run "opencli plugin update ${site}" to re-transpile, or install esbuild.` + ); } })); } diff --git a/src/plugin.ts b/src/plugin.ts index 3397c95b..8d2853fe 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -937,7 +937,10 @@ function transpilePluginTs(pluginDir: string): void { const esbuildBin = resolveEsbuildBin(); if (!esbuildBin) { - log.debug('esbuild not found in host node_modules, via resolve, or in PATH, skipping TS transpilation'); + log.warn( + 'esbuild not found. TS plugin files will not be transpiled and may fail to load. ' + + 'Install esbuild (`npm i -g esbuild`) or ensure it is available in the opencli host node_modules.' + ); return; } @@ -965,8 +968,8 @@ function transpilePluginTs(pluginDir: string): void { log.warn(`Failed to transpile ${tsFile}: ${getErrorMessage(err)}`); } } - } catch { - // Non-fatal: skip transpilation if anything goes wrong + } catch (err) { + log.warn(`TS transpilation setup failed: ${getErrorMessage(err)}`); } }